/*
 * 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.ei.services.domain.service.confirmation;

import com.scytl.products.ov.commons.beans.domain.model.authentication.AuthenticationToken;
import com.scytl.products.ov.commons.validation.ValidationError;
import com.scytl.products.ov.ei.services.domain.model.confirmation.ConfirmationInformation;
import com.scytl.products.ov.ei.services.domain.model.confirmation.ConfirmationMessageValidation;
import com.scytl.products.ov.ei.services.domain.model.validation.ElectionValidationRequest;
import com.scytl.products.ov.ei.services.domain.service.election.ElectionService;

import javax.inject.Inject;

/**
 * This class implements the confirmation message is member of a mathematical group.
 */
public class ConfirmationMessageElectionDatesValidation implements ConfirmationMessageValidation {

	@Inject
	private ElectionService electionService;

	/**
	 * This method validates whether the ballot box is still active before cast the vote.
	 * 
	 * @param tenantId - the tenant identifier.
	 * @param electionEventId - the election event identifier.
	 * @param votingCardId - the voting card identifier.
	 * @param confirmationInformation - the confirmation information to be validated.
	 * @param authenticationToken - the authentication token.
	 * @return ValidationError with information about validation execution.
	 */
	@Override
	public ValidationError execute(String tenantId, String electionEventId, String votingCardId,
			ConfirmationInformation confirmationInformation, AuthenticationToken authenticationToken) {
		String ballotBoxId = authenticationToken.getVoterInformation().getBallotBoxId();
		// The validation will take into account the grace period
		ElectionValidationRequest electionValidationRequest = ElectionValidationRequest.create(tenantId, electionEventId, ballotBoxId, true);
		return electionService.validateIfElectionIsOpen(electionValidationRequest);
	}
}
