using MainShell.Alarm.ViewModel;
using MwFramework.AlarmManager;
using Stylet;
using System.Threading.Tasks;

namespace MainShell.Alarm
{
    [AlarmID(begin:10000, end:20011)]
    public class UserAlarmProduce : AbstractAlarmAsyncProcedure
    {
        private readonly IAlarmManager _alarmManager;
        private readonly IWindowManager _windowManager;

        public UserAlarmProduce(IAlarmManager alarmManager, IWindowManager windowManager)
        {
            _alarmManager = alarmManager;
            _windowManager = windowManager;
        }
        
        public override async Task HandleAsync(IAlarmSend inContext = null, IAlarmResponse outContext = null)
        {
            var dialogViewModel = new AlarmDialogViewModel
            {
                AlarmInfo = AlarmInfo
            };
            var resetRequested = false;
            await Execute.OnUIThreadAsync(() =>
            {
                resetRequested = _windowManager.ShowDialog(dialogViewModel) == true;
            }).ConfigureAwait(false);

            if (resetRequested)
            {
                _alarmManager.ClearPLCAlarm?.Invoke();
            }
        }
    }
}
