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 INT_PT002.DataStore;
using INT_PT002.Part11_UserManager;
using INT_PT002.DialogForms;

namespace INT_PT002.Forms
{
    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()
        {

        }
        private void InitializeControl()
        {
            this.textBoxID.Text = "";
            this.textBoxPassword.Text = "";
            this.textBoxID.Focus();

            this.Location = new Point(157, 150);

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

        }

        public void CB_UserLogin(UserItem item)
        {

        }
        #endregion

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

            #region Login1
            //User user = null;

            //user = this.ParentForm.CurrentCollectionUser.FindUser(this.labelID.Text);

            //if (user != null)
            //{
            //    if (user.Password == this.labelPassword.Text)
            //    {
            //        this.ParentForm.SystemConfig.CURRENT_USER.ID = user.ID;
            //        this.ParentForm.SystemConfig.CURRENT_USER.Password = user.Password;
            //        this.ParentForm.SystemConfig.CURRENT_USER.Group = user.Group;

            //        this.DialogResult = DialogResult.OK;
            //        this.Close();
            //    }
            //    else
            //    {
            //        DialogFormMessage myMsg = new DialogFormMessage(33, this.ParentForm.SystemConfig.LANGUAGE);
            //        myMsg.ShowDialog();
            //    }
            //}
            //else
            //{
            //    DialogFormMessage myMsg = new DialogFormMessage(32, this.ParentForm.SystemConfig.LANGUAGE);
            //    myMsg.ShowDialog();
            //}  
            #endregion

            #region Login2
            //UserItem user = null;

            //user = this.ParentForm.FindUser(this.labelID.Text);
            //if (user != null)
            //{
            //    if (user.Password == this.labelPassword.Text)
            //    {

            //        if (user.ID == "Intech")
            //            this.ParentForm.SystemConfig.CURRENT_USER.Status = Define.E_UserStatus.Developer;
            //        else if (user.ID == "Admin00")
            //            this.ParentForm.SystemConfig.CURRENT_USER.Status = Define.E_UserStatus.Admin;
            //        else
            //            this.ParentForm.SystemConfig.CURRENT_USER.Status = Define.E_UserStatus.Guest;

            //        this.ParentForm.SystemConfig.CURRENT_USER.ID = user.ID;
            //        this.ParentForm.SystemConfig.CURRENT_USER.Password = user.Password;
            //        this.ParentForm.SystemConfig.CURRENT_USER.PreviousPassword1 = user.PreviousPassword1;
            //        this.ParentForm.SystemConfig.CURRENT_USER.PreviousPassword2 = user.PreviousPassword2;
            //        this.ParentForm.SystemConfig.CURRENT_USER.PreviousPassword3 = user.PreviousPassword3;
            //        this.ParentForm.SystemConfig.CURRENT_USER.ExpireAccount = user.ExpireAccount;
            //        this.ParentForm.SystemConfig.CURRENT_USER.ExpirePassword = user.ExpirePassword;
            //        this.ParentForm.SystemConfig.CURRENT_USER.DateRegister = user.DateRegister;
            //        this.ParentForm.SystemConfig.CURRENT_USER.DateLogin = user.DateLogin;        // Login 시간 저장하는 코드 추가할것
            //        this.ParentForm.SystemConfig.CURRENT_USER.IsAdmin = user.IsAdmin;
            //        this.ParentForm.SystemConfig.CURRENT_USER.IsMainProductNo = user.IsMainProductNo;
            //        this.ParentForm.SystemConfig.CURRENT_USER.IsMainWeightSetting = user.IsMainWeightSetting;
            //        this.ParentForm.SystemConfig.CURRENT_USER.IsMainClear = user.IsMainClear;
            //        this.ParentForm.SystemConfig.CURRENT_USER.IsMainSubMenu = user.IsMainSubMenu;
            //        this.ParentForm.SystemConfig.CURRENT_USER.IsMenuTimeSetting = user.IsMenuTimeSetting;
            //        this.ParentForm.SystemConfig.CURRENT_USER.IsMenuInformation = user.IsMenuInformation;
            //        this.ParentForm.SystemConfig.CURRENT_USER.IsMenuDataBackup = user.IsMenuDataBackup;
            //        this.ParentForm.SystemConfig.CURRENT_USER.IsMenuConfiguration = user.IsMenuConfiguration;
            //        this.ParentForm.SystemConfig.CURRENT_USER.IsMenuCalibration = user.IsMenuCalibration;
            //        this.ParentForm.SystemConfig.CURRENT_USER.IsMenuSystemSetting = user.IsMenuSystemSetting;
            //        this.ParentForm.SystemConfig.CURRENT_USER.IsMenuMotorSetting = user.IsMenuMotorSetting;
            //        this.ParentForm.SystemConfig.CURRENT_USER.IsMenuIOTest = user.IsMenuIOTest;
            //        this.ParentForm.SystemConfig.CURRENT_USER.IsMenuUpdate = user.IsMenuUpdate;
            //        this.ParentForm.SystemConfig.CURRENT_USER.IsMenuFactoryReset = user.IsMenuFactoryReset;
            //        this.ParentForm.SystemConfig.CURRENT_USER.IsMenuUserEditor = user.IsMenuUserEditor;
            //        this.ParentForm.SystemConfig.CURRENT_USER.IsMenuEquipmentSetting = user.IsMenuEquipmentSetting;
            //        this.ParentForm.SystemConfig.CURRENT_USER.IsMenuDataStatistics = user.IsMenuDataStatistics;

