using Valg.EVA.Skanning.UserInterfaceElements.LightBox;
using Valg.EVA.Skanning.UserInterfaceElements.UserSettings;
using Valg.EVA.Skanning.UserInterfaceElements;

namespace Valg.EVA.Skanning.Verify.Startup.VerifyBallotPaper.Messages
{
    public class UnknownBallotInformationViewModel : ViewModelBase, IUnknownBallotInformationViewModel
    {
        private readonly IUserSettingsProvider settingManager;
        private bool dontShowUnknownBallotInformation;
        private bool? isOkButtonFocused;

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

        
        
        
        
        
        
        
        public bool DontShowUnknownBallotInformation
        {
            get { return dontShowUnknownBallotInformation; }
            set
            {
                dontShowUnknownBallotInformation = value;
                OnPropertyChanged();
            }
        }

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

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

        public void ShowUnknownBallotInformationIfNotHiddenByUser()
        {
            if (!DontShowUnknownBallotInformation)
            {
                DelayedFocusSetter.SetFocus(() => IsOkButtonFocused = true);

                var eventArgs = new LightBoxEventArgs(typeof(UnknownBallotInformationWindow), this);
                PublishEvent<ShowLightBoxEvent, LightBoxEventArgs>(eventArgs);
            }
        }

        private void SetPropertyValues()
        {
            UserSettings settings = settingManager.GetUserSettings();
            DontShowUnknownBallotInformation = !settings.ShowUnknownBallotInformation;
        }
    }
}
