using System;
using Valg.EVA.Skanning.UserInterfaceElements;
using Valg.EVA.Skanning.UserInterfaceElements.LightBox;
using Valg.EVA.Skanning.UserInterfaceElements.UserSettings;
using Valg.EVA.Skanning.Verify.Startup.VerifyBallotPaper.Messaging.Events;

namespace Valg.EVA.Skanning.Verify.Startup.VerifyBallotPaper.VerifiableViewModelsCollection
{
    public class AutoVerifyInfoViewModel : ViewModelBase, IAutoVerifyInfoViewModel
    {
        private readonly IUserSettingsProvider settingManager;
        private bool dontShowTipOnAutoVerify;
        private bool? isOkButtonFocused;

        public AutoVerifyInfoViewModel(IUserSettingsProvider settingManager)
        {
            this.settingManager = settingManager;
            SetPropertyValues();
        }

        
        
        
        
        
        
        
        public bool DontShowTipOnAutoVerify
        {
            get { return dontShowTipOnAutoVerify; }
            set
            {
                dontShowTipOnAutoVerify = value;
                OnPropertyChanged();
            }
        }

        public bool? IsOkButtonFocused
        {
            get { return isOkButtonFocused; }
            set
            {
                isOkButtonFocused = value;
                OnPropertyChanged();
            }
        }

        
        
        
        public void ShowTipIfNotHiddenByUser()
        {
            if (!DontShowTipOnAutoVerify)
            {
                DelayedFocusSetter.SetFocus(() => IsOkButtonFocused = true);
                
                var eventArgs = new LightBoxEventArgs(typeof(AutoVerifyInfoWindow), this);
                PublishEvent<ShowLightBoxEvent, LightBoxEventArgs>(eventArgs);
            }
        }

        public void SaveSettings()
        {
            UserSettings settings = settingManager.GetUserSettings();
            settings.ShowAutoVerifyTip = !DontShowTipOnAutoVerify;
            settingManager.SaveUserSettings(settings);
        }

        private void SetPropertyValues()
        {
            UserSettings settings = settingManager.GetUserSettings();
            DontShowTipOnAutoVerify = !settings.ShowAutoVerifyTip;
        }
    }
}
