﻿using System.Collections.Generic;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using NotifierReborn.Gui.Windows.Infrastructure;
using NotifierReborn.Gui.Windows.Properties;
using Toofz.Logging;

namespace NotifierReborn.Gui.Windows.ViewModels
{
    internal sealed class OptionsWindowViewModel : ViewModelBase, IOptionsWindowViewModel
    {
        private ILogger log;

        /// <summary>
        /// Short-hand for default settings.
        /// </summary>
        private Settings Settings = Settings.Default;

        #region Properties

        #region IsDefaultMailto

        /// <summary>
        /// Identifies the <see cref="IsDefaultMailto" /> property.
        /// </summary>
        public const string IsDefaultMailtoPropertyName = "IsDefaultMailto";
        private bool _IsDefaultMailto;
        public bool IsDefaultMailto
        {
            get { return _IsDefaultMailto; }
            set
            {
                if (_IsDefaultMailto == value)
                    return;
                var oldValue = _IsDefaultMailto;
                _IsDefaultMailto = value;
                RaisePropertyChanged(IsDefaultMailtoPropertyName, oldValue, value, true);
            }
        }

        #endregion

        #region Browsers

        /// <summary>
        /// Identifies the <see cref="Browsers" /> property.
        /// </summary>
        public const string BrowsersPropertyName = "Browsers";
        private IEnumerable<WindowsBrowser> _Browsers;
        public IEnumerable<WindowsBrowser> Browsers
        {
            get { return _Browsers; }
            set
            {
                if (_Browsers == value)
                    return;
                var oldValue = _Browsers;
                _Browsers = value;
                RaisePropertyChanged(BrowsersPropertyName, oldValue, value, true);
            }
        }

        #endregion

        #region SelectedBrowser

        /// <summary>
        /// Identifies the <see cref="SelectedBrowser" /> property.
        /// </summary>
        public const string SelectedBrowserPropertyName = "SelectedBrowser";
        private WindowsBrowser _SelectedBrowser;
        public WindowsBrowser SelectedBrowser
        {
            get { return _SelectedBrowser; }
            set
            {
                if (_SelectedBrowser == value)
                    return;
                var oldValue = _SelectedBrowser;
                _SelectedBrowser = value;
                RaisePropertyChanged(SelectedBrowserPropertyName, oldValue, value, true);
            }
        }

        #endregion

        #endregion

        #region Commands

        /// <summary>
        /// Identifies the <see cref="OK" /> command.
        /// </summary>
        public const string OKNotification = "OptionsOK";
        private RelayCommand _OK;
        public RelayCommand OK
        {
            get
            {
                return _OK ?? (_OK = new RelayCommand(
                    () =>
                    {
                        log.Information("Pushed OK on Options window");
                        Util.SendMessage(OKNotification);

                        Mailto.SetDefault(IsDefaultMailto);

                        if (SelectedBrowser != null)
                            Settings.SelectedBrowserKey = SelectedBrowser.Id;
                        else
                            Settings.SelectedBrowserKey = null;
                        Settings.Save();
                    }));
            }
        }

        /// <summary>
        /// Identifies the <see cref="Cancel" /> command.
        /// </summary>
        public const string CancelNotification = "OptionsCancel";
        private RelayCommand _Cancel;
        public RelayCommand Cancel
        {
            get
            {
                return _Cancel ?? (_Cancel = new RelayCommand(
                    () =>
                    {
                        log.Information("Pushed Cancel on Options window");
                        Util.SendMessage(CancelNotification);
                    }));
            }
        }

        #endregion

        /// <summary>
        /// Initializes an instance of the <see cref="OptionsWindowViewModel" /> class.
        /// </summary>
        public OptionsWindowViewModel(ILogger log)
        {
            this.log = log;
            Initialize();
        }

        public void Initialize()
        {
            log.Information("");
            log.Information("===========================");
            log.Information("Initializing Options window");
            log.Information("");

            IsDefaultMailto = Mailto.IsDefaultMailto;

            log.Information("Installed browsers:");
            foreach (var browser in WindowsBrowser.InstalledBrowsers)
                log.Information("    {0}", browser.Name);
            Browsers = WindowsBrowser.InstalledBrowsers;

            var logSelectedBrowser = !string.IsNullOrEmpty(Settings.SelectedBrowserKey) ? Settings.SelectedBrowserKey : WindowsBrowser.Default.ToString();
            SelectedBrowser = WindowsBrowser.GetBrowser(Settings.SelectedBrowserKey);

            log.Information("Selected browser = {0}", logSelectedBrowser);
            log.Information("IsDefaultMailto = {0}", IsDefaultMailto);

            log.Information("");
            log.Information("===========================");
            log.Information("");
        }
    }
}
