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 INT69DB_2A_ImageDll;
using INT69DB_2A.Forms;
using INT69DB_2A.Part11_UserManager;

namespace INT69DB_2A.DialogForms
{
    public partial class DialogFormLogOn : Form
    {
        #region Field
        private FormMain m_ParentForm;
        #endregion

        #region Constructor
        public DialogFormLogOn(FormMain parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

            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.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)
            {
             
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.German)
            {
                
            }
            else
            {
                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));
            }
        }
        private void InitializeControl()
        {
            this.textBoxID.Text = "";
            this.textBoxPassword.Text = "";

            this.Location = new Point(45, 63);

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

        }
        #endregion

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

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

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

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

                return;
            }

            UserManager.UserManager_UserLoginDirect(this.textBoxID.Text, this.textBoxPassword.Text, 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.ExpireAccount = userInfo.expire_period_account;
                this.ParentForm.SystemConfig.CurrentUser.ExpirePassword = userInfo.expire_period_pw;

                DateTime time = DateTime.ParseExact(userInfo.register_date.GetDateTime(), "yyyyMMddHHmmss", null);
                this.ParentForm.SystemConfig.CurrentUser.DateRegister = time;
                time = DateTime.ParseExact(userInfo.login_date.GetDateTime(), "yyyyMMddHHmmss", null);
                this.ParentForm.SystemConfig.CurrentUser.DateLogin = time;
                time = DateTime.ParseExact(userInfo.expire_account_date.GetDateTime(), "yyyyMMddHHmmss", null);
                this.ParentForm.SystemConfig.CurrentUser.DateExpireRegister = time;
                time = DateTime.ParseExact(userInfo.expire_register_date.GetDateTime(), "yyyyMMddHHmmss", null);
                this.ParentForm.SystemConfig.CurrentUser.DateExpireLogin = time;

                this.ParentForm.SystemConfig.CurrentUser.IsLockAccount = userInfo.flock_status_account == 0 ? false : true;
                this.ParentForm.SystemConfig.CurrentUser.IsLockPassword = userInfo.flock_status_password == 0 ? false : true;

                this.ParentForm.SystemConfig.CurrentUser.IsAdmin = userInfo.fadmin == 0 ? false : true;

                this.ParentForm.SystemConfig.CurrentUser.ActiveLevel = userInfo.active_level;
                #endregion

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                DialogFormMessage myMsg = new DialogFormMessage(null, userInfo.status, this.ParentForm.SystemConfig.Language);
                myMsg.ShowDialog();
            }
        }
        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();
        }
        #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!";
        }
    }
}