using Valg.EVA.Skanning.Entity.SqlConnection;
using Valg.EVA.Skanning.Repositories.EmergencyMode;

namespace Valg.EVA.Skanning.Commands.EmergencyMode
{
    public class StopEmergencyModeCommandHandler : IHandleCommands<StopEmergencyModeCommand>
    {
        private readonly IEmergencyModeWriteableRepository emergencyModeWriteableRepository;
        private readonly TransactionScopeFactory transactionScopeFactory;

        public StopEmergencyModeCommandHandler(IEmergencyModeWriteableRepository emergencyModeWriteableRepository, TransactionScopeFactory transactionScopeFactory)
        {
            this.emergencyModeWriteableRepository = emergencyModeWriteableRepository;
            this.transactionScopeFactory = transactionScopeFactory;
        }

        public void HandleCommand(StopEmergencyModeCommand command)
        {
            using (var scope = transactionScopeFactory.GetTransactionScope())
            {
                emergencyModeWriteableRepository.Stop(command.ModifiedBy);
                scope.Complete();
            }
        }
    }
}
