using Valg.EVA.Skanning.Entity.SqlConnection;
using Valg.EVA.Skanning.Repositories.Box;
using Valg.EVA.Skanning.Repositories.Counting;
using System;
using Valg.EVA.Skanning.Commands.UpdateBallotPaperSeenStatus;
using Valg.EVA.Skanning.Diagnostics;

namespace Valg.EVA.Skanning.Commands.ResolveLockedBallots
{
    public class ResolveLockedBallotsCommandHandler : IHandleCommandsWithReturnValue<ResolveLockedBallotsCommand, ResolveLockedBallotsCommandResult>
    {
        private readonly IBoxRepository boxRepository;
        private readonly ICommandDispatcher commandDispatcher;
        private readonly ICountingRepository countingRepository;
        private readonly ICountingWriteableRepository countingWriteableRepository;
        private readonly TransactionScopeFactory transactionScopeFactory;

        public ResolveLockedBallotsCommandHandler(IBoxRepository boxRepository, ICommandDispatcher commandDispatcher, ICountingRepository countingRepository,
            ICountingWriteableRepository countingWriteableRepository, TransactionScopeFactory transactionScopeFactory)
        {
            this.boxRepository = boxRepository;
            this.commandDispatcher = commandDispatcher;
            this.countingRepository = countingRepository;
            this.countingWriteableRepository = countingWriteableRepository;
            this.transactionScopeFactory = transactionScopeFactory;
        }

        public void HandleCommand(ResolveLockedBallotsCommand command)
        {
            CommandResult = new ResolveLockedBallotsCommandResult();

            using (var scope = transactionScopeFactory.GetTransactionScope())
            {
                try
                {
                    if (command.IsInEmergencyMode)
                    {
                        var counting = countingRepository.GetCountingByBoxCountingId(command.BoxCountingId);
                        if (!counting.IsProcessedInEmergencyMode)
                        {
                            countingWriteableRepository.MarkCountingAsProcessedDuringEmergencMode(counting.Id);
                        }
                    }

                    var lockedBallotPapers = boxRepository.GetLockedBallotPapersInBoxCounting(command.BoxCountingId);
                    foreach (var ballotPaper in lockedBallotPapers)
                    {
                        commandDispatcher.DispatchCommand<UpdateBallotPaperSeenStatusCommand, UpdateBallotPaperSeenStatusCommandResult>(new UpdateBallotPaperSeenStatusCommand 
                        {
                            BallotPaperId = ballotPaper.BallotPaperId,
                            BallotPaperConcurrencyToken = ballotPaper.CurrentBallotPaperValuesId,
                            ChangedBy = command.ChangedBy,
                            RevokeSeenStatus = true
                        });
                    }
                    scope.Complete();

                    CommandResult.UnlockedBallotPapers = lockedBallotPapers.Count;
                    CommandResult.Success = true;
                }
                catch (Exception exception)
                {
                    EvaSkanningEventSource.Log.UnlockOfLockedBallotsFailed(exception.ToString());
                    CommandResult.Success = false;
                    CommandResult.Exception = exception;
                }
            }
        }

        public ResolveLockedBallotsCommandResult CommandResult { get; private set; }
    }
}
