using System.Threading.Tasks;
using Valg.EVA.Skanning.Entity.SqlConnection;
using Valg.EVA.Skanning.Repositories.Transfers;

namespace Valg.EVA.Skanning.Commands.UpdateTransferState
{
    public class UpdateTransferStateAsyncCommandHandler : IHandleAsyncCommands<UpdateTransferStateCommand>
    {
        private readonly TransactionScopeFactory transactionScopeFactory;
        private readonly ITransferWriteableRepository transferWriteableRepository;

        public UpdateTransferStateAsyncCommandHandler(TransactionScopeFactory transactionScopeFactory, ITransferWriteableRepository transferWriteableRepository)
        {
            this.transactionScopeFactory = transactionScopeFactory;
            this.transferWriteableRepository = transferWriteableRepository;
        }

        public async Task HandleCommandAsync(UpdateTransferStateCommand command)
        {
            using (var scope = transactionScopeFactory.GetTransactionScope())
            {
                await transferWriteableRepository.UpdateTransferStateAsync(command.TransferState, command.LastUpdated, command.LastTransferState, command.TransferId, command.UpdatedBy).ConfigureAwait(false);
                scope.Complete();
            }
        }
    }
}
