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

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

        #region Constructor
        public ControlMenuUserGroupEditor(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 > GroupEditor";
        }

        private void KeyboardClose()
        {
            this.smartKeyboard.Hide();

            this.groupBoxLevel1.FrameLineColor1 = Color.FromArgb(58, 110, 165);
            this.groupBoxLevel2.FrameLineColor1 = Color.FromArgb(58, 110, 165);
            this.groupBoxLevel3.FrameLineColor1 = Color.FromArgb(58, 110, 165);

            this.textBoxLevel1.Visible = false;
            this.textBoxLevel2.Visible = false;
            this.textBoxLevel3.Visible = false;

            this.groupBoxLevel1.Enabled = true;
            this.groupBoxLevel2.Enabled = true;
            this.groupBoxLevel3.Enabled = true;

            this.buttonSave.Enabled = true;
        }

        private void UpdateDisplay(UserGroup group, SystemConfiguration system)
        {
            this.groupBoxLevel1.Text = this.textBoxLevel1.Text = system.LEVEL1_NAME;
            this.groupBoxLevel2.Text = this.textBoxLevel2.Text = system.LEVEL2_NAME;
            this.groupBoxLevel3.Text = this.textBoxLevel3.Text = system.LEVEL3_NAME;

            this.groupBoxLevel1.FrameLineColor1 = Color.FromArgb(58, 110, 165);
            this.groupBoxLevel2.FrameLineColor1 = Color.FromArgb(58, 110, 165);
            this.groupBoxLevel3.FrameLineColor1 = Color.FromArgb(58, 110, 165);
            
            #region Level1
            this.checkBoxL1MainProductChange.Checked = group.Level1.IsMainDisplayProductChange;
            this.checkBoxL1MainClear.Checked = group.Level1.IsMainDisplayClear;
            this.checkBoxL1MainSubMenu.Checked = group.Level1.IsMainDisplaySubMenu;

            this.checkBoxL1MenuProduct.Checked = group.Level1.IsMenuRecipe;
            this.checkBoxL1MenuUserSetting.Checked = group.Level1.IsMenuUserSetting;
            this.checkBoxL1MenuUserGroupEditor.Checked = group.Level1.IsMenuUserGroupEditor;
            this.checkBoxL1MenuIOTest.Checked = group.Level1.IsMenuIOTest;
            this.checkBoxL1MenuHistoryLog.Checked = group.Level1.IsMenuHistoryLog;
            this.checkBoxL1MenuInspectionLog.Checked = group.Level1.IsMenuInspectionLog;
            this.checkBoxL1MenuAlarmLog.Checked = group.Level1.IsMenuAlarmLog;
            this.checkBoxL1MenuInformation.Checked = group.Level1.IsMenuInformation;
            this.checkBoxL1MenuStatus.Checked = group.Level1.IsMenuStatus;
            #endregion

            #region Level2
            this.checkBoxL2MainProductChange.Checked = group.Level2.IsMainDisplayProductChange;
            this.checkBoxL2MainClear.Checked = group.Level2.IsMainDisplayClear;
            this.checkBoxL2MainSubMenu.Checked = group.Level2.IsMainDisplaySubMenu;

            this.checkBoxL2MenuProduct.Checked = group.Level2.IsMenuRecipe;
            this.checkBoxL2MenuUserSetting.Checked = group.Level2.IsMenuUserSetting;
            this.checkBoxL2MenuUserGroupEditor.Checked = group.Level2.IsMenuUserGroupEditor;
            this.checkBoxL2MenuIOTest.Checked = group.Level2.IsMenuIOTest;
            this.checkBoxL2MenuHistoryLog.Checked = group.Level2.IsMenuHistoryLog;
            this.checkBoxL2MenuInspectionLog.Checked = group.Level2.IsMenuInspectionLog;
            this.checkBoxL2MenuAlarmLog.Checked = group.Level2.IsMenuAlarmLog;
            this.checkBoxL2MenuInformation.Checked = group.Level2.IsMenuInformation;
            this.checkBoxL2MenuStatus.Checked = group.Level2.IsMenuStatus;
            #endregion

            #region Level3
            this.checkBoxL3MainProductChange.Checked = group.Level3.IsMainDisplayProductChange;
            this.checkBoxL3MainClear.Checked = group.Level3.IsMainDisplayClear;
            this.checkBoxL3MainSubMenu.Checked = group.Level3.IsMainDisplaySubMenu;

            this.checkBoxL3MenuProduct.Checked = group.Level3.IsMenuRecipe;
            this.checkBoxL3MenuUserSetting.Checked = group.Level3.IsMenuUserSetting;
            this.checkBoxL3MenuUserGroupEditor.Checked = group.Level3.IsMenuUserGroupEditor;
            this.checkBoxL3MenuIOTest.Checked = group.Level3.IsMenuIOTest;
            this.checkBoxL3MenuHistoryLog.Checked = group.Level3.IsMenuHistoryLog;
            this.checkBoxL3MenuInspectionLog.Checked = group.Level3.IsMenuInspectionLog;
            this.checkBoxL3MenuAlarmLog.Checked = group.Level3.IsMenuAlarmLog;
            this.checkBoxL3MenuInformation.Checked = group.Level3.IsMenuInformation;
            this.checkBoxL3MenuStatus.Checked = group.Level3.IsMenuStatus;
            #endregion

            #region Default
            this.checkBoxDefaultMainProductChange.Checked = group.Default.IsMainDisplayProductChange;
            this.checkBoxDefaultMainClear.Checked = group.Default.IsMainDisplayClear;
            this.checkBoxDefaultMainSubMenu.Checked = group.Default.IsMainDisplaySubMenu;

            this.checkBoxDefaultMenuProduct.Checked = group.Default.IsMenuRecipe;
            this.checkBoxDefaultMenuUserSetting.Checked = group.Default.IsMenuUserSetting;
            this.checkBoxDefaultMenuUserGroupEditor.Checked = group.Default.IsMenuUserGroupEditor;
            this.checkBoxDefaultMenuIOTest.Checked = group.Default.IsMenuIOTest;
            this.checkBoxDefaultMenuEquipmentLog.Checked = group.Default.IsMenuHistoryLog;
            this.checkBoxDefaultMenuCheckLog.Checked = group.Default.IsMenuInspectionLog;
            this.checkBoxDefaultMenuAlarmList.Checked = group.Default.IsMenuAlarmLog;
            this.checkBoxDefaultMenuInformation.Checked = group.Default.IsMenuInformation;
            this.checkBoxDefaultMenuStatus.Checked = group.Default.IsMenuStatus;
            #endregion
        }

        public void DisplayRefresh()
        {
            this.ParentForm.ParentForm.SetDisplayMode(Define.E_EquipmentMode.Menu);

            this.UpdateDisplay(this.ParentForm.ParentForm.CurrentUserGroup, this.ParentForm.ParentForm.SystemConfig);

            this.buttonSave.Visible = false;
            this.buttonGroupNameChange.Visible = false;
        }
        #endregion

        #region Event Handler
        private void checkBox_Click(object sender, EventArgs e)
        {
            if (this.buttonSave.Visible == false)
                this.buttonSave.Visible = true;
        }
        private void groupBox_DoubleClick(object sender, EventArgs e)
        {
            SmartX.SmartGroupBox groupBox = sender as SmartX.SmartGroupBox;

            if (groupBox == this.groupBoxLevel1)
            {
                this.groupBoxLevel1.FrameLineColor1 = Color.FromArgb(0, 192, 0);
                this.groupBoxLevel2.FrameLineColor1 = Color.FromArgb(58, 110, 165);
                this.groupBoxLevel3.FrameLineColor1 = Color.FromArgb(58, 110, 165);
            }
            else if (groupBox == this.groupBoxLevel2)
            {
                this.groupBoxLevel1.FrameLineColor1 = Color.FromArgb(58, 110, 165);
                this.groupBoxLevel2.FrameLineColor1 = Color.FromArgb(0, 192, 0);
                this.groupBoxLevel3.FrameLineColor1 = Color.FromArgb(58, 110, 165);
            }
            else if (groupBox == this.groupBoxLevel3)
            {
                this.groupBoxLevel1.FrameLineColor1 = Color.FromArgb(58, 110, 165);
                this.groupBoxLevel2.FrameLineColor1 = Color.FromArgb(58, 110, 165);
                this.groupBoxLevel3.FrameLineColor1 = Color.FromArgb(0, 192, 0);
            }

            this.buttonGroupNameChange.Visible = true;
        }

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

                this.ParentForm.ParentForm.SystemConfig.LEVEL1_NAME = this.textBoxLevel1.Text;
                this.groupBoxLevel1.Text = this.textBoxLevel1.Text;
                this.ParentForm.ParentForm.SaveSystemConfigurationFile(this.ParentForm.ParentForm.SystemConfig);
                this.ParentForm.ParentForm.ChildFormMenu.UpdateLevelNameDisplay(this.ParentForm.ParentForm.SystemConfig);
                this.KeyboardClose();
            }
            else if (e.KeyChar == 27)
            {
                e.Handled = true;

                this.KeyboardClose();
                this.UpdateDisplay(this.ParentForm.ParentForm.CurrentUserGroup, this.ParentForm.ParentForm.SystemConfig);
            }
            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 textBoxLevel2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                e.Handled = true;

                this.ParentForm.ParentForm.SystemConfig.LEVEL2_NAME = this.textBoxLevel2.Text;
                this.groupBoxLevel2.Text = this.textBoxLevel2.Text;
                this.ParentForm.ParentForm.SaveSystemConfigurationFile(this.ParentForm.ParentForm.SystemConfig);
                this.ParentForm.ParentForm.ChildFormMenu.UpdateLevelNameDisplay(this.ParentForm.ParentForm.SystemConfig);
                this.KeyboardClose();
            }
            else if (e.KeyChar == 27)
            {
                e.Handled = true;

                this.KeyboardClose();
                this.UpdateDisplay(this.ParentForm.ParentForm.CurrentUserGroup, this.ParentForm.ParentForm.SystemConfig);
            }
            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 textBoxLevel3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                e.Handled = true;

                this.ParentForm.ParentForm.SystemConfig.LEVEL3_NAME = this.textBoxLevel3.Text;
                this.groupBoxLevel3.Text = this.textBoxLevel3.Text;
                this.ParentForm.ParentForm.SaveSystemConfigurationFile(this.ParentForm.ParentForm.SystemConfig);
                this.ParentForm.ParentForm.ChildFormMenu.UpdateLevelNameDisplay(this.ParentForm.ParentForm.SystemConfig);
                this.KeyboardClose();
            }
            else if (e.KeyChar == 27)
            {
                e.Handled = true;

                this.KeyboardClose();
                this.UpdateDisplay(this.ParentForm.ParentForm.CurrentUserGroup, this.ParentForm.ParentForm.SystemConfig);
            }
            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)
        {
            this.KeyboardClose();
            this.UpdateDisplay(this.ParentForm.ParentForm.CurrentUserGroup, this.ParentForm.ParentForm.SystemConfig);
        }

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

            if (this.groupBoxLevel1.FrameLineColor1 == Color.FromArgb(0, 192, 0))
            {
                this.textBoxLevel1.Visible = true;
                this.smartKeyboard.TargetInputObject = this.textBoxLevel1;
                this.textBoxLevel1.Select(this.textBoxLevel1.Text.Length, 0);
            }
            else if (this.groupBoxLevel2.FrameLineColor1 == Color.FromArgb(0, 192, 0))
            {
                this.textBoxLevel2.Visible = true;
                this.smartKeyboard.TargetInputObject = this.textBoxLevel2;
                this.textBoxLevel2.Select(this.textBoxLevel2.Text.Length, 0);
            }
            else if (this.groupBoxLevel3.FrameLineColor1 == Color.FromArgb(0, 192, 0))
            {
                this.textBoxLevel3.Visible = true;
                this.smartKeyboard.TargetInputObject = this.textBoxLevel3;
                this.textBoxLevel3.Select(this.textBoxLevel3.Text.Length, 0);
            }
            this.smartKeyboard.Show();

            this.buttonSave.Enabled = false;
            this.buttonGroupNameChange.Visible = false;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            #region Level1
            this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMainDisplayProductChange = this.checkBoxL1MainProductChange.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMainDisplayClear = this.checkBoxL1MainClear.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMainDisplaySubMenu = this.checkBoxL1MainSubMenu.Checked;

            this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMenuRecipe = this.checkBoxL1MenuProduct.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMenuUserSetting = this.checkBoxL1MenuUserSetting.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMenuUserGroupEditor = this.checkBoxL1MenuUserGroupEditor.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMenuIOTest = this.checkBoxL1MenuIOTest.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMenuHistoryLog = this.checkBoxL1MenuHistoryLog.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMenuInspectionLog = this.checkBoxL1MenuInspectionLog.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMenuAlarmLog = this.checkBoxL1MenuAlarmLog.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMenuInformation = this.checkBoxL1MenuInformation.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMenuStatus = this.checkBoxL1MenuStatus.Checked;
            #endregion

            #region Level2
            this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMainDisplayProductChange = this.checkBoxL2MainProductChange.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMainDisplayClear = this.checkBoxL2MainClear.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMainDisplaySubMenu = this.checkBoxL2MainSubMenu.Checked;

            this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMenuRecipe = this.checkBoxL2MenuProduct.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMenuUserSetting = this.checkBoxL2MenuUserSetting.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMenuUserGroupEditor = this.checkBoxL2MenuUserGroupEditor.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMenuIOTest = this.checkBoxL2MenuIOTest.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMenuHistoryLog = this.checkBoxL2MenuHistoryLog.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMenuInspectionLog = this.checkBoxL2MenuInspectionLog.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMenuAlarmLog = this.checkBoxL2MenuAlarmLog.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMenuInformation = this.checkBoxL2MenuInformation.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMenuStatus = this.checkBoxL2MenuStatus.Checked;
            #endregion

            #region Level3
            this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMainDisplayProductChange = this.checkBoxL3MainProductChange.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMainDisplayClear = this.checkBoxL3MainClear.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMainDisplaySubMenu = this.checkBoxL3MainSubMenu.Checked;

            this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuRecipe = this.checkBoxL3MenuProduct.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuUserSetting = this.checkBoxL3MenuUserSetting.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuUserGroupEditor = this.checkBoxL3MenuUserGroupEditor.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuIOTest = this.checkBoxL3MenuIOTest.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuHistoryLog = this.checkBoxL3MenuHistoryLog.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuInspectionLog = this.checkBoxL3MenuInspectionLog.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuAlarmLog = this.checkBoxL3MenuAlarmLog.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuInformation = this.checkBoxL3MenuInformation.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuStatus = this.checkBoxL3MenuStatus.Checked;
            #endregion

            #region Default
            this.ParentForm.ParentForm.CurrentUserGroup.Default.IsMainDisplayProductChange = this.checkBoxDefaultMainProductChange.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Default.IsMainDisplayClear = this.checkBoxDefaultMainClear.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Default.IsMainDisplaySubMenu = this.checkBoxDefaultMainSubMenu.Checked;

            this.ParentForm.ParentForm.CurrentUserGroup.Default.IsMenuRecipe = this.checkBoxDefaultMenuProduct.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Default.IsMenuUserSetting = this.checkBoxDefaultMenuUserSetting.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Default.IsMenuUserGroupEditor = this.checkBoxDefaultMenuUserGroupEditor.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Default.IsMenuIOTest = this.checkBoxDefaultMenuIOTest.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Default.IsMenuHistoryLog = this.checkBoxDefaultMenuEquipmentLog.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Default.IsMenuInspectionLog = this.checkBoxDefaultMenuCheckLog.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Default.IsMenuAlarmLog = this.checkBoxDefaultMenuAlarmList.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Default.IsMenuInformation = this.checkBoxDefaultMenuInformation.Checked;
            this.ParentForm.ParentForm.CurrentUserGroup.Default.IsMenuStatus = this.checkBoxDefaultMenuStatus.Checked;
            #endregion

            this.ParentForm.ParentForm.SaveUserGroupFile(this.ParentForm.ParentForm.CurrentUserGroup);

            this.buttonSave.Visible = false;
        }
        #endregion
    }
}
