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;
using Unity;

namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// Window_Password.xaml 的交互逻辑
    /// </summary>
    public partial class WdPassword : FLY.ControlLibrary.WindowBigClose,INotifyPropertyChanged
    {
        PasswordAuthorize passwordAuthorize;
        static PasswordKeep mPwKeep = new PasswordKeep();

        public WdPassword()
        {
            InitializeComponent();
        }
        public void Init(PasswordAuthorize passwordAuthorize, int level) 
        {
            this.passwordAuthorize = passwordAuthorize;
            this.Level = level;
        }
        /// <summary>
        /// 密码级别
        /// </summary>
        int Level = 0;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="uiName"></param>
        /// <returns></returns>
        public static bool Authorize(string uiName) 
        {
            //从容器获取
            var container = Application.Current.Properties["container"] as IUnityContainer;
            if (container == null)
                return true;

            var passwordAuthorize = container.Resolve<PasswordAuthorize>();
            int level = passwordAuthorize.GetLv(uiName);
            if (level <= 0) 
                return true;
            
            WdPassword w = new WdPassword();
            w.Init(passwordAuthorize,level);
            w.Owner = Application.Current.MainWindow;
            return (bool)w.ShowDialog();
        }
        private void button_apply_Click(object sender, RoutedEventArgs e)
        {
            if(string.IsNullOrEmpty(passwordbox.Password))
            {
                FLY.ControlLibrary.Window_WarningTip.Show("错误", "密码为空",
                    TimeSpan.FromSeconds(2));
                return;
            }
            

            AUTHORIZE_RESULT r = passwordAuthorize.Authorize(passwordbox.Password, Level);
            switch (r) 
            {
                case AUTHORIZE_RESULT.OK:
                    {
                        mPwKeep.SetPSK(passwordbox.Password);
                        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 ButtonOSK_Click(object sender, RoutedEventArgs e)
        {
            TextBox tbPw = new TextBox
            {
                Text = passwordbox.Password,
                Tag = "full password"
            };
            tbPw.TextChanged += (s, _e) =>
            {
                var textbox = s as TextBox;
                passwordbox.Password = textbox.Text;
            };
            FLY.ControlLibrary.UI.OSK.KeyboardBehavior.PopPad(tbPw);
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //从容器获取
            //this.DataContext = this;
            togglebutton_keep5min.DataContext = mPwKeep;
            if (mPwKeep.GetPSK(out string psk))
            {
                passwordbox.Password = psk;
            }
        }
        #region INotifyPropertyChanged 成员
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
    }

    /// <summary>
    /// 上一次的密码保持5分钟
    /// </summary>
    public class PasswordKeep:INotifyPropertyChanged
    {

        /// <summary>
        /// 密码保存5分钟
        /// </summary>
        public bool Keep5Min { get; set; } = true;
        
        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;
        #endregion
    }
}
