using Valg.EVA.Skanning.Entity.SqlConnection;
using Valg.EVA.Skanning.Entity.WriteModel;
using Valg.EVA.Skanning.Repositories.BallotPaper;

namespace Valg.EVA.Skanning.Commands.SaveCombinedScanImage
{
    public class SaveCombinedScanImageCommandHandler : IHandleCommands<SaveCombinedScanImageCommand>
    {
        private readonly IBallotPaperWritableRepository ballotPaperWritableRepository;
        private readonly TransactionScopeFactory transactionScopeFactory;

        public SaveCombinedScanImageCommandHandler(IBallotPaperWritableRepository ballotPaperWritableRepository, TransactionScopeFactory transactionScopeFactory)
        {
            this.ballotPaperWritableRepository = ballotPaperWritableRepository;
            this.transactionScopeFactory = transactionScopeFactory;
        }

        public void HandleCommand(SaveCombinedScanImageCommand command)
        {
            using (var scope = transactionScopeFactory.GetTransactionScope())
            {
                ballotPaperWritableRepository.UpdateBallotPaperCombinedImage(new BallotPaperCombinedImageInsert(command.BallotPaperId, command.CombinedImage));
                scope.Complete();
            }
        }
    }
}
