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 INT63DC_ImageDll;
using INT63DC_2C.DialogForms;

namespace INT63DC_2C.Forms
{
    public partial class FormUserSetting : Form
    {
        #region Field
        private FormMain m_ParentForm;

        private object LockObject;
        private User m_SelectedUser;
        #endregion

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

            this.ParentForm = parent;
        }
        #endregion

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

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

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

            switch (this.ParentForm.SystemConfig.Language)
            {
                case DataStore.LanguageID.Korean:
                    break;
                case DataStore.LanguageID.English:
                    #region English
                    this.labelTitle.Text = "User settings";

                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackUp)); 
                    #endregion
                    break;
                case DataStore.LanguageID.Chinese:
                    #region Chinese
                    this.labelTitle.Text = "用户设置";

                    this.labelTitleLevel1.Text = "级别 1";
                    this.labelTitleLevel2.Text = "级别 2";
                    this.labelTitleLevel3.Text = "级别 3";

                    this.labelTitleID.Text = "用户名";
                    this.labelTitlePassword.Text = "密码";

                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackUp)); 
                    #endregion
                    break;
                case DataStore.LanguageID.Czech:
                    #region Czech
                    this.labelTitle.Text = "uživatelské nastavení";

                    this.labelTitleLevel1.Text = "Úroveň 1";
                    this.labelTitleLevel2.Text = "Úroveň 2";
                    this.labelTitleLevel3.Text = "Úroveň 3";

                    this.labelTitlePassword.Text = "Heslo";

                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackUp)); 
                    #endregion
                    break;
                case DataStore.LanguageID.German:
                    #region German
                    this.labelTitle.Text = "Benutzereinstellungen";

                    this.labelTitleLevel1.Text = "Stufe 1";
                    this.labelTitleLevel2.Text = "Stufe 2";
                    this.labelTitleLevel3.Text = "Stufe 3";

                    this.labelTitlePassword.Text = "Passwort";

                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackUp)); 
                    #endregion
                    break;
                case DataStore.LanguageID.Japanese:
                    #region Japanese
                    this.labelTitle.Text = "ユㅡザㅡの 設定";

                    this.labelTitleLevel1.Text = "Level 1";
                    this.labelTitleLevel2.Text = "Level 2";
                    this.labelTitleLevel3.Text = "Level 3";

                    this.labelTitlePassword.Text = "パスワㅡ";

                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackUp));
                    #endregion
                    break;
                default:
                    break;
            }
        }
        private void DefaultSetting()
        {
            this.LockObject = new object();

            this.smartKeyboard.KeyboardType = SmartX.SmartKeyboard.KEYBOARDTYPES.NORMAL;
            this.smartKeyboard.Show();

            this.SelectedUser = new User();
        }

        private void UpdateUserListDisplay(CollectionUser 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);
        }
        private void UpdateSelectUserDisplay(User 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()
        {
            this.ParentForm.SystemConfig.CurrentForm = DataStore.FormStore.FormUserSetting;
            this.UpdateUserListDisplay(this.ParentForm.CurrentCollectionUser);

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

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

        #region Override Member
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

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

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

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

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

                    this.listBoxLevel2.SelectedIndexChanged += new EventHandler(this.listBoxLevel2_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.CurrentCollectionUser.Level2Users[this.listBoxLevel2.SelectItemIndex]);

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

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

                    this.listBoxLevel1.SelectedIndexChanged += new EventHandler(this.listBoxLevel1_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.CurrentCollectionUser.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.SystemConfig.CurrentUser.ID == this.ParentForm.CurrentCollectionUser.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.SystemConfig.Language, 13);
            if (myDlg.ShowDialog() == DialogResult.Yes)
            {
                // ID 검사
                if (this.textBoxID.Text == "-")
                {
                    DialogFormMessage myMsg = new DialogFormMessage(32, this.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();

                    return;
                }

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

                        return;
                    }
                    // ID Space 및 자리수 검사
                    if (this.textBoxID.Text.Trim().Length == 0)
                    {
                        DialogFormMessage myMsg = new DialogFormMessage(32, this.ParentForm.SystemConfig.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.SystemConfig.Language);
                    myMsg.ShowDialog();

                    return;
                }

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

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

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

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

            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.SystemConfig.Language, 14);
            if (myDlg.ShowDialog() == DialogResult.Yes)
            {
                if (this.listBoxLevel1.SelectItemIndex != -1)
                {
                    this.listBoxLevel1.Items[this.listBoxLevel1.SelectItemIndex] = user.ID;
                    this.ParentForm.CurrentCollectionUser.Level1Users[this.listBoxLevel1.SelectItemIndex].ID = user.ID;
                    this.ParentForm.CurrentCollectionUser.Level1Users[this.listBoxLevel1.SelectItemIndex].Password = user.Password;
                    this.ParentForm.CurrentCollectionUser.Level1Users[this.listBoxLevel1.SelectItemIndex].Group = user.Group;
                    this.ParentForm.SaveUserLevel1File(this.ParentForm.CurrentCollectionUser.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.CurrentCollectionUser.Level2Users[this.listBoxLevel2.SelectItemIndex].ID = user.ID;
                    this.ParentForm.CurrentCollectionUser.Level2Users[this.listBoxLevel2.SelectItemIndex].Password = user.Password;
                    this.ParentForm.CurrentCollectionUser.Level2Users[this.listBoxLevel2.SelectItemIndex].Group = user.Group;
                    this.ParentForm.SaveUserLevel2File(this.ParentForm.CurrentCollectionUser.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.CurrentCollectionUser.Level3Users[this.listBoxLevel3.SelectItemIndex].ID = user.ID;
                        this.ParentForm.CurrentCollectionUser.Level3Users[this.listBoxLevel3.SelectItemIndex].Password = user.Password;
                        this.ParentForm.CurrentCollectionUser.Level3Users[this.listBoxLevel3.SelectItemIndex].Group = user.Group;
                        this.ParentForm.SaveUserLevel3File(this.ParentForm.CurrentCollectionUser.Level3Users);

                        this.textBoxID.Text = user.ID;
                        this.textBoxPassword.Text = user.Password;
                    }
                }
                this.UpdateUserListDisplay(this.ParentForm.CurrentCollectionUser);
            }
        }
        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.ParentForm.ChildFormMenu.DisplayRefresh();
            ((FormMain)(Owner)).smartForm.Show((int)DataStore.FormStore.FormMenu);
        }
        private void buttonGroupEditor_Click(object sender, EventArgs e)
        {
            this.ParentForm.ChildFormUserGroupEditor.DisplayRefresh();
            ((FormMain)(Owner)).smartForm.Show((int)DataStore.FormStore.FormUserGroupEditor);
        }
        #endregion
    }
}