using System.Collections.Generic;
using Valg.EVA.Skanning.Contracts.CommonEnums;

namespace Valg.EVA.Skanning.Commands.SaveBallotPaperVerifications
{
    public class ResetAllFieldsFilter : IFilter<ResetAllFieldsFilterInput, bool>
    {
        private readonly FieldValueOperationsHelper fieldValueOperationsHelper;

        public ResetAllFieldsFilter(FieldValueOperationsHelper fieldValueOperationsHelper)
        {
            this.fieldValueOperationsHelper = fieldValueOperationsHelper;
        }

        public bool Execute(ResetAllFieldsFilterInput input)
        {
            bool wasReset = false;
            var command = input.Command;
            if (ShouldResetAllFields(command))
            {
                int ballotPaperId = command.BallotPaperId;
                
                fieldValueOperationsHelper.ResetFieldValues(ballotPaperId, 
                    new List<FieldType> { FieldType.Stamp, FieldType.BallotID, FieldType.BallotNo, FieldType.MarkIncr, FieldType.Candidate, FieldType.Ca_renumber, FieldType.MarkDecr }, 
                    input.CurrentFieldValues);
                
                wasReset = true;
            }

            return wasReset;
        }

        private bool ShouldResetAllFields(SaveBallotPaperVerificationsCommand command)
        {
            return command.IsRejectedBecauseOfInvalidStamp;
        }
    }
}
