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 INT99DC_ImageDll;
using INT99DC_7C.Forms;
using INT99DC_7C.Part11_UserManager;

namespace INT99DC_7C.DialogForms
{
    public partial class DialogFormLogOn : Form
    {
        #region Field
        private FormMain m_ParentForm;
        private bool IsLoginMode;
        private bool IsChangePassword;
        #endregion

        #region Constructor
        public DialogFormLogOn(FormMain parent, bool login, bool changePass)
        {
            InitializeComponent();

            this.ParentForm = parent;
            this.IsLoginMode = login;
            this.IsChangePassword = changePass;

            this.InitializeDesign();
            this.InitializeControl();
            this.DefaultSetting();
        } 
        #endregion

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

        #region Method
        private void InitializeDesign()
        {
            ImageDll images = new ImageDll();

            if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
            {

            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
            {
                this.buttonLogOn.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainLoginDisable));
                this.buttonLogOn.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainLoginDown));
                this.buttonLogOn.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainLoginUp));
                this.buttonCancel.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainCancelDisable));
                this.buttonCancel.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainCancelDown));
                this.buttonCancel.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainCancelUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
                this.smartForm1.Image = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackgroundUserLogin));

                this.buttonLogOn.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnMainLoginDisable));
                this.buttonLogOn.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnMainLoginDown));
                this.buttonLogOn.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnMainLoginUp));
                this.buttonCancel.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnMainCancelDisable));
                this.buttonCancel.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnMainCancelDown));
                this.buttonCancel.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnMainCancelUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
                this.buttonLogOn.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainLoginDisable));
                this.buttonLogOn.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainLoginDown));
                this.buttonLogOn.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainLoginUp));
                this.buttonCancel.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeMainCancelDisable));
                this.buttonCancel.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeMainCancelDown));
                this.buttonCancel.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeMainCancelUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.German)
            {
                this.buttonLogOn.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainLoginDisable));
                this.buttonLogOn.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainLoginDown));
                this.buttonLogOn.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engMainLoginUp));
                this.buttonCancel.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerMainCancelDisable));
                this.buttonCancel.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerMainCancelDown));
                this.buttonCancel.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerMainCancelUp));
            }
            else
            {

            }
        }
        private void InitializeControl()
        {
            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(710, 470);

            if (this.IsChangePassword == false)
            {
                if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
                {
                    this.labelTitle1.Text = "로그인";
                    this.labelTitle2.Text = "ID";
                    this.labelTitle3.Text = "비밀번호";
                }
                else
                {
                    this.labelTitle1.Text = "User Login";
                    this.labelTitle2.Text = "ID";
                    this.labelTitle3.Text = "Password";
                }

                this.textBoxID.PasswordChar = default(char);
            }
            else
            {
                if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
                {
                    this.labelTitle1.Text = "비밀번호 변경";
                    this.labelTitle2.Text = "새 비밀번호";
                    this.labelTitle3.Text = "비밀번호 확인";
                }
                else
                {
                    this.labelTitle1.Text = "Change Password";
                    this.labelTitle2.Text = "New";
                    this.labelTitle3.Text = "Confirmation";
                }

                this.textBoxID.PasswordChar = '*';
            }

            this.textBoxID.Text = "";
            this.textBoxPassword.Text = "";

            //this.smartKeyboard.HanYoungKeyToggle();
            this.smartKeyboard.HanYoungKeyDisable = true;
            this.smartKeyboard.KeyboardType = SmartX.SmartKeyboard.KEYBOARDTYPES.NORMAL;
            this.smartKeyboard.Show();
        }
        private void DefaultSetting()
        {

        }

        private void LogOn()
        {
            UserManager.UserMgr_user_info_t userInfo = new UserManager.UserMgr_user_info_t();

            if (this.textBoxID.Text == "")
            {
                if (this.ParentForm.SystemConfig.IsPart11 == true)
                    this.ParentForm.SetTrackingHistoryData(DataStore.TrackingOperation.Login, "Fail : ID blank");

                // ID : 6~20자 입력 하세요
                DialogFormMessage myMsg = new DialogFormMessage(null, 3, this.ParentForm.SystemConfig.Language);
                myMsg.ShowDialog();

                return;
            }
            if (this.textBoxPassword.Text == "")
            {
                if (this.ParentForm.SystemConfig.IsPart11 == true)
                    this.ParentForm.SetTrackingHistoryData(DataStore.TrackingOperation.Login, "Fail : PW blank");

                // PASSWORD : 6~20자 입력 하세요
                DialogFormMessage myMsg = new DialogFormMessage(null, 9, this.ParentForm.SystemConfig.Language);
                myMsg.ShowDialog();

                return;
            }

            if (this.textBoxID.Text == "admin00" && this.textBoxPassword.Text == "admin20090810")
            {
                DialogFormYesNo dlg = new DialogFormYesNo(this.ParentForm.SystemConfig.Language, 17);
                if (dlg.ShowDialog() == DialogResult.Yes)
                {
                    this.ParentForm.ChildFormMainDisplay.ChangeID = this.textBoxID.Text;
                    this.DialogResult = DialogResult.Abort;
                    this.Close();
                }
                return;
            }

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

            if (userInfo.status == 0 && userInfo.fFirstPW == 0)
            {
                //MessageBox.Show("로그인 성공");

                #region 로그인 성공
                if (userInfo.active_level == 1)
                    this.ParentForm.SystemConfig.CurrentUser.Group = DataStore.UserGroup.Level1;
                else if (userInfo.active_level == 2)
                    this.ParentForm.SystemConfig.CurrentUser.Group = DataStore.UserGroup.Level2;
                else if (userInfo.active_level == 3)
                    this.ParentForm.SystemConfig.CurrentUser.Group = DataStore.UserGroup.Level3;
                else if (userInfo.active_level == 9)
                    this.ParentForm.SystemConfig.CurrentUser.Group = DataStore.UserGroup.Admin;
                else if (userInfo.active_level == 10)
                    this.ParentForm.SystemConfig.CurrentUser.Group = DataStore.UserGroup.Developer;

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

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

                //Console.WriteLine(userInfo.password_register_date.GetDateTime());
                //Console.WriteLine(userInfo.id_login_date.GetDateTime());
                //Console.WriteLine(userInfo.id_expire_date.GetDateTime());
                //Console.WriteLine(userInfo.password_expire_date.GetDateTime());

                DateTime time = DateTime.ParseExact(userInfo.password_register_date.GetDateTime(), "yyyyMMddHHmmss", null);
                this.ParentForm.SystemConfig.CurrentUser.DatePasswordRegister = time;
                time = DateTime.ParseExact(userInfo.id_login_date.GetDateTime(), "yyyyMMddHHmmss", null);
                this.ParentForm.SystemConfig.CurrentUser.DateIdLogin = time;
                time = DateTime.ParseExact(userInfo.id_expire_date.GetDateTime(), "yyyyMMddHHmmss", null);
                this.ParentForm.SystemConfig.CurrentUser.DateIdExpire = time;
                time = DateTime.ParseExact(userInfo.password_expire_date.GetDateTime(), "yyyyMMddHHmmss", null);
                this.ParentForm.SystemConfig.CurrentUser.DatePasswordExpire = time;

                this.ParentForm.SystemConfig.CurrentUser.IsLockAccount = userInfo.id_flock_status == 0 ? false : true;
                this.ParentForm.SystemConfig.CurrentUser.IsLockPassword = userInfo.password_flock_status == 0 ? false : true;
                this.ParentForm.SystemConfig.CurrentUser.IsAdmin = userInfo.fadmin == 0 ? false : true;
                this.ParentForm.SystemConfig.CurrentUser.ActiveLevel = userInfo.active_level;
                this.ParentForm.SystemConfig.CurrentUser.IsFirstPassword = userInfo.fFirstPW;
                #endregion

                if (this.ParentForm.SystemConfig.IsPart11 == true)
                    this.ParentForm.SetTrackingHistoryData(DataStore.TrackingOperation.Login, "Success");
                else
                    UserManager.UserManager_AutoLogoutTimeoutPause();

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else if (userInfo.fFirstPW == 1)
            {
                if (this.ParentForm.SystemConfig.IsPart11 == true)
                    this.ParentForm.SetTrackingHistoryData(DataStore.TrackingOperation.Login, "First login");
                else
                    UserManager.UserManager_AutoLogoutTimeoutPause();

                // 최초 로그인 시 비밀번호 변경
                this.ParentForm.ChildFormMainDisplay.ChangeID = this.textBoxID.Text;
                this.DialogResult = DialogResult.Abort;
                this.Close();
            }
            else
            {
                DialogFormMessage myMsg = new DialogFormMessage(null, userInfo.status, this.ParentForm.SystemConfig.Language);
                myMsg.ShowDialog();

                // 비밀번호 만료 시
                if (userInfo.status == 15)
                {
                    if (this.ParentForm.SystemConfig.IsPart11 == true)
                        this.ParentForm.SetTrackingHistoryData(DataStore.TrackingOperation.Login, "Password expiration");
                    else
                        UserManager.UserManager_AutoLogoutTimeoutPause();

                    this.ParentForm.ChildFormMainDisplay.ChangeID = this.textBoxID.Text;
                    this.DialogResult = DialogResult.Abort;
                    this.Close();
                }
                else
                {
                    if (this.ParentForm.SystemConfig.IsPart11 == true)
                        this.ParentForm.SetTrackingHistoryData(DataStore.TrackingOperation.Login, "Fail : " + this.ReturnUserStatus(userInfo.status));
                    else
                        UserManager.UserManager_AutoLogoutTimeoutPause();
                }
            }
        }
        private void ChangePassword()
        {
            int ret = 0;
            string id = "", pass = "";
            UserManager.UserMgr_user_info_t userInfo = new UserManager.UserMgr_user_info_t();

            if (this.textBoxID.Text == "")
            {
                if (this.ParentForm.SystemConfig.IsPart11 == true)
                    this.ParentForm.SetTrackingHistoryData(DataStore.TrackingOperation.Login, "Fail : TextBox1 blank");

                // New Password : 6~20자 입력 하세요
                DialogFormMessage myMsg = new DialogFormMessage(null, 9, this.ParentForm.SystemConfig.Language);
                myMsg.ShowDialog();

                return;
            }
            if (this.textBoxPassword.Text == "")
            {
                if (this.ParentForm.SystemConfig.IsPart11 == true)
                    this.ParentForm.SetTrackingHistoryData(DataStore.TrackingOperation.Login, "Fail : TextBox2 blank");

                // New Password Confirmation : 6~20자 입력 하세요
                DialogFormMessage myMsg = new DialogFormMessage(null, 9, this.ParentForm.SystemConfig.Language);
                myMsg.ShowDialog();

                return;
            }
            if (this.textBoxID.Text.Equals(this.textBoxPassword.Text) == false)
            {
                if (this.ParentForm.SystemConfig.IsPart11 == true)
                    this.ParentForm.SetTrackingHistoryData(DataStore.TrackingOperation.Login, "Fail : Diffrent char");

                // 비밀번호를 확인하세요
                DialogFormMessage myMsg = new DialogFormMessage(null, 14, this.ParentForm.SystemConfig.Language);
                myMsg.ShowDialog();

                return;
            }

            if (this.IsLoginMode == true)
            {
                id = this.ParentForm.SystemConfig.CurrentUser.ID;
                pass = this.textBoxID.Text;

                //Console.WriteLine(string.Format("UserManager_UserModifyPW : {0}, {1}", id, pass));
                ret = UserManager.UserManager_UserModifyPWDirect(id, pass);
            }
            else
            {
                id = this.ParentForm.ChildFormMainDisplay.ChangeID;
                pass = this.textBoxID.Text;

                //Console.WriteLine(string.Format("UserManager_UserModifyPW : {0}, {1}", id, pass));
                ret = UserManager.UserManager_UserModifyPWDirect(id, pass);
            }

            // 비밀번호 변경이 완료 되면 로그인 시도
            if (ret == 0)
            {
                UserManager.UserManager_UserLoginDirect(id, pass, ref userInfo);

                if (userInfo.status == 0)
                {
                    //MessageBox.Show("로그인 성공");

                    #region 로그인 성공
                    if (userInfo.active_level == 1)
                        this.ParentForm.SystemConfig.CurrentUser.Group = DataStore.UserGroup.Level1;
                    else if (userInfo.active_level == 2)
                        this.ParentForm.SystemConfig.CurrentUser.Group = DataStore.UserGroup.Level2;
                    else if (userInfo.active_level == 3)
                        this.ParentForm.SystemConfig.CurrentUser.Group = DataStore.UserGroup.Level3;
                    else if (userInfo.active_level == 9)
                        this.ParentForm.SystemConfig.CurrentUser.Group = DataStore.UserGroup.Admin;
                    else if (userInfo.active_level == 10)
                        this.ParentForm.SystemConfig.CurrentUser.Group = DataStore.UserGroup.Developer;

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

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

                    DateTime time = DateTime.ParseExact(userInfo.password_register_date.GetDateTime(), "yyyyMMddHHmmss", null);
                    this.ParentForm.SystemConfig.CurrentUser.DatePasswordRegister = time;
                    time = DateTime.ParseExact(userInfo.id_login_date.GetDateTime(), "yyyyMMddHHmmss", null);
                    this.ParentForm.SystemConfig.CurrentUser.DateIdLogin = time;
                    time = DateTime.ParseExact(userInfo.id_expire_date.GetDateTime(), "yyyyMMddHHmmss", null);
                    this.ParentForm.SystemConfig.CurrentUser.DateIdExpire = time;
                    time = DateTime.ParseExact(userInfo.password_expire_date.GetDateTime(), "yyyyMMddHHmmss", null);
                    this.ParentForm.SystemConfig.CurrentUser.DatePasswordExpire = time;

                    this.ParentForm.SystemConfig.CurrentUser.IsLockAccount = userInfo.id_flock_status == 0 ? false : true;
                    this.ParentForm.SystemConfig.CurrentUser.IsLockPassword = userInfo.password_flock_status == 0 ? false : true;
                    this.ParentForm.SystemConfig.CurrentUser.IsAdmin = userInfo.fadmin == 0 ? false : true;
                    this.ParentForm.SystemConfig.CurrentUser.ActiveLevel = userInfo.active_level;
                    this.ParentForm.SystemConfig.CurrentUser.IsFirstPassword = userInfo.fFirstPW;
                    #endregion

                    if (this.ParentForm.SystemConfig.IsPart11 == true)
                        this.ParentForm.SetTrackingHistoryData(DataStore.TrackingOperation.Login, "Success");

                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    if (this.ParentForm.SystemConfig.IsPart11 == true)
                        this.ParentForm.SetTrackingHistoryData(DataStore.TrackingOperation.Login, "Fail : " + this.ReturnUserStatus(ret));

                    DialogFormMessage myMsg = new DialogFormMessage(null, ret, this.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
            }
            else
            {
                if (this.ParentForm.SystemConfig.IsPart11 == true)
                    this.ParentForm.SetTrackingHistoryData(DataStore.TrackingOperation.Login, "Failure change password");

                // 비밀번호 변경 실패 시 메시지??
                //Console.WriteLine(ret.ToString());
                DialogFormMessage myMsg = new DialogFormMessage(null, ret, this.ParentForm.SystemConfig.Language);
                myMsg.ShowDialog();
            }
        }
        private string ReturnUserStatus(int num)
        {
            string ret = "";

            switch (num)
            {
                case 1:
                    ret = "ID_Not found";
                    break;
                case 2:
                    ret = "ID_Duplication";
                    break;
                case 3:
                    ret = "ID_Too short";
                    break;
                case 4:
                    ret = "ID_Too long";
                    break;
                case 5:
                    ret = "ID_Consecutive or duplicate char";
                    break;
                case 6:
                    ret = "ID_Invalid char";
                    break;
                case 7:
                    ret = "ID_Lock";
                    break;
                case 8:
                    ret = "PW_Duplicate prev PW";
                    break;
                case 9:
                    ret = "PW_Too short";
                    break;
                case 10:
                    ret = "PW_Too long";
                    break;
                case 11:
                    ret = "PW_Consecutive or duplicate char";
                    break;
                case 12:
                    ret = "PW_Invalid char";
                    break;
                case 13:
                    ret = "PW_Not include char";
                    break;
                case 14:
                    ret = "PW_Incorrect";
                    break;
                case 15:
                    ret = "PW_Lock";
                    break;
                case 16:
                    ret = "PW_Over expire period";
                    break;
                case 17:
                    ret = "ID_Over expire period";
                    break;
                case 18:
                    ret = "Auto logout";
                    break;
                default:
                    ret = "";
                    break;
            }

            return ret;
        }
        #endregion

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

            if (this.IsChangePassword == false)
                this.LogOn();
            else
                this.ChangePassword();
        }
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            // Automatic Logout Reset
            if (this.ParentForm.SystemConfig.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.ParentForm.SystemConfig.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.ParentForm.SystemConfig.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)
        {
            // Automatic Logout Reset
            if (this.ParentForm.SystemConfig.IsPart11 == true)
                this.ParentForm.ChildFormMainDisplay.Part11AutomaticLogoutReset();
        }
       
        private void buttonIntech_Click(object sender, EventArgs e)
        {
            this.textBoxID.Text = "Intech";
            this.textBoxPassword.Text = "I20090810!";
        }
        #endregion

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

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

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

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