package org.kar.karusic.api;

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

import org.bson.types.ObjectId;
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.FormDataOptional;
import org.kar.archidata.annotation.TypeScriptProgress;
import org.kar.archidata.dataAccess.DBAccess;
import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.dataAccess.addOnSQL.AddOnDataJson;
import org.kar.archidata.dataAccess.options.CheckFunction;
import org.kar.archidata.tools.DataTools;
import org.kar.karusic.model.Gender;
import org.kar.karusic.model.Gender.GenderChecker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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("/gender")
@Produces({ MediaType.APPLICATION_JSON })
public class GenderResource {
	private static final Logger LOGGER = LoggerFactory.getLogger(GenderResource.class);
	static final GenderChecker CHECKER = new GenderChecker();

	@GET
	@Path("{id}")
	@RolesAllowed("USER")
	public Gender get(@PathParam("id") final Long id) throws Exception {
		return DataAccess.get(Gender.class, id);
	}

	@GET
	@RolesAllowed("USER")
	public List<Gender> gets() throws Exception {
		return DataAccess.gets(Gender.class);
	}

	@POST
	@RolesAllowed("ADMIN")
	@Consumes(MediaType.APPLICATION_JSON)
	public Gender post(final Gender data) throws Exception {
		return DataAccess.insert(data, new CheckFunction(CHECKER));
	}

	@PATCH
	@Path("{id}")
	@RolesAllowed("ADMIN")
	@Consumes(MediaType.APPLICATION_JSON)
	public Gender patch(@PathParam("id") final Long id, @AsyncType(Gender.class) final String jsonRequest) throws Exception {
		DataAccess.updateWithJson(Gender.class, id, jsonRequest, new CheckFunction(CHECKER));
		return DataAccess.get(Gender.class, id);
	}

	@DELETE
	@Path("{id}")
	@RolesAllowed("ADMIN")
	public void remove(@PathParam("id") final Long id) throws Exception {
		DataAccess.delete(Gender.class, id);
	}

	@POST
	@Path("{id}/cover")
	@RolesAllowed("ADMIN")
	@Consumes({ MediaType.MULTIPART_FORM_DATA })
	@TypeScriptProgress
	public Gender uploadCover(@PathParam("id") final Long id, @FormDataOptional @FormDataParam("uri") final String uri, @FormDataOptional @FormDataParam("file") final InputStream fileInputStream,
			@FormDataOptional @FormDataParam("file") final FormDataContentDisposition fileMetaData) throws Exception {
		try (DBAccess db = DBAccess.createInterface()) {
			if (uri != null) {
				DataTools.uploadCoverFromUri(db, Gender.class, id, uri);
			} else {
				DataTools.uploadCover(db, Gender.class, id, fileInputStream, fileMetaData);
			}
			return db.get(Gender.class, id);
		}
	}

	@DELETE
	@Path("{id}/cover/{coverId}")
	@RolesAllowed("ADMIN")
	public Gender removeCover(@PathParam("id") final Long id, @PathParam("coverId") final ObjectId coverId) throws Exception {
		try (DBAccess db = DBAccess.createInterface()) {
			AddOnDataJson.removeLink(db, Gender.class, "id", id, "covers", coverId);
			return db.get(Gender.class, id);
		}
	}
}