            //        this.DialogResult = DialogResult.OK;
            //        this.Close();
            //    }
            //    else
            //    {
            //        // Password 확인하세요
            //        DialogFormMessage myMsg = new DialogFormMessage(33, this.ParentForm.SystemConfig.LANGUAGE);
            //        myMsg.ShowDialog();
            //    }
            //}
            //else
            //{
            //    // ID 확인하세요
            //    DialogFormMessage myMsg = new DialogFormMessage(32, this.ParentForm.SystemConfig.LANGUAGE);
            //    myMsg.ShowDialog();
            //}  
            #endregion

            #region Login3
            UserManager.UserMgr_user_info_t userInfo = new UserManager.UserMgr_user_info_t();

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

                return;
            }
            if (this.textBoxPassword.Text == "")
            {
                // PASSWORD : 4~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.user_id == "Intech")
                    this.ParentForm.CurrentSystemStatus.CurrentUser.Status = Define.E_UserStatus.Developer;
                else if (userInfo.user_id == "Admin00")
                    this.ParentForm.CurrentSystemStatus.CurrentUser.Status = Define.E_UserStatus.Admin;
                else if (userInfo.menuID.fMenu[25] == true)
                    this.ParentForm.CurrentSystemStatus.CurrentUser.Status = Define.E_UserStatus.Level1;
                else if (userInfo.menuID.fMenu[26] == true)
                    this.ParentForm.CurrentSystemStatus.CurrentUser.Status = Define.E_UserStatus.Level2;
                else if (userInfo.menuID.fMenu[27] == true)
                    this.ParentForm.CurrentSystemStatus.CurrentUser.Status = Define.E_UserStatus.Level3;

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

                this.ParentForm.CurrentSystemStatus.CurrentUser.ExpireAccount = userInfo.expire_period_account;
                this.ParentForm.CurrentSystemStatus.CurrentUser.ExpirePassword = userInfo.expire_period_pw;

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

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

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

                this.ParentForm.CurrentSystemStatus.CurrentUser.IsMainRecipeChange = userInfo.menuID.fMenu[0];
                this.ParentForm.CurrentSystemStatus.CurrentUser.IsMainClear = userInfo.menuID.fMenu[1];
                this.ParentForm.CurrentSystemStatus.CurrentUser.IsMainSubMenu = userInfo.menuID.fMenu[2];

                this.ParentForm.CurrentSystemStatus.CurrentUser.IsMenuProduct = userInfo.menuID.fMenu[3];
                this.ParentForm.CurrentSystemStatus.CurrentUser.IsMenuUserSetting = userInfo.menuID.fMenu[4];
                this.ParentForm.CurrentSystemStatus.CurrentUser.IsMenuUserGroupEditor = userInfo.menuID.fMenu[5];
                this.ParentForm.CurrentSystemStatus.CurrentUser.IsMenuIOTest = userInfo.menuID.fMenu[6];
                this.ParentForm.CurrentSystemStatus.CurrentUser.IsMenuEquipmentLog = userInfo.menuID.fMenu[7];
                this.ParentForm.CurrentSystemStatus.CurrentUser.IsMenuCheckLog = userInfo.menuID.fMenu[8];
                this.ParentForm.CurrentSystemStatus.CurrentUser.IsMenuAlarmList = userInfo.menuID.fMenu[9];
                this.ParentForm.CurrentSystemStatus.CurrentUser.IsMenuInformation = userInfo.menuID.fMenu[10];
                this.ParentForm.CurrentSystemStatus.CurrentUser.IsMenuEquipmentTest = userInfo.menuID.fMenu[11];
                #endregion

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                DialogFormMessage myMsg = new DialogFormMessage(null, userInfo.status, this.ParentForm.SystemConfig.LANGUAGE);
                myMsg.ShowDialog();
            }
            #endregion
        }
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            // Automatic Logout Reset
            this.ParentForm.ChildFormMainDisplay.Part11AutomaticLogoutReset();

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

        private void textBoxID_GotFocus(object sender, EventArgs e)
        {
            // Automatic Logout Reset
            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
            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
            this.ParentForm.ChildFormMainDisplay.Part11AutomaticLogoutReset();
        }
        #endregion
    }
}