package in.cdac.medinfo.csnotk.csnoserv.rest;


import static  in.cdac.medinfo.csnotk.csnoserv.CSnoServResource.*;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

//Logs
import in.cdac.medinfo.csnotk.csnolib.util.LoggerUtility;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

@Path("/disb")
public class DISBController{

    /**
     * Retrieve Substance detail from DISB
     *
     * @param id       SNOMED CT concept identifier of Substance
     * @param callback
     * @return JSON object of Substance detail
     */
    @GET
    @Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8" })
    @Consumes({ MediaType.APPLICATION_FORM_URLENCODED })
    @Path("/substanceinfo")
    public Response getSubstanceInfo(@QueryParam("id") String id, @QueryParam("callback") String callback) {
        String jsonString;
        initDISBProperties();
        Logger logger = LogManager.getLogger(DISBController.class);
        if (id == null || id.equalsIgnoreCase("") || !id.matches(_0_9)) {
            logger.error(LoggerUtility.DISBException, INVALID_QUERY_PARAMETER_VALUE_ID , id);
            return CORSHeaderSupport.addCORSHeader(Response.Status.BAD_REQUEST,Response.Status.BAD_REQUEST.toString());
        }
        try {
            jsonString = disbAgent.getSubstanceDetail(id.trim());
            if (jsonString == null)
                jsonString = "{}";

            // if callback in URL and is not just the "?" (e.g. from localhost)
            if (callback != null && callback.length() > 1)
                return CORSHeaderSupport.addCORSHeader(Response.Status.OK,callback + "(" + jsonString + ");");
            else
                return CORSHeaderSupport.addCORSHeader(Response.Status.OK,jsonString);
        } catch (Exception e) {
            // Log the Exception
            logger.error(LoggerUtility.DISBException, LoggerUtility.createErrorMessage(e));
            return CORSHeaderSupport.addCORSHeader(Response.Status.INTERNAL_SERVER_ERROR
                    ,Response.Status.INTERNAL_SERVER_ERROR.toString() + " : " + e.getMessage());
        }
    }

    /**
     * Retrieve Generic detail from DISB
     *
     * @param id       SNOMED CT concept identifier of Substance
     * @param callback
     * @return JSON object of Generic detail
     */
    @GET
    @Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8" })
    @Consumes({ MediaType.APPLICATION_FORM_URLENCODED })
    @Path("/genericinfo")
    public Response getGenericInfo(@QueryParam("id") String id, @QueryParam("callback") String callback) {
        String jsonString;
        initDISBProperties();
        Logger logger = LogManager.getLogger(DISBController.class);
        if (id == null || id.equalsIgnoreCase("") || !id.matches(_0_9)) {
            logger.error(LoggerUtility.DISBException, INVALID_QUERY_PARAMETER_VALUE_ID , id);
            return CORSHeaderSupport.addCORSHeader(Response.Status.BAD_REQUEST,Response.Status.BAD_REQUEST.toString());
        }
        try {
            jsonString = disbAgent.getGenericDetail(id.trim());
            if (jsonString == null)
                jsonString = "{}";

            // if callback in URL and is not just the "?" (e.g. from localhost)
            if (callback != null && callback.length() > 1)
                return CORSHeaderSupport.addCORSHeader(Response.Status.OK,callback + "(" + jsonString + ");");
            else
                return CORSHeaderSupport.addCORSHeader(Response.Status.OK,jsonString);
        } catch (Exception e) {
            // Log the Exception
            logger.error(LoggerUtility.DISBException, LoggerUtility.createErrorMessage(e));
            return CORSHeaderSupport.addCORSHeader(Response.Status.INTERNAL_SERVER_ERROR,
                    Response.Status.INTERNAL_SERVER_ERROR.toString() + " : " + e.getMessage());
        }
    }

    /**
     * Retrieve Medicine detail from DISB
     *
     * @param id       SNOMED CT concept identifier of Substance
     * @param callback
     * @return JSON object of Medicine detail
     */
    @GET
    @Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8" })
    @Consumes({ MediaType.APPLICATION_FORM_URLENCODED })
    @Path("/medicineinfo")
    public Response getMedicineInfo(@QueryParam("id") String id, @QueryParam("callback") String callback) {
        String jsonString;
        initDISBProperties();
        Logger logger = LogManager.getLogger(DISBController.class);
        if (id == null || id.equalsIgnoreCase("") || !id.matches(_0_9)) {
            logger.error(LoggerUtility.DISBException, INVALID_QUERY_PARAMETER_VALUE_ID , id);
            return CORSHeaderSupport.addCORSHeader(Response.Status.BAD_REQUEST,Response.Status.BAD_REQUEST.toString());
        }
        try {
            jsonString = disbAgent.getMedicineDetail(id.trim());
            if (jsonString == null)
                jsonString = "{}";

            // if callback in URL and is not just the "?" (e.g. from localhost)
            if (callback != null && callback.length() > 1)
                return CORSHeaderSupport.addCORSHeader(Response.Status.OK,callback + "(" + jsonString + ");");
            else
                return CORSHeaderSupport.addCORSHeader(Response.Status.OK,jsonString);
        } catch (Exception e) {
            // Log the Exception
            logger.error(LoggerUtility.DISBException, LoggerUtility.createErrorMessage(e));
            return CORSHeaderSupport.addCORSHeader(Response.Status.INTERNAL_SERVER_ERROR,
                    Response.Status.INTERNAL_SERVER_ERROR.toString() + " : " + e.getMessage());
        }
    }




}