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

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
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.LoginData;
import com.fp.frontend.webservices.rest.dto.LoginResult;
import com.fp.frontend.webservices.rest.dto.StatusMessage;
import com.fp.frontend.webservices.rest.util.JwtValidateException;

@Path("/security")
@Produces(MediaType.APPLICATION_JSON)
public class SecurityResource extends CommonResource {

    @POST
    @Path("/login")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response login(LoginData loginData) {
        APPLogger.getLogger().info("Ingresa a Login MOB");
        Response response = null;
        try {
            String respuesta = "0";
            CallerHelper callerHelper = new CallerHelper();
            Request request = callerHelper.getRequest();
            request.setCompany(1);
            request.put("USERCODE", loginData.getUsername());
            request.put("PASSWORD", callerHelper.getPasswordEncriptado(loginData.getPassword()));
            request.put("queryalias", "CONSULTALOGIN");
            com.fp.dto.Response responseMaia = callerHelper.executeQuery(request);
            if (responseMaia.getResponseCode().compareTo(com.fp.dto.Response.RESPONSE_OK) == 0) {
                respuesta = (String) responseMaia.get("RESPUESTA");//esta contiene la localidad de la direccion principal del usuario
                if (respuesta == null || respuesta.isEmpty()) {
                    throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(new StatusMessage(Status.BAD_REQUEST.getStatusCode(), "Usuario/Password Incorrecto")).build());
                }
            } else {
                throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(new StatusMessage(Status.BAD_REQUEST.getStatusCode(), "Usuario/Password Incorrecto")).build());
            }
            String token = generateTokenJwt(loginData.getUsername());
            response = Response.ok().entity(new LoginResult(loginData.getUsername(), token)).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;
    }

}
