using Itrycn_Project2;
using ryCommon;
using RyHardWare;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Itrycn
{
    public partial class FrmLogin : Form
    {
        public delegate void LoginHandler(object sender,out bool LoginSuccess);
        [Description("点击登录按钮时激发")]
        public event LoginHandler OnLoginClick;
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
        private void BtnLogin_Click(object sender, EventArgs e)
        {
            txtUserId.Enabled = false;
            txtPwd.Enabled = false;
            btnLogin.Enabled = false;
            LoginPram.UserId = txtUserId.Text;
            LoginPram.Pwd = txtPwd.Text;
            bool LoginSuccess = false;
            OnLoginClick?.Invoke(this,out LoginSuccess);
            txtUserId.Enabled = true;
            txtPwd.Enabled = true;
            btnLogin.Enabled = true;
            if (LoginSuccess)
            {
                #region 登录成功
                ryCommon.Ini ryIni = new Ini(Soft_ConstInfo.UserDataFolder + "\\Setting.dat");
                if (chkAutoLogin.Checked || chkRememberUserId.Checked)
                {
                    ryIni.WriteIni("User", "id", LoginPram.UserId);
                    if (chkAutoLogin.Checked)
                    {
                        LoginPram.isAutoLogin = 2;
                        ryIni.WriteIni("User", "pwd", rySafe.AES.Encode("121" + LoginPram.Pwd, RyHardWare.Network.GetMacs() + "|" + LoginPram.UserId));
                    }
                    else
                    {
                        LoginPram.isAutoLogin = 1;
                        ryIni.DelKey("User", "pwd");
                    }
                }
                else
                {
                    ryIni.DelKey("User", "id");
                    LoginPram.isAutoLogin = 0;
                } 
                #endregion
                DialogResult = DialogResult.OK;
            }
        }

        private void TxtPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Enter)
            {
                btnLogin.PerformClick();
            }
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            lblTitle.Text = Soft_ConstInfo.Soft_Title;
            if (System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(Soft_ConstInfo.UserDataFolder)))
            {
                System.IO.Directory.CreateDirectory(Soft_ConstInfo.UserDataFolder);
            }
            ryCommon.Ini ryIni = new Ini(Soft_ConstInfo.UserDataFolder + "\\Setting.dat");
            txtUserId.Text=ryIni.ReadIni("User", "id", LoginPram.UserId);
            if(txtUserId.Text!="")
            { chkRememberUserId.Checked = true; }
            string pwd = rySafe.AES.Decode(ryIni.ReadIni("User", "pwd", ""), Network.GetMacs() + "|" + txtUserId.Text);
            if (pwd.IndexOf("121") == 0) { pwd = pwd.Substring(3); } else { pwd = ""; }
            txtPwd.Text = pwd;
            if (pwd != "")
            {
                chkAutoLogin.Checked = true;
                txtUserId.Enabled = false;
                txtPwd.Enabled = false;
                btnLogin.PerformClick();
                btnLogin.Enabled = false;
            }
            else
            {
                txtUserId.SelectionLength = 0;
                txtUserId.SelectionStart = 0;
                txtPwd.Select();
            }
        }

        private void ChkAutoLogin_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAutoLogin.Checked) { chkRememberUserId.Checked = true; }
        }
    }
    public static class LoginPram
    {
        public static string UserId = "";
        public static string Pwd = "";
        public static int isAutoLogin = 0;
    }
}
