using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using FLY.Thick.Base.IService;

namespace ThickTcpUiInWindow
{
    /// <summary>
    /// Window_Password.xaml 的交互逻辑
    /// </summary>
    public partial class Window_Password : FLY.ControlLibrary.WindowBigClose,INotifyPropertyChanged
    {
        IPasswordService mPassword = null;
        static PasswordKeep mPwKeep = new PasswordKeep();

        private string psk;
        public string PSK {
            get {
                return psk;
            }
            set {
                psk = value;
                NotifyPropertyChanged("PSK");
            }
        }
        public Window_Password()
        {
            InitializeComponent();
            
        }
        public int Level = 0;
       
        public static bool Authorize(int level) 
        {
            Window_Password w = new Window_Password();
            w.Level = level;
            //TODO
            w.Owner = Application.Current.MainWindow;
            return (bool)w.ShowDialog();
        }
        private void button_apply_Click(object sender, RoutedEventArgs e)
        {
            if(string.IsNullOrEmpty(PSK))
            {
                FLY.ControlLibrary.Window_WarningTip.Show("错误", "密码为空",
                    TimeSpan.FromSeconds(2));
                return;
            }
            if (mPassword == null)
            {
                this.DialogResult = true;
                this.Close();
                return;
            }
            AUTHORIZE_RESULT r = mPassword.Authorize(PSK, Level);
            switch (r) 
            {
                case AUTHORIZE_RESULT.OK:
                    {
                        mPwKeep.SetPSK(PSK);
                        this.DialogResult = true;
                        this.Close();
                        return;
                    }
                case AUTHORIZE_RESULT.ERR_LEVEL:
                    {
                        FLY.ControlLibrary.Window_WarningTip.Show("错误", $"权限不足 需要Lv{Level}以上密码",
                            TimeSpan.FromSeconds(2));
                        return;
                    }
                case AUTHORIZE_RESULT.ERR_PW:
                    {
                        FLY.ControlLibrary.Window_WarningTip.Show("错误", "密码错误",
                            TimeSpan.FromSeconds(2));
                        return;
                    }
            }
            return;
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            mPassword = Application.Current.Properties["Password"] as IPasswordService;

            this.DataContext = this;
            togglebutton_keep5min.DataContext = mPwKeep;
            string psk=null;
            if (mPwKeep.GetPSK(out psk))
            {
                PSK = psk;
            }
        }

        private void Window_Unloaded(object sender, RoutedEventArgs e)
        {

        }


        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        #endregion
    }

    /// <summary>
    /// 上一次的密码保持5分钟
    /// </summary>
    public class PasswordKeep:INotifyPropertyChanged
    {
        private bool keep5min = true;
        public bool Keep5Min 
        {
            get { return keep5min; }
            set {
                if (keep5min != value)
                {
                    keep5min = value;
                    NotifyPropertyChanged("Keep5Min");
                }
            }
        }
        string PSK;
        /// <summary>
        /// 密码输入时间
        /// </summary>
        DateTime Time = DateTime.MinValue;
        /// <summary>
        /// 复位
        /// </summary>
        public void Reset()
        {
            Time = DateTime.MinValue;
        }
        public bool GetPSK(out string psk) 
        {
            psk = null;
            if (Keep5Min == false)
                return false;

            if ((DateTime.Now - Time) > TimeSpan.FromMinutes(5)) //5分钟内
                return false;
            
            psk = PSK;
            return true;
        }
        public void SetPSK(string psk)
        {
            PSK = psk;
            Time = DateTime.Now;
        }

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        #endregion
    }
}
