using Valg.EVA.Skanning.Entity.WriteModel;

namespace Valg.EVA.Skanning.Repositories.BallotPaper
{
    public class BallotPaperValuesUpdater : IBallotPaperValuesUpdater
    {
        private readonly IBallotPaperWritableRepository ballotPaperWritableRepository;

        public BallotPaperValuesUpdater(IBallotPaperWritableRepository ballotPaperWritableRepository)
        {
            this.ballotPaperWritableRepository = ballotPaperWritableRepository;
        }

        public void UpdateBallotPaperValues(RequiredInputBallotPaperValues required, CopiedUnlessSpecifiedValues optional)
        {
            var ballotPaperValuesInsert = new BallotPaperValuesInsert();

            if (optional.IgnoreCandidatesIsSpecified)
            {
                ballotPaperValuesInsert.IgnoreCandidatesIsSpecified = true;
                ballotPaperValuesInsert.IgnoreCandidates = optional.IgnoreCandidates;
            }

            if (optional.IsValidIsSpecified)
            {
                ballotPaperValuesInsert.IsValidIsSpecified = true;
                ballotPaperValuesInsert.IsValid = optional.IsValid;
            }

            if (optional.NeededVerificationIsSpecified)
            {
                ballotPaperValuesInsert.NeededVerificationIsSpecified = true;
                ballotPaperValuesInsert.NeededVerification = optional.NeededVerification;
            }

            if (optional.RejectionReasonTypeIsSpecified)
            {
                ballotPaperValuesInsert.RejectionReasonTypeIsSpecified = true;
                ballotPaperValuesInsert.RejectionReasonType = optional.RejectionReasonType;
            }

            if (optional.SeenIsSpecified)
            {
                ballotPaperValuesInsert.SeenIsSpecified = true;
                ballotPaperValuesInsert.Seen = optional.Seen;
            }

            if (optional.VerificationIndexIsSpecified)
            {
                ballotPaperValuesInsert.VerificationIndexIsSpecified = true;
                ballotPaperValuesInsert.VerificationIndex = optional.VerificationIndex;
            }

            ballotPaperValuesInsert.BallotPaperId = required.BallotPaperId;
            ballotPaperValuesInsert.CreatedBy = required.ChangedBy;
            ballotPaperValuesInsert.ValueSourceType = required.ValueSourceType;
            ballotPaperValuesInsert.CurrentBallotPaperValuesId = required.BallotPaperConcurrencyToken;

            ballotPaperWritableRepository.InsertBallotPaperValues(ballotPaperValuesInsert);
        }
    }
}
