package org.kar.karideo.api;

import java.io.InputStream;
import java.util.List;
import java.util.UUID;

import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import org.kar.archidata.annotation.AsyncType;
import org.kar.archidata.annotation.TypeScriptProgress;
import org.kar.archidata.dataAccess.DBAccess;
import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.dataAccess.QueryCondition;
import org.kar.archidata.dataAccess.addOnSQL.AddOnDataJson;
import org.kar.archidata.dataAccess.options.Condition;
import org.kar.archidata.tools.DataTools;
import org.kar.karideo.model.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.security.RolesAllowed;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.PATCH;
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.MediaType;

@Path("/type")
@Produces(MediaType.APPLICATION_JSON)
public class TypeResource {
	static final Logger LOGGER = LoggerFactory.getLogger(TypeResource.class);

	@GET
	@RolesAllowed("USER")
	@Operation(description = "Get all Type", tags = "GLOBAL")
	public List<Type> gets() throws Exception {
		return DataAccess.gets(Type.class);
	}

	@GET
	@Path("{id}")
	@RolesAllowed("USER")
	@Consumes(MediaType.APPLICATION_JSON)
	@Operation(description = "Get a specific Type with his ID", tags = "GLOBAL")
	public Type get(@PathParam("id") final Long id) throws Exception {
		return DataAccess.get(Type.class, id);
	}

	public static Type getId(final Long id) throws Exception {
		return DataAccess.get(Type.class, id);
	}

	/* ============================================================================= ADMIN SECTION: ============================================================================= */

	@POST
	@RolesAllowed("ADMIN")
	@Consumes(MediaType.APPLICATION_JSON)
	@Operation(description = "Create a new Type", tags = "GLOBAL")
	public Type post(final Type jsonRequest) throws Exception {
		return DataAccess.insert(jsonRequest);
	}

	@PATCH
	@Path("{id}")
	@RolesAllowed("ADMIN")
	@Consumes(MediaType.APPLICATION_JSON)
	@Operation(description = "Modify a specific Type", tags = "GLOBAL")
	public Type patch(@PathParam("id") final Long id, @AsyncType(Type.class) final String jsonRequest) throws Exception {
		DataAccess.updateWithJson(Type.class, id, jsonRequest);
		return DataAccess.get(Type.class, id);
	}

	@DELETE
	@Path("{id}")
	@RolesAllowed("ADMIN")
	@Operation(description = "Remove a specific Type", tags = "GLOBAL")
	public void remove(@PathParam("id") final Long id) throws Exception {
		DataAccess.delete(Type.class, id);
	}

	@POST
	@Path("{id}/cover")
	@RolesAllowed("ADMIN")
	@Consumes({ MediaType.MULTIPART_FORM_DATA })
	@Operation(description = "Upload a new season cover Type", tags = "GLOBAL")
	@TypeScriptProgress
	public Type uploadCover(@PathParam("id") final Long id, @FormDataParam("file") final InputStream fileInputStream, @FormDataParam("file") final FormDataContentDisposition fileMetaData)
			throws Exception {
		try (DBAccess dbIo = DBAccess.createInterface()) {
			DataTools.uploadCover(dbIo, Type.class, id, fileInputStream, fileMetaData);
			return dbIo.get(Type.class, id);
		}
	}

	@DELETE
	@Path("{id}/cover/{coverId}")
	@RolesAllowed("ADMIN")
	@Operation(description = "Remove a specific cover of a type", tags = "GLOBAL")
	public Type removeCover(@PathParam("id") final Long id, @PathParam("coverId") final UUID coverId) throws Exception {

		try (DBAccess dbIo = DBAccess.createInterface()) {
			AddOnDataJson.removeLink(dbIo, Type.class, "id", id, "covers", coverId);
			return dbIo.get(Type.class, id);
		}
	}

	public static Type getOrCreate(final String name) {
		try {
			Type out = DataAccess.getWhere(Type.class, new Condition(new QueryCondition("name", "=", name)));
			if (out == null) {
				out = new Type();
				out.name = name;
				out = DataAccess.insert(out);
			}
			return out;
		} catch (final Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
