package br.edu.ifpb.ifopendoors.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import br.edu.ifpb.ifopendoors.dao.DoorDAO;
import br.edu.ifpb.ifopendoors.dao.OpenDAO;
import br.edu.ifpb.ifopendoors.dao.PersonDAO;
import br.edu.ifpb.ifopendoors.dao.RoomDAO;
import br.edu.ifpb.ifopendoors.dao.TypeRoomDAO;
import br.edu.ifpb.ifopendoors.entity.Door;
import br.edu.ifpb.ifopendoors.entity.Erro;
import br.edu.ifpb.ifopendoors.entity.Open;
import br.edu.ifpb.ifopendoors.entity.Person;
import br.edu.ifpb.ifopendoors.entity.Room;
import br.edu.ifpb.ifopendoors.exception.SQLExceptionIFOpenDoors;
import br.edu.ifpb.ifopendoors.util.BancoUtil;
import br.edu.ifpb.ifopendoors.validatio.Validate;

@Path("door")
public class DoorController {

	@POST
	@Path("/insert")
	@Consumes("application/json")
	@Produces("application/json")
	public Response open(Door door) {
		
		ResponseBuilder builder = Response.status(Response.Status.BAD_REQUEST);
		builder.expires(new Date());
		
		try {			
			
			Integer idDoor = DoorDAO.getInstance().insert(door);
			
			if (idDoor != BancoUtil.IDVAZIO) {

				builder.status(Response.Status.OK);
				builder.entity(door);
			}
		
		} catch (SQLExceptionIFOpenDoors qme) {
			
			Erro erro = new Erro();
			erro.setCodigo(qme.getErrorCode());
			erro.setMensagem(qme.getMessage());

			builder.status(Response.Status.INTERNAL_SERVER_ERROR).entity(erro);
			
		}		
		
		return builder.build();	
	}
	
	@GET
	@Path("/all")
	@Produces("application/json")
	public List<Door> getAll() {
		
		List<Door> door = new ArrayList<Door>();
		
		door = DoorDAO.getInstance().getAll();
		
		return door;
	}
	
	@GET
	@Path("/allWithoutRoom")
	@Produces("application/json")
	public List<Door> getAllWithoutRoom() {
		
		List<Door> door = new ArrayList<Door>();
		
		door = DoorDAO.getInstance().getAllWithoutRoom();
		
		return door;
	}
}
