using System;
using System.Windows.Forms;
using StuMgmLib.MyNameSpace;
using System.Text.RegularExpressions;

namespace StuMgmClient
{
    public partial class LoginForm : Form
    {
        const string deaultIp = "10.10.0.44";
        const int deaultPort = 502;
        const short job_id = 01943;


        public LoginForm()
        {
            InitializeComponent();
            SystemComm.Init(deaultIp, deaultPort);
        }

        //账号密码正则校验
        public bool RegexUser(string u, string p)
        {
            var regex = new Regex("^[A-Za-z0-9]{1,5}$");
            //校验密码是否符合
            bool resultU = regex.IsMatch(u);
            bool resultP = regex.IsMatch(p);
            if (resultU == true && resultP == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void chkPassWord_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void labForgetPassword_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            Lvl userRole;
            int token;
            short job_id;
            if (VerifLogin(out job_id, out userRole, out token) != ErrCode.Success)
                return;

            if (SystemData.InitSystemData() != ErrCode.Success)
                throw new Exception("初始化异常，请重新启动");

            switch (userRole)
            {
                case Lvl.Student:
                    ShowStudentForm(job_id, token);
                    break;
                case Lvl.Teacher:
                    ShowTeacherForm(job_id, token);
                    break;
                default:
                    MessageBox.Show(userRole.ToString());
                    break;
            }
        }

        ErrCode VerifLogin(out short job_id, out Lvl userRole, out int token)
        {
            if (!RegexUser(txtUserName.Text, txtPassWord.Text))
            {
                MessageBox.Show("账号密码格式错误，请重新输入", "提示");
                job_id = 0;
                userRole = Lvl.Error;
                token = 0;
                return ErrCode.ErrData;
            }
            else
            {
                job_id = Convert.ToInt16(txtUserName.Text);
                string password = txtPassWord.Text;
                //ErrCode errcode = SystemCtrl.VerifLogin(txtUserName.Text, txtPassWord.Text, out userRole, out token);

                ErrCode errcode = SystemCtrl.VerifLogin(job_id, password, out userRole, out token);
                if (errcode != ErrCode.Success)
                    MessageBox.Show(errcode.ToString());
                return errcode;
            }
        }

        void ShowStudentForm(short job_id, int token)
        {
            this.Hide();
            StudentForm stu = new StudentForm(job_id, token);
            stu.ShowDialog();
            this.Show();
        }
        void ShowTeacherForm(short job_id, int token)
        {
            this.Hide();
            TeacherForm tea = new TeacherForm(job_id, token);
            tea.ShowDialog();
            this.Show();
        }
    }
}
