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

using INT69DC_ImageDll;
using INT69DC_7C.Forms;
using INT69DC_7C.Part11_UserManager;
using System.Text.RegularExpressions;

namespace INT69DC_7C.DialogForms
{
    public partial class DialogFormLogOn1 : Form
    {
        #region Field
        private bool IsPart11;

        // false : 패스워드 변경, true : 로그인
        private bool IsLogin;
        private string ID;
        private string Password;

        UserManager.UserMgr_user_info_t UserInfo = new UserManager.UserMgr_user_info_t();

        private FormMain m_ParentForm;
        #endregion

        #region Constructor
        public DialogFormLogOn1(FormMain parent, DataStore.LanguageID language, bool isPart11)
        {
            InitializeComponent();

            this.ParentForm = parent;
            this.IsPart11 = isPart11;
            this.IsLogin = true;

            this.InitializeContnrol();
            this.InitializeDesignLogOn(language);
            this.DefaultSettingLogOn();
        }
        public DialogFormLogOn1(FormMain parent, DataStore.LanguageID language, string id)
        {
            InitializeComponent();

            this.ParentForm = parent;
            this.ID = id;
            this.IsLogin = false;

            this.InitializeContnrol();
            this.InitializeDesignChangePassword(language);
            this.DefaultSettingChangePassword();
        } 
        #endregion

        #region Property
        public FormMain ParentForm
        {
            get { return this.m_ParentForm; }
            private set { this.m_ParentForm = value; }
        }
        #endregion

