package de.oaa.xxx.aufgaben;

import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.oaa.xxx.framework.request.RequestChecker;
import jakarta.annotation.Resource;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.transaction.SystemException;
import jakarta.transaction.UserTransaction;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
import jakarta.ws.rs.core.UriInfo;

@ApplicationScoped
@Path("toy")
public class ToyResource {

	private Logger LOGGER = LoggerFactory.getLogger(getClass());
	
	@PersistenceContext(name = "aufgaben")
	private EntityManager entityManager;
	@Resource
	private UserTransaction transaction;
	
	@Path("{toyId}")
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public Response get(@PathParam("toyId") UUID toyId, @Context HttpServletRequest req)
			throws IllegalStateException, SecurityException, SystemException {
		try {
			RequestChecker.checkRequest(req);
			transaction.begin();
			ToyEntity entity = new ToyDAO(entityManager).findById(toyId);
			if (entity != null) {
				Toy toy = entity.toToy();
				transaction.commit();
				return Response.ok(toy).build();
			} else {
				transaction.rollback();
				return Response.noContent().build();
			}
		} catch (Exception exception) {
			LOGGER.error(exception.getMessage(), exception);
			transaction.rollback();
			return Response.serverError().build();
		}
	}
	
	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	public Response create(Toy toy, @Context UriInfo uriInfo, @Context HttpServletRequest req) throws IllegalStateException, SecurityException, SystemException {
		try {
			RequestChecker.checkRequest(req);
			if (toy.getName() == null || toy.getGruppeId() == null) {
				return Response.status(Status.BAD_REQUEST).build();
			}
			transaction.begin();
			AufgabenGruppeEntity gruppeEntity = new AufgabenGruppeDAO(entityManager).findById(toy.getGruppeId());
			if (gruppeEntity == null || gruppeEntity.getAufgaben().size() > 50) {			
				transaction.rollback();
				return Response.status(Status.BAD_REQUEST).build();
			}
			ToyEntity entity = ToyEntity.create(toy, gruppeEntity);
			new ToyDAO(entityManager).persist(entity);
			transaction.commit();
			return Response.created(uriInfo.getAbsolutePathBuilder().path(entity.getToyId().toString()).build()).build();
		} catch (Exception exception) {
			LOGGER.error(exception.getMessage(), exception);
			transaction.rollback();
			return Response.serverError().build();
		}
	}
	
	@DELETE
	@Consumes(MediaType.APPLICATION_JSON)
	public Response delete(Toy toy, @Context HttpServletRequest req) throws IllegalStateException, SecurityException, SystemException {
		try {
			RequestChecker.checkRequest(req);
			transaction.begin();
			ToyDAO dao = new ToyDAO(entityManager);
			ToyEntity entity = dao.findById(toy.getToyId());
			dao.remove(entity);
			transaction.rollback();
			return Response.accepted().build();
		} catch (Exception exception) {
			LOGGER.error(exception.getMessage(), exception);
			transaction.rollback();
			return Response.serverError().build();
		}
	}
}
