using System;
using System.Windows;
using Valg.EVA.Skanning.ContainerExtensions;
using Valg.EVA.Skanning.UserInterfaceElements.ApplicationEvents;
using Valg.EVA.Skanning.UserInterfaceElements.LightBox;
using Valg.EVA.Skanning.Identity;
using Valg.EVA.Skanning.Services.EmergencyMode;
using Valg.EVA.Skanning.UserInterfaceElements.MessageBox;
using Prism.Commands;
using Valg.EVA.Skanning.UserInterfaceElements;
using Valg.EVA.Skanning.JobManagement.Startup.DataRefresh;

namespace Valg.EVA.Skanning.JobManagement.Startup.EmergencyMode
{
    public class ResetEmergencyModeLinkViewModel : ViewModelBase, IResetEmergencyModeLinkViewModel
    {
        private readonly IEmergencyModeService emergencyModeService;
        private readonly IIdentityProvider identityProvider;
        private readonly ITypedFactory typedFactory;
        private bool isVisible;

        public ResetEmergencyModeLinkViewModel(IEmergencyModeService emergencyModeService, 
            IIdentityProvider identityProvider, ITypedFactory typedFactory)
        {
            this.emergencyModeService = emergencyModeService;
            this.identityProvider = identityProvider;
            this.typedFactory = typedFactory;
            StopEmergencyModeCommand = new DelegateCommand(StopEmergencyMode);
            SetVisibility(new DataRefreshRequestedEventArgs());
        }

        public DelegateCommand StopEmergencyModeCommand { get; }

        public bool IsVisible
        {
            get { return isVisible; }
            set
            {
                isVisible = value;
                OnPropertyChanged();
            }
        }

        public string LinkText => Resources.Language.LanguageString.EmergencyMode_TopBoxEmergencyMode_LinkText;

        private void StopEmergencyMode()
        {
            var messageBox = typedFactory.Create<IMessageBox>();
            
            messageBox.Initialize(Resources.Language.JobManagementLanguageString.DeactivateEmergencyMode,
                Resources.Language.JobManagementLanguageString.DeactivateEmergencyModeInformation, 
                MessageBoxType.Information, new DelegateCommand(
                    () =>
                    {
                        emergencyModeService.StopEmergencyMode(identityProvider.GetCurrentUserName());
                        PublishEvent<RestartApplicationRequestedEvent, EventArgs>(EventArgs.Empty);
                    }),
                null);

            var eventArgs = new LightBoxEventArgs(messageBox as Window);
            PublishEvent<ShowLightBoxEvent, LightBoxEventArgs>(eventArgs);
        }

        private void SetVisibility(DataRefreshRequestedEventArgs dataRefreshRequestedEventArgs)
        {
            try
            {
                IsVisible = emergencyModeService.IsEmergencyModeActive();
            }
            catch (Exception)
            {
                
                
                
                
                
                
            }
        }

        protected override void SubscribeToViewModelSpecificEvents()
        {
            SubscribeToEvent<DataRefreshRequestedEvent, DataRefreshRequestedEventArgs>(SetVisibility);
        }
    }
}