        #region Method
        private void InitializeContnrol()
        {
            int x = 0, y = 0;

            x = Screen.PrimaryScreen.Bounds.Width / 2 - this.Size.Width / 2;
            y = Screen.PrimaryScreen.Bounds.Height / 2 - this.Size.Height / 2;

            this.Location = new Point(x, y);

            this.Size = new Size(450, 165);
        }
        public void InitializeDesignLogOn(DataStore.LanguageID language)
        {
            ImageDll images = new ImageDll();

            this.pictureBoxImage.Image = new Bitmap(images.GetImage(ImageDll.ButtonImages.LoginImage1));

            if (language == DataStore.LanguageID.English)
            {
                this.labelTitle.Text = "User Login";
                this.labelTitleID.Text = "ID";
                this.labelTitlePassword.Text = "Password";

                this.buttonCancel.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainLoginCancelDisable));
                this.buttonCancel.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainLoginCancelDown));
                this.buttonCancel.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainLoginCancelUp));
                this.buttonLogon.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainLoginLoginDisable));
                this.buttonLogon.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainLoginLoginDown));
                this.buttonLogon.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainLoginLoginUp));
            }
            else if (language == DataStore.LanguageID.Chinese)
            {
                this.labelTitle.Text = "登錄";
                this.labelTitleID.Text = "用户名";
                this.labelTitlePassword.Text = "密码";

                this.buttonCancel.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnMainLoginCancelDisable));
                this.buttonCancel.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnMainLoginCancelDown));
                this.buttonCancel.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnMainLoginCancelUp));
                this.buttonLogon.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnMainLoginLoginDisable));
                this.buttonLogon.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnMainLoginLoginDown));
                this.buttonLogon.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnMainLoginLoginUp));
            }
            else if (language == DataStore.LanguageID.Czech)
            {
            }
            else
            {
                this.labelTitle.Text = "로그인";
                this.labelTitleID.Text = "ID";
                this.labelTitlePassword.Text = "비밀번호";

                this.buttonCancel.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.korMainLoginCancelDisable));
                this.buttonCancel.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.korMainLoginCancelDown));
                this.buttonCancel.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.korMainLoginCancelUp));
                this.buttonLogon.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.korMainLoginLoginDisable));
                this.buttonLogon.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.korMainLoginLoginDown));
                this.buttonLogon.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.korMainLoginLoginUp));
            }
        }
        private void DefaultSettingLogOn()
        {
            this.textBoxID.Text = "";
            this.textBoxPassword.Text = "";

            this.smartKeyboard.KeyboardType = SmartX.SmartKeyboard.KEYBOARDTYPES.NORMAL;
            this.smartKeyboard.Show();
        }
        private void Login()
        {
            // Check ID
            if (this.textBoxID.Text.Length < 6)
            {
                // ID : 6~20자 입력하세요
                DialogFormMessage msg = new DialogFormMessage(null, 3, this.ParentForm.SystemConfig.Language);
                msg.ShowDialog();

                return;
            }
            // Check Password
            if (this.textBoxPassword.Text.Length < 6)
            {
                // PASSWORD : 6~20자 입력하세요
                DialogFormMessage msg = new DialogFormMessage(null, 9, this.ParentForm.SystemConfig.Language);
                msg.ShowDialog();

                return;
            }

            UserManager.UserManager_UserLoginDirect(this.textBoxID.Text, this.textBoxPassword.Text, ref this.UserInfo);

            if (this.UserInfo.status == 0)
            {
                #region 로그인 성공
                try
                {
                    string sValue = "";

                    this.SuccessLogin(this.UserInfo);

                    //sValue = this.ParentForm.ChildFormMainDisplay.DisplayExpireOfPassword(this.ParentForm.SystemConfig.CurrentUser.Group);
                    if (sValue != "-")
                    {
                        int intExpireDate = int.Parse(sValue);
                        if (intExpireDate >= -10)
                        {
                            int extract = int.Parse(Regex.Replace(sValue, @"\D", ""));
                            DialogFormYesNo df = new DialogFormYesNo(this.ParentForm.SystemConfig.Language, 2, extract.ToString());
                            df.BringToFront();
                            if (df.ShowDialog() == DialogResult.Yes)
                            {
                                DialogFormLogOn1 changePW = new DialogFormLogOn1(this.ParentForm, this.ParentForm.SystemConfig.Language, this.textBoxID.Text);
                                if (changePW.ShowDialog() == DialogResult.OK)
                                {
                                    UserManager.UserManager_UserLockRelease(this.textBoxID.Text, false, true);
                                    UserManager.UserManager_UserLoginDirect(this.textBoxID.Text, this.ParentForm.SystemConfig.CurrentUser.Password, ref this.UserInfo);

                                    this.SuccessLogin(this.UserInfo);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    FormMain.Exception(ex);

                    this.DialogResult = DialogResult.Cancel;
                    this.Close();

                    return;
                }

                this.DialogResult = DialogResult.OK;
                this.Close();
                #endregion
            }
            else
            {
                if (this.UserInfo.status == 15) // USER_MGR_STATUS_PW_PASSWORD_LOCK
                {
                    this.Close();

                    DialogFormLogOn1 changePW = new DialogFormLogOn1(this.ParentForm, this.ParentForm.SystemConfig.Language, this.textBoxID.Text);
                    if (changePW.ShowDialog() == DialogResult.OK)
                    {
                        UserManager.UserManager_UserLoginDirect(this.textBoxID.Text, this.ParentForm.SystemConfig.CurrentUser.Password, ref this.UserInfo);
                        // console
                        Console.WriteLine(this.UserInfo.status.ToString());
                        this.SuccessLogin(this.UserInfo);

                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                }
                else
                {
                    DialogFormMessage myMsg = new DialogFormMessage(null, this.UserInfo.status, this.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
            }
        }
        private void SuccessLogin(UserManager.UserMgr_user_info_t user)
        {
            if (user.active_level == 1)
                this.ParentForm.SystemConfig.CurrentUser.Group = DataStore.UserGroup.Level1;
            else if (user.active_level == 2)
                this.ParentForm.SystemConfig.CurrentUser.Group = DataStore.UserGroup.Level2;
            else if (user.active_level == 3)
                this.ParentForm.SystemConfig.CurrentUser.Group = DataStore.UserGroup.Level3;
            else if (user.active_level == 9)
                this.ParentForm.SystemConfig.CurrentUser.Group = DataStore.UserGroup.Admin;
            else if (user.active_level == 10)
                this.ParentForm.SystemConfig.CurrentUser.Group = DataStore.UserGroup.Developer;
            this.ParentForm.SystemConfig.CurrentUser.ActiveLevel = user.active_level;

            this.ParentForm.SystemConfig.CurrentUser.ID = user.user_id;
            this.ParentForm.SystemConfig.CurrentUser.Password = user.user_pw;

            this.ParentForm.SystemConfig.CurrentUser.ExpireId = user.id_expire_period;
            this.ParentForm.SystemConfig.CurrentUser.ExpirePassword = user.password_expire_period;

            // console
            Console.WriteLine(user.password_login_date.GetDateTime());
            DateTime time = DateTime.ParseExact(user.password_login_date.GetDateTime(), "yyyyMMddHHmmss", null);
            this.ParentForm.SystemConfig.CurrentUser.DatePasswordLogin = time;
            // console
            Console.WriteLine(user.id_register_date.GetDateTime());
            time = DateTime.ParseExact(user.id_register_date.GetDateTime(), "yyyyMMddHHmmss", null);
            this.ParentForm.SystemConfig.CurrentUser.DateIdRegister = time;

            if (this.ParentForm.SystemConfig.CurrentUser.Group == DataStore.UserGroup.Admin
                || this.ParentForm.SystemConfig.CurrentUser.Group == DataStore.UserGroup.Developer)
            {
                this.ParentForm.SystemConfig.CurrentUser.DateIdExpire = DateTime.Now;
                this.ParentForm.SystemConfig.CurrentUser.DatePasswordExpire = DateTime.Now;
            }
            else
            {
                // console
                Console.WriteLine(user.id_expire_date.GetDateTime());
                time = DateTime.ParseExact(user.id_expire_date.GetDateTime(), "yyyyMMddHHmmss", null);
                this.ParentForm.SystemConfig.CurrentUser.DatePasswordExpire = time;
                // console
                Console.WriteLine(user.password_expire_date.GetDateTime());
                time = DateTime.ParseExact(user.password_expire_date.GetDateTime(), "yyyyMMddHHmmss", null);
                this.ParentForm.SystemConfig.CurrentUser.DateIdExpire = time;
            }

            this.ParentForm.SystemConfig.CurrentUser.IsLockAccount = user.id_flock_status == 0 ? false : true;
            this.ParentForm.SystemConfig.CurrentUser.IsLockPassword = user.password_flock_status == 0 ? false : true;

            if (this.ParentForm.SystemConfig.IsPart11 == true)
                this.ParentForm.SystemConfig.CurrentUser.IsAdmin = user.fadmin == 0 ? false : true;
            else
                this.ParentForm.SystemConfig.CurrentUser.IsAdmin = true;
        }

        public void InitializeDesignChangePassword(DataStore.LanguageID language)
        {
            ImageDll images = new ImageDll();

            this.pictureBoxImage.Image = new Bitmap(images.GetImage(ImageDll.ButtonImages.LoginImage2));

            if (language == DataStore.LanguageID.English)
            {
                this.labelTitle.Text = "Change Password";
                this.labelTitleID.Text = "New";
                this.labelTitlePassword.Text = "Verify";

                this.buttonCancel.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainLoginCancelDisable));
                this.buttonCancel.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainLoginCancelDown));
                this.buttonCancel.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainLoginCancelUp));
                this.buttonLogon.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainLoginChangeDisable));
                this.buttonLogon.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainLoginChangeDown));
                this.buttonLogon.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainLoginChangeUp));
            }
            else if (language == DataStore.LanguageID.Chinese)
            {
                this.labelTitle.Text = "更改密码";
                this.labelTitleID.Text = "新的";
                this.labelTitlePassword.Text = "核实";

                this.buttonCancel.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnMainLoginCancelDisable));
                this.buttonCancel.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnMainLoginCancelDown));
                this.buttonCancel.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnMainLoginCancelUp));
                this.buttonLogon.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnMainLoginChangeDisable));
                this.buttonLogon.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnMainLoginChangeDown));
                this.buttonLogon.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnMainLoginChangeUp));
            }
            else if (language == DataStore.LanguageID.Czech)
            {
            }
            else
            {
                this.labelTitle.Text = "비밀번호 변경";
                this.labelTitleID.Text = "새 비밀번호";
                this.labelTitlePassword.Text = "비밀번호 확인";

                this.buttonCancel.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.korMainLoginCancelDisable));
                this.buttonCancel.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.korMainLoginCancelDown));
                this.buttonCancel.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.korMainLoginCancelUp));
                this.buttonLogon.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.korMainLoginChangeDisable));
                this.buttonLogon.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.korMainLoginChangeDown));
                this.buttonLogon.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.korMainLoginChangeUp));
            }
        }
        private void DefaultSettingChangePassword()
        {
            this.textBoxID.Text = "";
            this.textBoxPassword.Text = "";

            this.textBoxID.PasswordChar = '*';
            this.textBoxPassword.PasswordChar = '*';

            this.smartKeyboard.KeyboardType = SmartX.SmartKeyboard.KEYBOARDTYPES.NORMAL;
            this.smartKeyboard.Show();
        }
        private void SaveChangePassword()
        {
            string detail = "";

            // Check Password
            if (this.textBoxPassword.Text.Length < 6)
            {
                // PASSWORD : 6~20자 입력하세요
                DialogFormMessage msg = new DialogFormMessage(null, 9, this.ParentForm.SystemConfig.Language);
                msg.ShowDialog();

                return;
            }

            try
            {
                if (this.textBoxID.Text != this.textBoxPassword.Text)
                {
                    // 비밀번호가 동일하지 않습니다
                    DialogFormMessage myMsg = new DialogFormMessage(null, 17, this.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    UserManager.UserManager_UserModifyPW(this.ID, this.textBoxPassword.Text);
                    this.ParentForm.SystemConfig.CurrentUser.Password = this.textBoxPassword.Text;

                    this.DialogResult = DialogResult.OK;
                    this.Close();
                    this.Dispose();
                }
            }
            catch
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
                this.Dispose();
            }
        }
        #endregion

        #region Event Handler
        private void buttonLogOn_Click(object sender, EventArgs e)
        {
            // Automatic Logout Reset
            if (this.IsPart11 == true)
                this.ParentForm.ChildFormMainDisplay.Part11AutomaticLogoutReset();

            if (this.IsLogin == true)
                this.Login();
            else
                this.SaveChangePassword();
        }
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            // Automatic Logout Reset
            if (this.IsPart11 == true)
                this.ParentForm.ChildFormMainDisplay.Part11AutomaticLogoutReset();

            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void textBoxID_GotFocus(object sender, EventArgs e)
        {
            // Automatic Logout Reset
            if (this.IsPart11 == true)
                this.ParentForm.ChildFormMainDisplay.Part11AutomaticLogoutReset();

            this.smartKeyboard.TargetInputObject = this.textBoxID;

            this.textBoxID.Select(this.textBoxID.Text.Length, 0);
        }
        private void textBoxPassword_GotFocus(object sender, EventArgs e)
        {
            // Automatic Logout Reset
            if (this.IsPart11 == true)
                this.ParentForm.ChildFormMainDisplay.Part11AutomaticLogoutReset();

            this.smartKeyboard.TargetInputObject = this.textBoxPassword;

            this.textBoxPassword.Select(this.textBoxPassword.Text.Length, 0);
        }

        private void smartKeyboard_OnXKeyClick(object sender, EventArgs e)
        {
            //this.smartKeyboard.Show();
        }
        private void smartKeyboard_Click(object sender, EventArgs e)
        {

        }
        #endregion

        private void smartButton1_Click(object sender, EventArgs e)
        {
            this.textBoxID.Text = "Intech";
            this.textBoxPassword.Text = "I20090810!";
        }

        private void smartButtonAdmin_Click(object sender, EventArgs e)
        {
            this.textBoxID.Text = "admin00";
            this.textBoxPassword.Text = "admin12!";
        }

        private void smartButtonIUser11_Click(object sender, EventArgs e)
        {
            this.textBoxID.Text = "user11";
            this.textBoxPassword.Text = "user11!";
        }

        private void smartButtonUser22_Click(object sender, EventArgs e)
        {
            this.textBoxID.Text = "user22";
            this.textBoxPassword.Text = "user22!";
        }

        private void smartButtonUser33_Click(object sender, EventArgs e)
        {
            this.textBoxID.Text = "user33";
            this.textBoxPassword.Text = "user33!";
        }
    }
}