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

using INT_PT002.Forms;
using INT_PT002.DialogForms;
using INT_PT002.Part11_UserManager;
using INT_PT002.DataStore;

namespace INT_PT002.Controls
{
    public partial class ControlMenuUserUserEditor : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;

        private UserItem SelectedUserItem;
        #endregion

        #region Constructor
        public ControlMenuUserUserEditor(FormMenu parent)
        {
            InitializeComponent();

            this.ParentForm = parent;
            this.Initialize();
        }
        #endregion

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

        #region Method
        private void Initialize()
        {
            this.smartGroupBox1.Text = "User > UserEditor";
        }

        private void SetEnableID(bool value)
        { 
        }
        private void SetEnablePassword(bool value)
        { 
        
        }
        private void KeyboardClose()
        {
            this.smartKeyboard.Hide();

            this.buttonRegister.Enabled = true;
            this.buttonDelete.Enabled = true;
            this.groupBoxAccessRight.Enabled = true;
            this.listBoxUserList.Enabled = true;
            this.textBoxPassword.Enabled = true;
            this.labelPasswordBackGround.BackGroundColor = Color.White;
            this.buttonPasswordEdit.Enabled = true;
            this.textBoxID.Enabled = true;
            this.labelIDBackGround.BackGroundColor = Color.White;
            this.buttonIDEdit.Enabled = true;

            if(this.buttonSave.Visible == false)
                this.buttonSave.Visible = true;
        }
        private void ChangeUserControlEnable(bool enable)
        {
            this.listBoxUserList.Enabled = enable;
            this.smartGroupBox1.Enabled = enable;
        }
        private void UserNew()
        {
            this.listBoxUserList.SelectedIndexChanged -= new EventHandler(this.listBoxUserList_SelectedIndexChanged);
            this.listBoxUserList.SelectItemIndex = -1;
            this.listBoxUserList.SelectedIndexChanged += new EventHandler(this.listBoxUserList_SelectedIndexChanged);

            this.UpdateInitializeUserDisplay(this.ParentForm.ParentForm.SystemConfig);

            this.buttonDelete.Enabled = false;
            this.buttonSave.Visible = false;

            this.textBoxID.Enabled = true;
            this.labelIDBackGround.BackGroundColor = Color.White;
            this.textBoxPassword.Enabled = true;
            this.labelPasswordBackGround.BackGroundColor = Color.White;
            this.labelExpiryDateOfAccount.Enabled = true;
            this.labelExpiryDateOfPassword.Enabled = true;
            this.labelAutomaticLogoutTime.Enabled = true;
        }
        private void SaveUserDll()
        {
            string detail = "";
            string code = "", message1 = "", message2 = "";
            bool isNewReg = false;

            #region listbox 미선택 시 신규 유저 등록, 선택 시 기존 유저 정보 수정
            if (this.listBoxUserList.SelectItemIndex == -1)
                isNewReg = true;
            else
                isNewReg = false;

            if (isNewReg == true)
            {
                switch (this.ParentForm.ParentForm.SystemConfig.LANGUAGE)
                {
                    case Define.E_LanguageID.Korean:
                        code = "유저 설정";
                        message1 = "신규 사용자 추가 하시겠습니까?";
                        message2 = "";
                        break;
                    case Define.E_LanguageID.English:
                        code = "User Editor";
                        message1 = "Would you like to register as a user?";
                        message2 = "";
                        break;
                    default:
                        break;
                }
            }
            else
            {
                switch (this.ParentForm.ParentForm.SystemConfig.LANGUAGE)
                {
                    case Define.E_LanguageID.Korean:
                        code = "유저 설정";
                        message1 = "선택된 사용자의 정보를 수정 하시겠습니까?";
                        message2 = "";
                        break;
                    case Define.E_LanguageID.English:
                        code = "User Editor";
                        message1 = "Are you sure you want to edit the";
                        message2 = "selected user's information?";
                        break;
                    default:
                        break;
                }
            }
            #endregion
                                   
            DialogFormYesNo dlg = new DialogFormYesNo(Define.E_MessageBoxIcon.Question, code, message1, message2);
            if (dlg.ShowDialog() == DialogResult.Yes)
            {
                UserItem item = new UserItem();
                DateTime time = DateTime.Now;
                UserManager.MenuID_t menuId = new UserManager.MenuID_t();

                #region ID, Password 검사
                // Check ID
                if (isNewReg == true)
                {
                    if (this.textBoxID.Text.Length < 6)
                    {
                        // ID : 6~20자 입력하세요
                        DialogFormMessage msg = new DialogFormMessage(null, 3, this.ParentForm.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.ParentForm.SystemConfig.LANGUAGE);
                    msg.ShowDialog();

                    return;
                }

                if (this.labelAccessRight.Text == "")
                {
                    DialogFormMessage msg = new DialogFormMessage(null, 16, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                    msg.ShowDialog();

                    return;
                }

                if (this.labelExpiryDateOfAccount.Text == "000")
                {
                    DialogFormMessage msg = new DialogFormMessage(null, 16, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                    msg.ShowDialog();

                    return;
                }

                if (this.labelExpiryDateOfPassword.Text == "00")
                {
                    DialogFormMessage msg = new DialogFormMessage(null, 16, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                    msg.ShowDialog();

                    return;
                }
                #endregion

                item.ID = this.textBoxID.Text;
                item.SetPassword(this.textBoxPassword.Text);
                item.ExpireAccount = int.Parse(this.labelExpiryDateOfAccount.Text);
                item.ExpirePassword = int.Parse(this.labelExpiryDateOfPassword.Text);

                //item.IsAdmin = this.cbAdministrator.Checked;

                menuId.fMenu = new bool[UserManager.USER_MENU_ID_MAX];

                menuId.fMenu[0] = this.checkBoxMainProductChange.Checked;
                menuId.fMenu[1] = this.checkBoxMainClear.Checked;
                menuId.fMenu[2] = this.checkBoxMainSubMenu.Checked;

                menuId.fMenu[3] = this.checkBoxMenuRecipe.Checked;
                menuId.fMenu[4] = this.checkBoxMenuUserEditor.Checked;
                menuId.fMenu[5] = this.checkBoxMenuUserGroupEditor.Checked;
                menuId.fMenu[6] = this.checkBoxMenuIOTest.Checked;
                menuId.fMenu[7] = this.checkBoxMenuHistoryLog.Checked;
                menuId.fMenu[8] = this.checkBoxMenuCheckLog.Checked;
                menuId.fMenu[9] = this.checkBoxMenuAlarmList.Checked;
                menuId.fMenu[10] = this.checkBoxMenuInformation.Checked;
                menuId.fMenu[11] = this.checkBoxMenuEquipment.Checked;

                for (int i = 12; i < UserManager.USER_MENU_ID_MAX - 11; i++)
                    menuId.fMenu[i] = false;
                               
                if (this.buttonLevel1.ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                    menuId.fMenu[25] = true;
                if (this.buttonLevel2.ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                    menuId.fMenu[26] = true; 
                if (this.buttonLevel3.ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                    menuId.fMenu[27] = true;

                if (isNewReg == true)
                {
                    #region 신규 등록
                    UserManager.UserManager_UserNew(item.ID, item.Password, item.IsAdmin == false ? 0 : 1, item.ExpirePassword, item.ExpireAccount, menuId);

                    // Part 11
                    detail = string.Format("Add : {0}", item.ID);
                    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingOperation.UserEditor, detail);
                    #endregion
                }
                else
                {
                    #region 유저 수정
                    UserManager.UserManager_UserModify(item.ID, item.Password, item.IsAdmin == false ? 0 : 1, item.ExpirePassword, item.ExpireAccount, menuId);

                    // Part 11
                    detail = string.Format("Modify : {0}", item.ID);
                    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingOperation.UserEditor, detail);
                    #endregion
                }

                #region 자동 로그아웃 타임
                //this.ParentForm.ParentForm.SystemConfig.AUTOMATIC_LOGOUT = int.Parse(this.labelAutomaticLogoutTime.Text);

                //if (this.ParentForm.ParentForm.SystemConfig.AUTOMATIC_LOGOUT == 1)
                //    this.ParentForm.ParentForm.FlagAutomaticLogoutWarningTime = 30;
                //else
                //    this.ParentForm.ParentForm.FlagAutomaticLogoutWarningTime = 60;

                //UserManager.UserManager_AutoLogoutSetTimeout(this.ParentForm.ParentForm.SystemConfig.AUTOMATIC_LOGOUT, this.ParentForm.ParentForm.FlagAutomaticLogoutWarningTime);
                #endregion

                this.ParentForm.ParentForm.SaveSystemConfigurationFile(this.ParentForm.ParentForm.SystemConfig);

                this.buttonSave.Visible = false;
            }
        }
        private void CheckBoxCheckedAsLevel(Define.E_UserStatus level)
        {
            switch (level)
            {
                case Define.E_UserStatus.Level1:
                    this.buttonLevel2.ButtonUp();
                    this.buttonLevel3.ButtonUp();
                    this.buttonLevel1.ButtonDown();

                    this.labelAccessRight.Text = this.ParentForm.ParentForm.SystemConfig.LEVEL1_NAME;

                    this.checkBoxMainProductChange.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMainDisplayProductChange;
                    this.checkBoxMainClear.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMainDisplayClear;
                    this.checkBoxMainSubMenu.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMainDisplaySubMenu;

                    this.checkBoxMenuRecipe.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMenuRecipe;
                    this.checkBoxMenuUserEditor.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMenuUserSetting;
                    this.checkBoxMenuUserGroupEditor.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMenuUserGroupEditor;
                    this.checkBoxMenuIOTest.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMenuIOTest;
                    this.checkBoxMenuHistoryLog.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMenuHistoryLog;
                    this.checkBoxMenuCheckLog.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMenuInspectionLog;
                    this.checkBoxMenuAlarmList.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMenuAlarmLog;
                    this.checkBoxMenuInformation.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMenuInformation;
                    this.checkBoxMenuEquipment.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMenuStatus;
                    break;
                case Define.E_UserStatus.Level2:
                    this.buttonLevel1.ButtonUp();
                    this.buttonLevel3.ButtonUp();
                    this.buttonLevel2.ButtonDown();

                    this.labelAccessRight.Text = this.ParentForm.ParentForm.SystemConfig.LEVEL2_NAME;

                    this.checkBoxMainProductChange.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMainDisplayProductChange;
                    this.checkBoxMainClear.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMainDisplayClear;
                    this.checkBoxMainSubMenu.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMainDisplaySubMenu;

                    this.checkBoxMenuRecipe.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMenuRecipe;
                    this.checkBoxMenuUserEditor.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMenuUserSetting;
                    this.checkBoxMenuUserGroupEditor.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMenuUserGroupEditor;
                    this.checkBoxMenuIOTest.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMenuIOTest;
                    this.checkBoxMenuHistoryLog.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMenuHistoryLog;
                    this.checkBoxMenuCheckLog.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMenuInspectionLog;
                    this.checkBoxMenuAlarmList.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMenuAlarmLog;
                    this.checkBoxMenuInformation.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMenuInformation;
                    this.checkBoxMenuEquipment.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMenuStatus;
                    break;
                case Define.E_UserStatus.Level3:
                    this.buttonLevel1.ButtonUp();
                    this.buttonLevel2.ButtonUp();
                    this.buttonLevel3.ButtonDown();

                    this.labelAccessRight.Text = this.ParentForm.ParentForm.SystemConfig.LEVEL3_NAME;

                    this.checkBoxMainProductChange.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMainDisplayProductChange;
                    this.checkBoxMainClear.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMainDisplayClear;
                    this.checkBoxMainSubMenu.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMainDisplaySubMenu;

                    this.checkBoxMenuRecipe.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuRecipe;
                    this.checkBoxMenuUserEditor.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuUserSetting;
                    this.checkBoxMenuUserGroupEditor.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuUserGroupEditor;
                    this.checkBoxMenuIOTest.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuIOTest;
                    this.checkBoxMenuHistoryLog.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuHistoryLog;
                    this.checkBoxMenuCheckLog.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuInspectionLog;
                    this.checkBoxMenuAlarmList.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuAlarmLog;
                    this.checkBoxMenuInformation.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuInformation;
                    this.checkBoxMenuEquipment.Checked = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuStatus;
                    break;
                default:
                    break;
            }
        }

        private void UpdateInitializeUserDisplay(SystemConfiguration system)
        {
            this.SelectedUserItem = new UserItem();

            this.textBoxID.Text = "";
            this.textBoxPassword.Text = "";
            this.labelExpiryDateOfAccount.Text = "180";
            this.labelExpiryDateOfPassword.Text = "90";
            this.labelExpiryDateOfAccount2.Text = "yyyy.mm.DD";
            this.labelExpiryDateOfPassword2.Text = "yyyy.mm.DD";
            this.labelAutomaticLogoutTime.Text = "0";
            this.labelAccessRight.Text = "";

            this.buttonLevel1.ButtonText = system.LEVEL1_NAME;
            this.buttonLevel2.ButtonText = system.LEVEL2_NAME;
            this.buttonLevel3.ButtonText = system.LEVEL3_NAME;

            this.checkBoxMainProductChange.Checked = false;
            this.checkBoxMainClear.Checked = false;
            this.checkBoxMainSubMenu.Checked = false;

            this.checkBoxMenuRecipe.Checked = false;
            this.checkBoxMenuUserEditor.Checked = false;
            this.checkBoxMenuUserGroupEditor.Checked = false;
            this.checkBoxMenuIOTest.Checked = false;
            this.checkBoxMenuHistoryLog.Checked = false;
            this.checkBoxMenuCheckLog.Checked = false;
            this.checkBoxMenuAlarmList.Checked = false;
            this.checkBoxMenuInformation.Checked = false;
            this.checkBoxMenuEquipment.Checked = false;

            this.buttonLevel1.ButtonUp();
            this.buttonLevel2.ButtonUp();
            this.buttonLevel3.ButtonUp();

            this.buttonDelete.Enabled = false;
        }
        public void UpdateLevelNameDisplay(SystemConfiguration system)
        {
            this.buttonLevel1.ButtonText = system.LEVEL1_NAME;
            this.buttonLevel2.ButtonText = system.LEVEL2_NAME;
            this.buttonLevel3.ButtonText = system.LEVEL3_NAME;
        }
        private void UpdateCurrentUserItem(UserManager.UserMgr_user_list_t item)
        {
            // CurrentUserItem SET
            this.SelectedUserItem.ID = item.user_id;
            this.SelectedUserItem.Password = item.user_pw;

            this.SelectedUserItem.ExpireAccount = item.expire_period_account;
            this.SelectedUserItem.ExpirePassword = item.expire_period_pw;

            DateTime time = DateTime.ParseExact(item.register_date.GetDateTime(), "yyyyMMddHHmmss", null);
            this.SelectedUserItem.DateRegister = time;
            time = DateTime.ParseExact(item.login_date.GetDateTime(), "yyyyMMddHHmmss", null);
            this.SelectedUserItem.DateLogin = time;
            time = DateTime.ParseExact(item.expire_account_date.GetDateTime(), "yyyyMMddHHmmss", null);
            this.SelectedUserItem.DateExpireRegister = time;
            time = DateTime.ParseExact(item.expire_register_date.GetDateTime(), "yyyyMMddHHmmss", null);
            this.SelectedUserItem.DateExpireLogin = time;

            this.SelectedUserItem.IsLockAccount = item.flock_status_account == 0 ? false : true;
            this.SelectedUserItem.IsLockPassword = item.flock_status_password == 0 ? false : true;

            this.SelectedUserItem.IsAdmin = item.fadmin == 0 ? false : true;
            this.SelectedUserItem.IsMainRecipeChange = item.menuID.fMenu[0];
            this.SelectedUserItem.IsMainClear = item.menuID.fMenu[1];
            this.SelectedUserItem.IsMainSubMenu = item.menuID.fMenu[2];

            this.SelectedUserItem.IsMenuProduct = item.menuID.fMenu[3];
            this.SelectedUserItem.IsMenuUserSetting = item.menuID.fMenu[4];
            this.SelectedUserItem.IsMenuUserGroupEditor = item.menuID.fMenu[5];
            this.SelectedUserItem.IsMenuIOTest = item.menuID.fMenu[6];
            this.SelectedUserItem.IsMenuEquipmentLog = item.menuID.fMenu[7];
            this.SelectedUserItem.IsMenuCheckLog = item.menuID.fMenu[8];
            this.SelectedUserItem.IsMenuAlarmList = item.menuID.fMenu[9];
            this.SelectedUserItem.IsMenuInformation = item.menuID.fMenu[10];
            this.SelectedUserItem.IsMenuEquipmentTest = item.menuID.fMenu[11];

            if (item.menuID.fMenu[25] == true)
                this.SelectedUserItem.Status = Define.E_UserStatus.Level1;
            else if(item.menuID.fMenu[26] == true)
                this.SelectedUserItem.Status = Define.E_UserStatus.Level2;
            else if(item.menuID.fMenu[27] == true)
                this.SelectedUserItem.Status = Define.E_UserStatus.Level3;
        }
        private void UpdateCurrentUserItem(UserManager.UserMgr_user_info_t item)
        {
            // CurrentUserItem SET
            this.SelectedUserItem.ID = item.user_id;
            this.SelectedUserItem.Password = item.user_pw;

            this.SelectedUserItem.ExpireAccount = item.expire_period_account;
            this.SelectedUserItem.ExpirePassword = item.expire_period_pw;

            DateTime time = DateTime.ParseExact(item.register_date.GetDateTime(), "yyyyMMddHHmmss", null);
            this.SelectedUserItem.DateRegister = time;
            time = DateTime.ParseExact(item.login_date.GetDateTime(), "yyyyMMddHHmmss", null);
            this.SelectedUserItem.DateLogin = time;
            time = DateTime.ParseExact(item.expire_account_date.GetDateTime(), "yyyyMMddHHmmss", null);
            this.SelectedUserItem.DateExpireRegister = time;
            time = DateTime.ParseExact(item.expire_register_date.GetDateTime(), "yyyyMMddHHmmss", null);
            this.SelectedUserItem.DateExpireLogin = time;

            this.SelectedUserItem.IsLockAccount = item.flock_status_account == 0 ? false : true;
            this.SelectedUserItem.IsLockPassword = item.flock_status_password == 0 ? false : true;

            this.SelectedUserItem.IsAdmin = item.fadmin == 0 ? false : true;
            this.SelectedUserItem.IsMainRecipeChange = item.menuID.fMenu[0];
            this.SelectedUserItem.IsMainClear = item.menuID.fMenu[1];
            this.SelectedUserItem.IsMainSubMenu = item.menuID.fMenu[2];

            this.SelectedUserItem.IsMenuProduct = item.menuID.fMenu[3];
            this.SelectedUserItem.IsMenuUserSetting = item.menuID.fMenu[4];
            this.SelectedUserItem.IsMenuUserGroupEditor = item.menuID.fMenu[5];
            this.SelectedUserItem.IsMenuIOTest = item.menuID.fMenu[6];
            this.SelectedUserItem.IsMenuEquipmentLog = item.menuID.fMenu[7];
            this.SelectedUserItem.IsMenuCheckLog = item.menuID.fMenu[8];
            this.SelectedUserItem.IsMenuAlarmList = item.menuID.fMenu[9];
            this.SelectedUserItem.IsMenuInformation = item.menuID.fMenu[10];
            this.SelectedUserItem.IsMenuEquipmentTest = item.menuID.fMenu[11];

            if (item.menuID.fMenu[25] == true)
                this.SelectedUserItem.Status = Define.E_UserStatus.Level1;
            else if (item.menuID.fMenu[26] == true)
                this.SelectedUserItem.Status = Define.E_UserStatus.Level2;
            else if (item.menuID.fMenu[27] == true)
                this.SelectedUserItem.Status = Define.E_UserStatus.Level3;
        }
        private void UpdateSelectUserDisplay(UserItem item)
        {
            // 화면 Enable 처리 추가할것
            this.ChangeUserControlEnable(true);
            this.textBoxID.Enabled = false;
            this.textBoxID.BackColor = Color.Silver;
            this.labelIDBackGround.BackGroundColor = Color.Silver;

            this.textBoxID.Text = item.ID;
            this.textBoxPassword.Text = item.Password;
            this.labelExpiryDateOfAccount.Text = item.ExpireAccount.ToString();
            this.labelExpiryDateOfPassword.Text = item.ExpirePassword.ToString();
            this.labelExpiryDateOfAccount2.Text = string.Format("{0:yy/MM/dd} ~ {1:yy/MM/dd}", item.DateRegister, item.DateExpireRegister);
            this.labelExpiryDateOfPassword2.Text = string.Format("{0:yy/MM/dd} ~ {1:yy/MM/dd}", item.DateLogin, item.DateExpireLogin);

            this.CheckBoxCheckedAsLevel(item.Status);

            //this.cbAdministrator.Visible = true;
            //this.cbAdministrator.Checked = item.IsAdmin;
        }
        private void UpdateUserListBoxDisplay(List<string> items)
        {
            this.listBoxUserList.ClearAll();
            for (int i = 1; i < items.Count; i++)
                this.listBoxUserList.AddItem(items[i]);
        }
        //private void UpdateDefaultSetAccessrightDisplay(UserGroupItem item)
        //{
        //    this.cbMainProductNo.Checked = item.IsMainDisplayProductNo;
        //    this.cbMainWeightSet.Checked = item.IsMainDisplayWeightSetting;
        //    this.cbMainClear.Checked = item.IsMainDisplayClear;
        //    this.cbMainSubMenu.Checked = item.IsMainDisplaySubMenu;

        //    this.cbMenuCalibration.Checked = item.IsMenuCalibration;
        //    this.cbMenuConfiguration.Checked = item.IsMenuConfiguration;
        //    this.cbMenuDataBackup.Checked = item.IsMenuDataBackup;
        //    this.cbMenuDataStatistics.Checked = item.IsMenuDataStatistics;
        //    this.cbMenuEquipmentSet.Checked = item.IsMenuEquipmentSetting;
        //    this.cbMenuFactoryReset.Checked = item.IsMenuFactoryReset;
        //    this.cbMenuInformation.Checked = item.IsMenuInformation;
        //    this.cbMenuIOTest.Checked = item.IsMenuIOTest;
        //    this.cbMenuMotorSet.Checked = item.IsMenuMotorSetting;
        //    this.cbMenuSystemSet.Checked = item.IsMenuSystemSetting;
        //    this.cbMenuTimeSet.Checked = item.IsMenuTimeSetting;
        //    this.cbMenuUpdate.Checked = item.IsMenuUpdate;
        //    this.cbMenuUserEditor.Checked = item.IsMenuUserSetting;
        //}

        public void CallBackGetUserData(UserManager.UserMgr_user_list_t user)
        {
            this.UpdateCurrentUserItem(user);

            this.UI_Invoke(delegate
            {
                this.UpdateSelectUserDisplay(this.SelectedUserItem);
            });
        }
        public void CallBackUserListNewData(UserManager.UserMgr_user_info_t user)
        {
            string code = "", message1 = "", message2 = "";

            switch (this.ParentForm.ParentForm.SystemConfig.LANGUAGE)
            {
                case Define.E_LanguageID.Korean:
                    code = "유저설정";
                    message1 = "사용자 추가 완료!";
                    message2 = "";
                    break;
                case Define.E_LanguageID.English:
                    code = "user settings";
                    message1 = "User addition complete!";
                    message2 = "";
                    break;
                default:
                    break;
            }

            if (user.status == (int)UserManager.E_user_mgr_status.USER_MGR_STATUS_OK)
            {
                this.UI_Invoke(delegate
                {
                    this.UpdateCurrentUserItem(user);
                    this.UpdateSelectUserDisplay(this.SelectedUserItem);

                    this.ParentForm.ParentForm.ListDllUserName.Add(user.user_id);
                    this.UpdateUserListBoxDisplay(this.ParentForm.ParentForm.ListDllUserName);
                    this.listBoxUserList.SelectItemIndex = this.listBoxUserList.ItemCount - 1;

                    this.textBoxID.Enabled = false;
                    this.textBoxID.BackColor = Color.Silver;
                    this.labelIDBackGround.BackGroundColor = Color.Silver;
                    this.textBoxPassword.Enabled = false;
                    this.textBoxPassword.BackColor = Color.Silver;
                    this.labelPasswordBackGround.BackGroundColor = Color.Silver;
                    this.labelExpiryDateOfAccount.Enabled = false;
                    this.labelExpiryDateOfAccount.BackColor = Color.Silver;
                    this.labelExpiryDateOfPassword.Enabled = false;
                    this.labelExpiryDateOfPassword.BackColor = Color.Silver;
                    this.labelAutomaticLogoutTime.Enabled = false;
                    this.labelAutomaticLogoutTime.BackColor = Color.Silver;
                    this.buttonDelete.Enabled = true;

                    DialogFormMessage dlg = new DialogFormMessage(Define.E_MessageBoxIcon.Asterisk, code, message1, message2, 0);
                    dlg.ShowDialog();
                });
            }
            else
            {
                DialogFormMessage msg = new DialogFormMessage(null, user.status, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                msg.ShowDialog();
            }
        }
        public void CallBackUserModifyUserData(UserManager.UserMgr_user_modify_t user)
        {
            string code = "", message1 = "", message2 = "";

            switch (this.ParentForm.ParentForm.SystemConfig.LANGUAGE)
            {
                case Define.E_LanguageID.Korean:
                    code = "유저설정";
                    message1 = "사용자 정보 수정 완료!";
                    message2 = "";
                    break;
                case Define.E_LanguageID.English:
                    code = "user settings";
                    message1 = "User information modified!";
                    message2 = "";
                    break;
                default:
                    break;
            }

            if (user.status == (int)UserManager.E_user_mgr_status.USER_MGR_STATUS_OK)
            {
                this.UI_Invoke(delegate
                {
                    // 메시지 박스 교체
                    UserManager.UserManager_GetUserListID(this.listBoxUserList.Items[this.listBoxUserList.SelectItemIndex]);

                    DialogFormMessage dlg = new DialogFormMessage(Define.E_MessageBoxIcon.Asterisk, code, message1, message2, 0);
                    dlg.ShowDialog();
                });
            }
            else
            {
                DialogFormMessage msg = new DialogFormMessage(null, user.status, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                msg.ShowDialog();
            }
        }
        public void CallBackUserListLockDataEvent(UserManager.UserMgr_user_lock_t user)
        {
            if (user.user_id == this.SelectedUserItem.ID)
            {
                this.SelectedUserItem.IsLockAccount = user.flock_status_account == 0 ? false : true;
                this.SelectedUserItem.IsLockPassword = user.flock_status_password == 0 ? false : true;

                UserManager.UserManager_GetUserListID(this.listBoxUserList.Items[this.listBoxUserList.SelectItemIndex]);
            }
        }
        public void CallBackUserListDeleteDataEvent(UserManager.UserMgr_user_del_t user)
        {
            string code = "", message1 = "", message2 = "", detail = "";

            switch (this.ParentForm.ParentForm.SystemConfig.LANGUAGE)
            {
                case Define.E_LanguageID.Korean:
                    code = "유저설정";
                    message1 = string.Format("{0} 사용자 삭제 완료!", user.user_id);
                    message2 = "";
                    break;
                case Define.E_LanguageID.English:
                    code = "user settings";
                    message1 = string.Format("{0} User deletion complete!", user.user_id);
                    message2 = "";
                    break;
                default:
                    break;
            }
            
            if (user.status == 0)
            {
                for (int i = 0; i < this.ParentForm.ParentForm.ListDllUserName.Count; i++)
                {
                    if (this.ParentForm.ParentForm.ListDllUserName[i] == user.user_id)
                    {
                        this.ParentForm.ParentForm.ListDllUserName.RemoveAt(i);
                        break;
                    }
                }

                this.UI_Invoke(delegate
                {
                    this.UpdateUserListBoxDisplay(this.ParentForm.ParentForm.ListDllUserName);

                    this.UserNew();

                    DialogFormMessage dlg = new DialogFormMessage(Define.E_MessageBoxIcon.Asterisk, code, message1, message2, 0);
                    dlg.ShowDialog();
                });

                //// Part 11
                //if (this.ParentForm.ParentForm.SystemConfig.IsPart11 == true)
                //{
                    detail = string.Format("Delete : {0}", user.user_id);
                    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingOperation.UserEditor, detail);
                //}
            }
            else
            {
                DialogFormMessage msg = new DialogFormMessage(null, user.status, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                msg.ShowDialog();
            }
        }
        public void CallBackUserListModifyInfoDataEvent(UserManager.UserMgr_user_info_t user)
        {
            //if (this.ChildControlUserSet.Visible == true)
            //{
            //      this.ChildControlUserSet.CallBackUserListModifyInfoDataEvent(user);
            //}
        }

        private bool UI_Invoke(ThreadStart invoker)
        {
            try
            {
                if (this.InvokeRequired)
                {
                    if (this.IsDisposed)
                        return true;

                    this.Invoke(invoker);
                }
                else
                {
                    invoker();
                }

                return true;

            }
            catch (Exception e)
            {
                return false;
            }
        }

        public void DisplayRefresh()
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplayMode = Define.E_DisplayModeStore.UserEditor;
            this.ParentForm.ParentForm.SetDisplayMode(Define.E_EquipmentMode.Menu);

            this.UpdateInitializeUserDisplay(this.ParentForm.ParentForm.SystemConfig);
            this.UpdateUserListBoxDisplay(this.ParentForm.ParentForm.ListDllUserName);
            this.listBoxUserList.SelectedIndexChanged -= new EventHandler(this.listBoxUserList_SelectedIndexChanged);
            this.listBoxUserList.SelectItemIndex = -1;
            this.listBoxUserList.SelectedIndexChanged += new EventHandler(this.listBoxUserList_SelectedIndexChanged);

            if (this.buttonSave.Visible == true)
                this.buttonSave.Visible = false;
        }
        #endregion

        #region Event Handler 
        private void listBoxUserList_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = this.listBoxUserList.SelectItemIndex;

            if (index != -1)
            {
                if (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentUser.ID == this.listBoxUserList.Items[index] || index == 0)
                    this.buttonDelete.Enabled = false;
                else
                    this.buttonDelete.Enabled = true;

                this.ChangeUserControlEnable(true);

                UserManager.UserManager_GetUserListID(this.listBoxUserList.Items[index]);
            }
        }

        private void textBoxID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                e.Handled = true;
                this.smartKeyboard.Hide();

                this.buttonRegister.Enabled = true;
                this.buttonDelete.Enabled = true;
                this.groupBoxAccessRight.Enabled = true;
                this.listBoxUserList.Enabled = true;
                this.textBoxPassword.Enabled = true;
                this.labelPasswordBackGround.BackGroundColor = Color.White;
                this.buttonPasswordEdit.Enabled = true;
                this.textBoxID.Enabled = true;
                this.labelIDBackGround.BackGroundColor = Color.White;
                this.buttonIDEdit.Enabled = true;

                if (this.buttonSave.Visible == false)
                    this.buttonSave.Visible = true;
            }
            else if (e.KeyChar == 27)
            {
                e.Handled = true;

                this.textBoxID.Text = this.SelectedUserItem.ID;
                this.KeyboardClose();
            }
            else if (e.KeyChar == '<' || e.KeyChar == '>' || e.KeyChar == '|' || e.KeyChar == '"' || e.KeyChar == '?'
                || e.KeyChar == '*' || e.KeyChar == ':' || e.KeyChar == '/' || e.KeyChar == '\\')
            {
                e.Handled = true;
            }
        }
        private void textBoxPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                e.Handled = true;
                this.smartKeyboard.Hide();

                this.buttonRegister.Enabled = true;
                this.buttonDelete.Enabled = true;
                this.groupBoxAccessRight.Enabled = true;
                this.listBoxUserList.Enabled = true;
                this.textBoxPassword.Enabled = true;
                this.labelPasswordBackGround.BackGroundColor = Color.White;
                this.buttonPasswordEdit.Enabled = true;
                this.textBoxID.Enabled = true;
                this.labelIDBackGround.BackGroundColor = Color.White;
                this.buttonIDEdit.Enabled = true;

                if (this.buttonSave.Visible == false)
                    this.buttonSave.Visible = true;
            }
            else if (e.KeyChar == 27)
            {
                e.Handled = true;

                this.textBoxPassword.Text = this.SelectedUserItem.Password;
                this.KeyboardClose();
            }
            else if (e.KeyChar == '<' || e.KeyChar == '>' || e.KeyChar == '|' || e.KeyChar == '"' || e.KeyChar == '?'
                || e.KeyChar == '*' || e.KeyChar == ':' || e.KeyChar == '/' || e.KeyChar == '\\')
            {
                e.Handled = true;
            }
        }
        private void smartKeyboard_OnXKeyClick(object sender, EventArgs e)
        {
            if (this.smartKeyboard.TargetInputObject == this.textBoxPassword)
                this.textBoxPassword.Text = this.SelectedUserItem.Password;
            else
                this.textBoxID.Text = this.SelectedUserItem.ID;

            this.KeyboardClose();
        }

        private void buttonRegister_Click(object sender, EventArgs e)
        {
            this.UserNew();
        }
        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (this.listBoxUserList.SelectItemIndex == -1)
                return;

            // 사용자를 삭제 하시겠습니까?
            DialogFormYesNo dlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig.LANGUAGE, 16);
            if (dlg.ShowDialog() == DialogResult.Yes)
            {
                if (this.listBoxUserList.Items.Count <= 1)
                    return;

                if (this.listBoxUserList.SelectItemIndex <= 0)
                    return;

                string id = this.listBoxUserList.Items[this.listBoxUserList.SelectItemIndex];

                UserManager.UserManager_UserDel(id);
            }
        }
        private void buttonSave_Click(object sender, EventArgs e)
        {
            this.SaveUserDll();
        }

        private void buttonIDEdit_Click(object sender, EventArgs e)
        {
            this.smartKeyboard.KeyboardType = SmartX.SmartKeyboard.KEYBOARDTYPES.NORMAL;
            this.smartKeyboard.Location = new Point(5, 139);
            this.smartKeyboard.Size = new Size(897, 352);
            this.smartKeyboard.Hide();

            this.smartKeyboard.TargetInputObject = this.textBoxID;
            this.smartKeyboard.Show();

            this.textBoxID.Select(this.textBoxID.Text.Length, 0);

            this.buttonRegister.Enabled = false;
            this.buttonDelete.Enabled = false;
            this.groupBoxAccessRight.Enabled = false;
            this.listBoxUserList.Enabled = false;
            this.buttonPasswordEdit.Enabled = false;
            this.textBoxPassword.Enabled = false;
            this.labelPasswordBackGround.BackGroundColor = Color.Silver;
            this.buttonIDEdit.Enabled = false;
        }
        private void buttonPasswordEdit_Click(object sender, EventArgs e)
        {
            this.smartKeyboard.KeyboardType = SmartX.SmartKeyboard.KEYBOARDTYPES.NORMAL;
            this.smartKeyboard.Location = new Point(5, 139);
            this.smartKeyboard.Size = new Size(897, 352);
            this.smartKeyboard.Hide();

            this.smartKeyboard.TargetInputObject = this.textBoxPassword;
            this.smartKeyboard.Show();

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

            this.buttonRegister.Enabled = false;
            this.buttonDelete.Enabled = false;
            this.groupBoxAccessRight.Enabled = false;
            this.listBoxUserList.Enabled = false;
            this.buttonPasswordEdit.Enabled = false;
            this.textBoxID.Enabled = false;
            this.labelIDBackGround.BackGroundColor = Color.Silver;
            this.buttonIDEdit.Enabled = false;
        }
        private void labelExpiryDateOfAccount_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelExpiryDateOfAccount.Text, 3, 0, false);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue < 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                    myMsg.ShowDialog();
                }
                else if (myKeyPad.IntValue > 180)
                {
                    this.labelExpiryDateOfAccount.Text = "180";

                    if (this.buttonSave.Visible == false)
                        this.buttonSave.Visible = true;
                }
                else
                {
                    this.labelExpiryDateOfAccount.Text = myKeyPad.StringValue;

                    if (this.buttonSave.Visible == false)
                        this.buttonSave.Visible = true;
                }
            }
        }
        private void labelExpiryDateOfPassword_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelExpiryDateOfPassword.Text, 3, 0, false);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue < 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                    myMsg.ShowDialog();
                }
                else if (myKeyPad.IntValue > 90)
                {
                    this.labelExpiryDateOfPassword.Text = "90";

                    if (this.buttonSave.Visible == false)
                        this.buttonSave.Visible = true;
                }
                else
                {
                    this.labelExpiryDateOfPassword.Text = myKeyPad.StringValue;

                    if (this.buttonSave.Visible == false)
                        this.buttonSave.Visible = true;
                }
            }
        }
        private void labelAutomaticLogoutTime_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelAutomaticLogoutTime.Text, 2, 0, false);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue < 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                    myMsg.ShowDialog();
                }
                else if (myKeyPad.IntValue > 90)
                {
                    this.labelAutomaticLogoutTime.Text = "90";

                    if (this.buttonSave.Visible == false)
                        this.buttonSave.Visible = true;
                }
                else
                {
                    this.labelAutomaticLogoutTime.Text = myKeyPad.StringValue;

                    if (this.buttonSave.Visible == false)
                        this.buttonSave.Visible = true;
                }
            }
        }

        private void buttonLevel_Click(object sender, EventArgs e)
        {
            SmartX.SmartButton button = sender as SmartX.SmartButton;

            if (button == this.buttonLevel1)
            {
                this.CheckBoxCheckedAsLevel(Define.E_UserStatus.Level1);
            }
            else if (button == this.buttonLevel2)
            {
                this.CheckBoxCheckedAsLevel(Define.E_UserStatus.Level2);
            }
            else if (button == this.buttonLevel3)
            {
                this.CheckBoxCheckedAsLevel(Define.E_UserStatus.Level3);
            }

            if (this.buttonSave.Visible == false)
                this.buttonSave.Visible = true;
        }
        #endregion
    }
}
