package com.fp.frontend.webservices.rest.services;

import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import com.fp.common.logger.APPLogger;
import com.fp.dto.Request;
import com.fp.frontend.helper.CallerHelper;
import com.fp.frontend.webservices.rest.dto.StatusMessage;
import com.fp.frontend.webservices.rest.util.JwtValidateException;
import com.fp.persistence.parmas.dto.ws.ArmaDto;

@Path("/arma")
@Produces(MediaType.APPLICATION_JSON)
public class ArmasResource extends CommonResource {

    @GET
    @Path("/{serie}")
    public Response consultarArma(@PathParam("serie") String serie, @HeaderParam("Authorization") String bearerToken) {
        Response response = null;
        try {
            ArmaDto armaDto = null;
            validateToken(bearerToken);
            CallerHelper callerHelper = new CallerHelper();
            Request request = callerHelper.getRequest();
            request.setCompany(1);
            request.put("queryalias", "ARMASWS");
            request.put("codigoarma", serie);
            com.fp.dto.Response responseMaia = callerHelper.executeQuery(request);
            if (responseMaia.getResponseCode().compareTo(com.fp.dto.Response.RESPONSE_OK) == 0) {
                armaDto = (ArmaDto) responseMaia.get("ARMASWS");
                if (armaDto == null) {
                    throw new WebApplicationException(
                            Response.status(Status.BAD_REQUEST)
                            .entity(new StatusMessage(Status.BAD_REQUEST.getStatusCode(), "El cod. arma no existe")).build());
                }
            } else {
                throw new WebApplicationException(
                        Response.status(Status.BAD_REQUEST)
                        .entity(new StatusMessage(Status.BAD_REQUEST.getStatusCode(), "Error al realizar la consulta del arma")).build());
            }
            response = Response.ok().entity(armaDto).build();
        } catch (JwtValidateException e) {
            APPLogger.getLogger().error(e.getMessage(), e);
            response = Response.status(e.getStatusMessage().getStatus()).entity(e.getStatusMessage()).build();
        } catch (WebApplicationException e) {
            APPLogger.getLogger().error(e.getMessage(), e);
            response = e.getResponse();
        } catch (Exception e) {
            APPLogger.getLogger().error(e.getMessage(), e);
            response = Response.serverError().entity(new StatusMessage(Status.INTERNAL_SERVER_ERROR.getStatusCode(), e.getMessage())).build();
        } catch (Throwable e) {
            APPLogger.getLogger().error(e.getMessage(), e);
            response = Response.serverError().entity(new StatusMessage(Status.INTERNAL_SERVER_ERROR.getStatusCode(), e.getMessage())).build();
        }
        return response;
    }
}
