using System.Collections.ObjectModel;
using System.Windows.Input;
using Valg.EVA.Skanning.UserInterfaceElements.Language;
using Valg.EVA.Skanning.UserInterfaceElements.UserSettings;
using Prism.Commands;

namespace Valg.EVA.Skanning.UserInterfaceElements.UserSettingWindow
{
    public class UserSettingWindowViewModel : ViewModelBase, IUserSettingWindowViewModel
    {
        private readonly ILanguageManager languageManager;
        private readonly IUserSettingsProvider userSettingsProvider;
        private readonly DelegateCommand saveCommand;
        private UserSettings.UserSettings userSettings;
        private readonly DelegateCommand resetCommand;

        public UserSettingWindowViewModel(ILanguageManager languageManager, IUserSettingsProvider userSettingsProvider)
        {
            this.languageManager = languageManager;
            this.userSettingsProvider = userSettingsProvider;
            userSettings = userSettingsProvider.GetUserSettings();

            saveCommand = new DelegateCommand(Save);
            resetCommand = new DelegateCommand(Reset);
        }

        protected override void SubscribeToViewModelSpecificEvents()
        {
            SubscribeToEvent<LanguageChangedEvent, LanguageChangedEventArgs>(NotifyPropertyChanged);
        }

        private void NotifyPropertyChanged(LanguageChangedEventArgs eventArgs)
        {
            OnPropertyChanged(null);
        }

        public ICommand SaveCommand => saveCommand;
        public ICommand ResetCommand => resetCommand;

        public string ResetText => Resources.Language.LanguageString.ResetUserSettings;
        public string OkText => Resources.Language.LanguageString.Button_Ok;
        public string CancelText => Resources.Language.LanguageString.Button_Cancel;
        public string UserSettingsText => Resources.Language.LanguageString.Label_UserSettingsText;

        public UserSettings.UserSettings UserSettings
        {
            get { return userSettings; }
            set
            {
                if (userSettings != value)
                {
                    userSettings = value;
                    OnPropertyChanged();
                }
            }
        }

        public ObservableCollection<AdditionalSettingCommand> AdditionalSettings { get; } = new ObservableCollection<AdditionalSettingCommand>();

        public void Save()
        {
            userSettingsProvider.SaveUserSettings(UserSettings);
            languageManager.SetGuiLanguage(UserSettings.Language);
        }

        public void Reset()
        {
            userSettingsProvider.ResetUserSettings();
            UserSettings = userSettingsProvider.GetUserSettings();
            languageManager.SetGuiLanguage(UserSettings.Language);
        }
    }
}

