/*
 * Copyright 2018 Scytl Secure Electronic Voting SA
 *
 * All rights reserved
 *
 * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
 */
package com.scytl.products.ov.ag.ui.ws.rs.operation.voting;

import com.google.gson.JsonObject;
import okhttp3.RequestBody;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import javax.ws.rs.Consumes;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;

import com.scytl.products.ov.ag.services.infrastructure.remote.voting.VotingWorkflowVotingClient;
import com.scytl.products.ov.ag.ui.ws.rs.RestApplication;
import com.scytl.products.ov.ag.ui.ws.rs.proxy.XForwardedForFactory;
import com.scytl.products.ov.ag.ui.ws.rs.proxy.XForwardedForFactoryImpl;
import com.scytl.products.ov.commons.infrastructure.remote.client.InputStreamTypedOutput;
import com.scytl.products.ov.commons.infrastructure.remote.client.RetrofitConsumer;
import com.scytl.products.ov.commons.infrastructure.remote.client.RetrofitException;
import com.scytl.products.ov.commons.tracking.TrackIdGenerator;
import com.scytl.products.ov.commons.util.PropertiesFileReader;

/**
 * The endpoint for confirmation messages
 */
@Stateless(name = "ag-ConfirmationMessageResource")
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
@Path(ConfirmationMessageResource.RESOURCE_PATH)
public class ConfirmationMessageResource {

    static final String RESOURCE_PATH = RestApplication.API_OV_VOTING_BASEURI + "/confirm";

    // The properties file reader.
    private static final PropertiesFileReader PROPERTIES = PropertiesFileReader.getInstance();

    // The path to the resource authentication information.
    static final String CONFIRMATION_PATH = PROPERTIES.getPropertyValue("CONFIRMATION_PATH");

    private final XForwardedForFactory xForwardedForFactory = XForwardedForFactoryImpl.getInstance();

    private VotingWorkflowVotingClient votingWorkflowVotingClient;

    private TrackIdGenerator trackIdGenerator;

    private static final Logger LOG = LoggerFactory.getLogger(ConfirmationMessageResource.class);
    
    @Inject
    ConfirmationMessageResource(VotingWorkflowVotingClient votingWorkflowVotingClient,
                                TrackIdGenerator trackIdGenerator) {
        this.votingWorkflowVotingClient = votingWorkflowVotingClient;
        this.trackIdGenerator = trackIdGenerator;
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("")
    public Response validateConfirmationMessage( //
            @PathParam(RestApplication.PARAMETER_VALUE_VERSION) final String version, //
            @PathParam(RestApplication.PARAMETER_VALUE_TENANT_ID) final String tenantId, //
            @PathParam(RestApplication.PARAMETER_VALUE_ELECTION_EVENT_ID) final String electionEventId, //
            @PathParam(RestApplication.PARAMETER_VALUE_VOTING_CARD_ID) final String votingCardId, //
            @HeaderParam(RestApplication.PARAMETER_AUTHENTICATION_TOKEN) final String authenticationTokenJsonString, //
            @NotNull final InputStream confirmation,
            @Context final HttpServletRequest request) {
        String xForwardedFor = xForwardedForFactory.newXForwardedFor(request);
        String trackingId = trackIdGenerator.generate();

        RequestBody body = new InputStreamTypedOutput(MediaType.APPLICATION_JSON, confirmation);

        try {
        	JsonObject processResponse = RetrofitConsumer.processResponse(
        			votingWorkflowVotingClient.validateConfirmationMessage(CONFIRMATION_PATH, tenantId, electionEventId, votingCardId,
                            authenticationTokenJsonString, xForwardedFor, trackingId, body));
	        return Response.ok().entity(processResponse.toString()).build();
        } catch (RetrofitException rfE) {
        	LOG.error("Error trying to validate confirmation message.", rfE);
			return Response.status(rfE.getHttpCode()).build();
		}
    }
}
