/*
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
package com.arrebol.apc.ws.administration;

import com.arrebol.apc.controller.mobile.controller.admin.StableGeneralBoxController;
import com.arrebol.apc.controller.mobile.controller.admin.StableSmallBoxController;
import com.arrebol.apc.controller.mobile.controller.bitacora.BitacoraController;
import com.arrebol.apc.controller.mobile.json.loan.GenericAPCResponserJaxb;
import java.io.Serializable;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 * @author Carlos Janitzio Zavala Lopez <janitzio.zavala@arrebol.com.mx>
 */
@Path("small-boxes")
public class SmallBoxWS implements Serializable {

    @PUT
    @Path("delete-small-box")
    @Produces(MediaType.APPLICATION_JSON)
    public Response deleteSmallBox(@FormParam("id") String idSmallBox,
            @FormParam("updatedBy") String updatedBy,
            @FormParam("office") String office) {
        Response response;
        StableSmallBoxController smallBoxController = new StableSmallBoxController();
        StableGeneralBoxController generalBoxController = new StableGeneralBoxController();
        GenericAPCResponserJaxb apcResponse = new GenericAPCResponserJaxb();
        try {
            if (!generalBoxController.existStableGeneralBoxByCreatedOn(office)) {
                if (smallBoxController.disableIdSmallBoxById(idSmallBox, updatedBy)) {
                    BitacoraController bitacoraController = new BitacoraController();

                    if (bitacoraController.insertBitacoraRecord("Eliminar cuadre de caja chica", "APK", "Se borro el corte de caja chica por administrador desde el celular", updatedBy, office, "Borrado desde la aplicación móvil")) {
                        apcResponse.setSuccessful(Boolean.TRUE);
                        apcResponse.setEnabledSubmit(Boolean.FALSE);
                        apcResponse.setMessage("Se borro el corte de caja chica.");
                        response = Response.ok().entity(apcResponse).build();
                    } else {
                        apcResponse.setSuccessful(Boolean.FALSE);
                        apcResponse.setEnabledSubmit(Boolean.FALSE);
                        apcResponse.setMessage("Error al guardar la bitacora. Contacte a su supervisor.");
                        response = Response.ok().entity(apcResponse).build();
                    }
                } else {
                    apcResponse.setSuccessful(Boolean.FALSE);
                    apcResponse.setEnabledSubmit(Boolean.FALSE);
                    apcResponse.setMessage("No se borro el corte de caja chica.");
                    response = Response.ok().entity(apcResponse).build();
                }
            } else {
                apcResponse.setSuccessful(Boolean.FALSE);
                apcResponse.setEnabledSubmit(Boolean.FALSE);
                apcResponse.setMessage("Ya existe un cuadre de caja general del día, el corte de caja chica no puede ser borrado.");
                response = Response.ok().entity(apcResponse).build();
            }
        } catch (Exception e) {
            apcResponse.setSuccessful(Boolean.FALSE);
            apcResponse.setEnabledSubmit(Boolean.FALSE);
            apcResponse.setMessage("Hubo un error al intentar borrar el corte de caja chica. Notifique a su supervisor.");
            response = Response.ok().entity(apcResponse).build();
            logger.error("deleteSmallBox", e);

        }
        return response;
    }

    @GET
    @Path("find-small-box")
    @Produces(MediaType.APPLICATION_JSON)
    public Response findSmallBoxByOffice(@QueryParam("office") String idOffice) {
        Response response;
        StableSmallBoxController smallBoxController = new StableSmallBoxController();
        StableGeneralBoxController generalBoxController = new StableGeneralBoxController();
        GenericAPCResponserJaxb apcResponse = new GenericAPCResponserJaxb();
        try {
            if (!generalBoxController.existStableGeneralBoxByCreatedOn(idOffice)) {
                String id = smallBoxController.findIdStableSmallBoxByOfficeInCurrentDate(idOffice);
                if (null != id && id.length() == 36) {
                    apcResponse.setSuccessful(Boolean.TRUE);
                    apcResponse.setEnabledSubmit(Boolean.TRUE);
                    apcResponse.setIdSmallBox(id);
                    apcResponse.setMessage("Corte de caja chica encontrado.");
                    response = Response.ok(apcResponse).build();
                } else {
                    apcResponse.setSuccessful(Boolean.FALSE);
                    apcResponse.setEnabledSubmit(Boolean.FALSE);
                    apcResponse.setMessage("Corte de caja chica no existente.");
                    response = Response.ok(apcResponse).build();
                }
            } else {
                apcResponse.setSuccessful(Boolean.FALSE);
                apcResponse.setEnabledSubmit(Boolean.FALSE);
                apcResponse.setMessage("No se pude seleccionar el corte de caja chica porque ya exite un corte general.");
                response = Response.ok(apcResponse).build();
            }
        } catch (Exception e) {
            apcResponse.setSuccessful(Boolean.FALSE);
            apcResponse.setEnabledSubmit(Boolean.FALSE);
            apcResponse.setMessage("Hubo un error onteniendo la caja chica. Contacte a su administrador.");
            response = Response.status(Response.Status.NOT_ACCEPTABLE).entity(apcResponse).build();
            logger.error("findSmallBoxByOffice", e);
        }
        return response;
    }

    final Logger logger = LogManager.getLogger(getClass());
}
