using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XKRS.UI.Main
{
    public partial class AdvancedPwdFrm : Form
    {
        public static Action<bool> OnLoginOK { get; set; }//内置泛型委托，以参数形式传递方法
        string pwd = "";//定义一个字符串字段存储密码
        string PWD//属性
        {
            get
            {
                if (string.IsNullOrEmpty(pwd))
                {
                    var pwdSetting = ConfigurationManager.AppSettings["Pwd"];//获取当前默认的数据
                    if (!string.IsNullOrEmpty(pwdSetting))//如果输入非空且不是null
                    {
                        pwd = pwdSetting.ToString();
                    }
                    else
                    {
                        pwd = "p@ssw0rd";
                    }
                }
                return pwd;
            }
        }
        public AdvancedPwdFrm()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            CheckInputPassword();
        }
        private void CheckInputPassword()
        {
            string input = txtPwd.Text.Trim();//移出当前字符串前导与结尾空白字符串
            if (string.IsNullOrWhiteSpace(input))
            {
                MessageBox.Show("Please input password");
                return;
            }
            if (input == PWD)
            {
                OnLoginOK?.Invoke(true);
                DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("Wrong Password");
                DialogResult = DialogResult.Cancel;
            }

        }

        private void txtPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                CheckInputPassword();
            }
        }

        private void btnExitLogin_Click(object sender, EventArgs e)
        {
            OnLoginOK?.Invoke(false);
            this.DialogResult = DialogResult.Abort;
        }
    }
}
