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.QueryAnd;
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.Season;
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("/season")
@Produces(MediaType.APPLICATION_JSON)
public class SeasonResource {
	static final Logger LOGGER = LoggerFactory.getLogger(SeasonResource.class);

	@GET
	@RolesAllowed("USER")
	@Operation(description = "Get a specific Season with his ID", tags = "GLOBAL")
	public List<Season> gets() throws Exception {
		return DataAccess.gets(Season.class);
	}

	@GET
	@Path("{id}")
	@RolesAllowed("USER")
	@Consumes(MediaType.APPLICATION_JSON)
	@Operation(description = "Get all season", tags = "GLOBAL")
	public Season get(@PathParam("id") final Long id) throws Exception {
		return DataAccess.get(Season.class, id);
	}

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

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

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

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

	@POST
	@Path("{id}/cover")
	@RolesAllowed("ADMIN")
	@Consumes(MediaType.MULTIPART_FORM_DATA)
	@Operation(description = "Upload a new season cover season", tags = "GLOBAL")
	@TypeScriptProgress
	public Season 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, Season.class, id, fileInputStream, fileMetaData);
			return dbIo.get(Season.class, id);
		}
	}

	@DELETE
	@Path("{id}/cover/{coverId}")
	@RolesAllowed("ADMIN")
	@Operation(description = "Remove a specific cover of a season", tags = "GLOBAL")
	public Season removeCover(@PathParam("id") final Long id, @PathParam("coverId") final UUID coverId) throws Exception {
		try (DBAccess dbIo = DBAccess.createInterface()) {
			AddOnDataJson.removeLink(dbIo, Season.class, "id", id, "covers", coverId);
			return dbIo.get(Season.class, id);
		}
	}

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

}
