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 ITC81DB.Forms;
using ITC81DB.Controls;
using ITC81DB.DialogForms;
using ITC81DB_ImageDll;

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

        //private ControlUserGroupEditor UserControlUserGroupEditor;

        private object LockObject = new object();

        private UserItem m_SelectedUser;
        #endregion

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

            this.ParentForm = parent;

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

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

        public UserItem SelectedUser
        {
            get { return this.m_SelectedUser; }
            private set { this.m_SelectedUser = value; }
        } 
        #endregion

        #region Method
        public void InitializeDesign()
        {
            Class1 images = new Class1();

            if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.English)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korBackgroundUser));

                this.labelTitleID.Text = "ID";
                this.labelTitlePassword.Text = "Password";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackgroundUser));

                this.labelTitleID.Text = "用户名";
                this.labelTitlePassword.Text = "密码";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.rusBackgroundUser));

                this.labelTitleID.Text = "ID";
                this.labelTitlePassword.Text = "Пароль";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerBackgroundUser));

                this.labelTitleID.Text = "ID";
                this.labelTitlePassword.Text = "Passwort";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Spanish)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korBackgroundUser));

                this.labelTitleID.Text = "ID";
                this.labelTitlePassword.Text = "Clave";
            }
            else
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korBackgroundUser));

                this.labelTitleID.Text = "ID";
                this.labelTitlePassword.Text = "Password";
            }
        }
        private void DefaultSetting()
        {
            //this.comboBoxGroup.SelectedIndexChanged -= new EventHandler(this.comboBoxGroup_SelectedIndexChanged);
            //this.comboBoxGroup.Items.Add("None");
            //this.comboBoxGroup.Items.Add("Level1");
            //this.comboBoxGroup.Items.Add("Level2");
            //this.comboBoxGroup.Items.Add("Level3");
            //this.comboBoxGroup.SelectedIndexChanged += new EventHandler(this.comboBoxGroup_SelectedIndexChanged);
            this.smartKeyboard.KeyboardType = SmartX.SmartKeyboard.KEYBOARDTYPES.NORMAL;
            this.smartKeyboard.Show();

            this.SelectedUser = new UserItem();
        }

        private void UpdateUserListDisplay(User user)
        {
            this.listBoxLevel1.Items.Clear();
            this.listBoxLevel1.Items.Add(user.Level1Users[0].ID);
            this.listBoxLevel1.Items.Add(user.Level1Users[1].ID);
            this.listBoxLevel1.Items.Add(user.Level1Users[2].ID);
            this.listBoxLevel1.Items.Add(user.Level1Users[3].ID);
            this.listBoxLevel1.Items.Add(user.Level1Users[4].ID);

            this.listBoxLevel2.Items.Clear();
            this.listBoxLevel2.Items.Add(user.Level2Users[0].ID);
            this.listBoxLevel2.Items.Add(user.Level2Users[1].ID);
            this.listBoxLevel2.Items.Add(user.Level2Users[2].ID);
            this.listBoxLevel2.Items.Add(user.Level2Users[3].ID);
            this.listBoxLevel2.Items.Add(user.Level2Users[4].ID);

            this.listBoxLevel3.Items.Clear();
            this.listBoxLevel3.Items.Add(user.Level3Users[0].ID);
            this.listBoxLevel3.Items.Add(user.Level3Users[1].ID);
            this.listBoxLevel3.Items.Add(user.Level3Users[2].ID);
            this.listBoxLevel3.Items.Add(user.Level3Users[3].ID);
            this.listBoxLevel3.Items.Add(user.Level3Users[4].ID);


            //this.listBoxLevel3.Items.Clear();
            //this.listBoxLevel3.Items.Add(user.Level3Users[0].ID);
            //this.listBoxLevel3.Items.Add(user.Level3Users[1].ID);
            //this.listBoxLevel3.Items.Add(user.Level3Users[2].ID);
            //this.listBoxLevel3.Items.Add(user.Level3Users[3].ID);
            //this.listBoxLevel3.Items.Add(user.Level3Users[4].ID);
        }
        private void UpdateSelectUserDisplay(UserItem item)
        {
            this.textBoxID.Text = item.ID;
            this.textBoxPassword.Text = item.Password;

            this.SelectedUser.ID = item.ID;
            this.SelectedUser.Password = item.Password;
            this.SelectedUser.Group = item.Group;
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = DataStore.DisplayStore.EquipUser;
            this.ParentForm.ParentForm.SetDisplayMode(DataStore.DisplayMode.Menu);
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);

            if (this.ParentForm.CenterEquipUserGroupEditor.Visible == true)
                this.ParentForm.CenterEquipUserGroupEditor.Visible = false;

            this.UpdateUserListDisplay(this.ParentForm.ParentForm.CurrentUser);

            this.listBoxLevel1.SelectItemIndex = -1;
            this.listBoxLevel2.SelectItemIndex = -1;
            this.listBoxLevel3.SelectItemIndex = -1;

            this.textBoxID.Text = this.textBoxPassword.Text = "";
        }
        #endregion

        #region Event Handler
        private void listBoxLevel1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listBoxLevel1.SelectItemIndex != -1)
            {
                lock (this.LockObject)
                {
                    this.UpdateSelectUserDisplay(this.ParentForm.ParentForm.CurrentUser.Level1Users[this.listBoxLevel1.SelectItemIndex]);

                    this.listBoxLevel2.SelectedIndexChanged -= new EventHandler(this.listBoxLevel2_SelectedIndexChanged);
                    this.listBoxLevel3.SelectedIndexChanged -= new EventHandler(this.listBoxLevel3_SelectedIndexChanged);
                    //this.listBoxLevel3.SelectedIndexChanged -= new EventHandler(this.listBoxLevel3_SelectedIndexChanged);

                    this.listBoxLevel2.SelectItemIndex = -1;
                    this.listBoxLevel3.SelectItemIndex = -1;
                    //this.listBoxLevel3.SelectItemIndex = -1;

                    this.listBoxLevel2.SelectedIndexChanged += new EventHandler(this.listBoxLevel2_SelectedIndexChanged);
                    this.listBoxLevel3.SelectedIndexChanged += new EventHandler(this.listBoxLevel3_SelectedIndexChanged);
                    //this.listBoxLevel3.SelectedIndexChanged += new EventHandler(this.listBoxLevel3_SelectedIndexChanged);

                    this.buttonDelete.Enabled = true; 
                }
            }
        }
        private void listBoxLevel2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listBoxLevel2.SelectItemIndex != -1)
            {
                lock (this.LockObject)
                {
                    this.UpdateSelectUserDisplay(this.ParentForm.ParentForm.CurrentUser.Level2Users[this.listBoxLevel2.SelectItemIndex]);

                    this.listBoxLevel1.SelectedIndexChanged -= new EventHandler(this.listBoxLevel1_SelectedIndexChanged);
                    this.listBoxLevel3.SelectedIndexChanged -= new EventHandler(this.listBoxLevel3_SelectedIndexChanged);
                    //this.listBoxLevel3.SelectedIndexChanged -= new EventHandler(this.listBoxLevel3_SelectedIndexChanged);

                    this.listBoxLevel1.SelectItemIndex = -1;
                    this.listBoxLevel3.SelectItemIndex = -1;
                    //this.listBoxLevel3.SelectItemIndex = -1;

                    this.listBoxLevel1.SelectedIndexChanged += new EventHandler(this.listBoxLevel1_SelectedIndexChanged);
                    this.listBoxLevel3.SelectedIndexChanged += new EventHandler(this.listBoxLevel3_SelectedIndexChanged);
                    //this.listBoxLevel3.SelectedIndexChanged += new EventHandler(this.listBoxLevel3_SelectedIndexChanged);

                    this.buttonDelete.Enabled = true; 
                }
            }
        }
        private void listBoxLevel3_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool value = false;

            if (this.listBoxLevel3.SelectItemIndex != -1)
            {
                this.UpdateSelectUserDisplay(this.ParentForm.ParentForm.CurrentUser.Level3Users[this.listBoxLevel3.SelectItemIndex]);

                this.listBoxLevel1.SelectedIndexChanged -= new EventHandler(this.listBoxLevel1_SelectedIndexChanged);
                this.listBoxLevel2.SelectedIndexChanged -= new EventHandler(this.listBoxLevel2_SelectedIndexChanged);

                this.listBoxLevel1.SelectItemIndex = -1;
                this.listBoxLevel2.SelectItemIndex = -1;

                this.listBoxLevel1.SelectedIndexChanged += new EventHandler(this.listBoxLevel1_SelectedIndexChanged);
                this.listBoxLevel2.SelectedIndexChanged += new EventHandler(this.listBoxLevel2_SelectedIndexChanged);

                if (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentUser.ID == this.ParentForm.ParentForm.CurrentUser.Level3Users[this.listBoxLevel3.SelectItemIndex].ID)
                    this.buttonDelete.Enabled = false;
                else
                    this.buttonDelete.Enabled = true;
            }
        }

        private void textBoxID_GotFocus(object sender, EventArgs e)
        {
            this.smartKeyboard.TargetInputObject = this.textBoxID;

            this.textBoxID.Select(this.textBoxID.Text.Length, 0);
        }
        private void textBoxPassword_GotFocus(object sender, EventArgs e)
        {
            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 buttonSave_Click(object sender, EventArgs e)
        {
            bool value = false;

            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig1.Language, 13);
            if (myDlg.ShowDialog() == DialogResult.Yes)
            {
                // ID 검사
                if (this.textBoxID.Text == "-")
                {
                    DialogFormMessage myMsg = new DialogFormMessage(32, this.ParentForm.ParentForm.SystemConfig1.Language);
                    myMsg.ShowDialog();

                    return;
                }
                
                // 패스워드만 변경 할 경우
                if (this.SelectedUser.ID != this.textBoxID.Text)
                {
                    // ID 중복 검사
                    if ((value = this.ParentForm.ParentForm.CurrentUser.SearchID(this.textBoxID.Text)) == true)
                    {
                        DialogFormMessage myMsg = new DialogFormMessage(34, this.ParentForm.ParentForm.SystemConfig1.Language);
                        myMsg.ShowDialog();

                        return;
                    }
                    // ID Space 및 자리수 검사
                    if (this.textBoxID.Text.Trim().Length == 0)
                    {
                        DialogFormMessage myMsg = new DialogFormMessage(32, this.ParentForm.ParentForm.SystemConfig1.Language);
                        myMsg.ShowDialog();

                        return;
                    }
                }
                else
                { 
                }

                // Password 자리수 검사 (4~10자)
                if (this.textBoxPassword.Text.Length < 4 || this.textBoxPassword.Text.Length > 10)
                {
                    DialogFormMessage myMsg = new DialogFormMessage(35, this.ParentForm.ParentForm.SystemConfig1.Language);
                    myMsg.ShowDialog();

                    return;
                }

                if (this.listBoxLevel1.SelectItemIndex != -1)
                {
                    this.ParentForm.ParentForm.CurrentUser.Level1Users[this.listBoxLevel1.SelectItemIndex].ID = this.textBoxID.Text;
                    this.ParentForm.ParentForm.CurrentUser.Level1Users[this.listBoxLevel1.SelectItemIndex].Password = this.textBoxPassword.Text;
                    this.ParentForm.ParentForm.CurrentUser.Level1Users[this.listBoxLevel1.SelectItemIndex].Group = DataStore.UserGroup.Level1Operator;
                    this.ParentForm.ParentForm.SaveUserLevel1File(this.ParentForm.ParentForm.CurrentUser.Level1Users);

                    this.listBoxLevel1.Items[this.listBoxLevel1.SelectItemIndex] = this.textBoxID.Text;
                }
                else if (this.listBoxLevel2.SelectItemIndex != -1)
                {
                    this.ParentForm.ParentForm.CurrentUser.Level2Users[this.listBoxLevel2.SelectItemIndex].ID = this.textBoxID.Text;
                    this.ParentForm.ParentForm.CurrentUser.Level2Users[this.listBoxLevel2.SelectItemIndex].Password = this.textBoxPassword.Text;
                    this.ParentForm.ParentForm.CurrentUser.Level2Users[this.listBoxLevel2.SelectItemIndex].Group = DataStore.UserGroup.Level2Engineer;
                    this.ParentForm.ParentForm.SaveUserLevel2File(this.ParentForm.ParentForm.CurrentUser.Level2Users);

                    this.listBoxLevel2.Items[this.listBoxLevel2.SelectItemIndex] = this.textBoxID.Text;
                }                
                else if (this.listBoxLevel3.SelectItemIndex != -1)
                {
                    this.ParentForm.ParentForm.CurrentUser.Level3Users[this.listBoxLevel3.SelectItemIndex].ID = this.textBoxID.Text;
                    this.ParentForm.ParentForm.CurrentUser.Level3Users[this.listBoxLevel3.SelectItemIndex].Password = this.textBoxPassword.Text;
                    this.ParentForm.ParentForm.CurrentUser.Level3Users[this.listBoxLevel3.SelectItemIndex].Group = DataStore.UserGroup.Level3Manager;
                    this.ParentForm.ParentForm.SaveUserLevel3File(this.ParentForm.ParentForm.CurrentUser.Level3Users);

                    this.listBoxLevel3.Items[this.listBoxLevel3.SelectItemIndex] = this.textBoxID.Text;
                }
                this.UpdateUserListDisplay(this.ParentForm.ParentForm.CurrentUser);
            }
        }
        private void buttonDelete_Click(object sender, EventArgs e)
        {
            UserItem user = new UserItem();

            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig1.Language, 14);
            if (myDlg.ShowDialog() == DialogResult.Yes)
            {
                if (this.listBoxLevel1.SelectItemIndex != -1)
                {
                    this.listBoxLevel1.Items[this.listBoxLevel1.SelectItemIndex] = user.ID;
                    this.ParentForm.ParentForm.CurrentUser.Level1Users[this.listBoxLevel1.SelectItemIndex].ID = user.ID;
                    this.ParentForm.ParentForm.CurrentUser.Level1Users[this.listBoxLevel1.SelectItemIndex].Password = user.Password;
                    this.ParentForm.ParentForm.CurrentUser.Level1Users[this.listBoxLevel1.SelectItemIndex].Group = user.Group;
                    this.ParentForm.ParentForm.SaveUserLevel1File(this.ParentForm.ParentForm.CurrentUser.Level1Users);

                    this.textBoxID.Text = user.ID;
                    this.textBoxPassword.Text = user.Password;
                }
                else if (this.listBoxLevel2.SelectItemIndex != -1)
                {
                    this.listBoxLevel2.Items[this.listBoxLevel2.SelectItemIndex] = user.ID;
                    this.ParentForm.ParentForm.CurrentUser.Level2Users[this.listBoxLevel2.SelectItemIndex].ID = user.ID;
                    this.ParentForm.ParentForm.CurrentUser.Level2Users[this.listBoxLevel2.SelectItemIndex].Password = user.Password;
                    this.ParentForm.ParentForm.CurrentUser.Level2Users[this.listBoxLevel2.SelectItemIndex].Group = user.Group;
                    this.ParentForm.ParentForm.SaveUserLevel2File(this.ParentForm.ParentForm.CurrentUser.Level2Users);

                    this.textBoxID.Text = user.ID;
                    this.textBoxPassword.Text = user.Password;
                }
                else if (this.listBoxLevel3.SelectItemIndex != -1)
                {
                    if (this.listBoxLevel3.SelectItemIndex != 0)
                    {
                        this.listBoxLevel3.Items[this.listBoxLevel3.SelectItemIndex] = user.ID;
                        this.ParentForm.ParentForm.CurrentUser.Level3Users[this.listBoxLevel3.SelectItemIndex].ID = user.ID;
                        this.ParentForm.ParentForm.CurrentUser.Level3Users[this.listBoxLevel3.SelectItemIndex].Password = user.Password;
                        this.ParentForm.ParentForm.CurrentUser.Level3Users[this.listBoxLevel3.SelectItemIndex].Group = user.Group;
                        this.ParentForm.ParentForm.SaveUserLevel3File(this.ParentForm.ParentForm.CurrentUser.Level3Users);

                        this.textBoxID.Text = user.ID;
                        this.textBoxPassword.Text = user.Password;
                    }
                }
                this.UpdateUserListDisplay(this.ParentForm.ParentForm.CurrentUser);
            }
        }

        private void buttonGroupEditor_Click(object sender, EventArgs e)
        {
            this.ParentForm.CenterEquipUserGroupEditor.Visible = true;
            this.ParentForm.CenterEquipUserGroupEditor.BringToFront();
            this.ParentForm.CenterEquipUserGroupEditor.DisplayRefresh(this.ParentForm.ParentForm.CurrentSystemStatus);
        }
        #endregion
    }
}
