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

using SmartX;
using INT_LKD_2.Forms;
using INT_LKD_2.DataStore;
using INT_LKD_2.DialogForms;

namespace INT_LKD_2.Controls
{
    public partial class ControlMenuSystemSetting : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;
        private string MessageBoxRange;

        private Collection<SmartLabel> CollLabel;
        #endregion

        #region Constructor
        public ControlMenuSystemSetting(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 = "System > Setting";
            this.MessageBoxRange = "";

            this.CollLabel = new Collection<SmartLabel>();
            this.CollLabel.Add(this.labelLcdDataPeriod);
            this.CollLabel.Add(this.labelChattering);
            this.CollLabel.Add(this.labelCutWait);
            this.CollLabel.Add(this.labelBuzzer);
            this.CollLabel.Add(this.labelSbDiffFilter);
        }

        private void UpdateDisplauUserControls(bool enable)
        {
            for (int i = 0; i < this.CollLabel.Count; i++)
                this.CollLabel[i].Enabled = enable;
        }
        private void UpdateDisplayUser(UserItem user)
        {
            switch (user.Group)
            {
                case Define.E_UserGroup.None:
                    this.UpdateDisplauUserControls(false);
                    break;
                case Define.E_UserGroup.Level1:
                    this.UpdateDisplauUserControls(this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMenuRecipe);
                    break;
                case Define.E_UserGroup.Level2:
                    this.UpdateDisplauUserControls(this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMenuRecipe);
                    break;
                case Define.E_UserGroup.Level3:
                    this.UpdateDisplauUserControls(this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuRecipe);
                    break;
                case Define.E_UserGroup.Admin:
                    this.UpdateDisplauUserControls(this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMenuRecipe);
                    break;
                case Define.E_UserGroup.Developer:
                    this.UpdateDisplauUserControls(true);
                    break;
                case Define.E_UserGroup.NotLogin:
                    this.UpdateDisplauUserControls(false);
                    break;
                case Define.E_UserGroup.LogOut:
                    this.UpdateDisplauUserControls(false);
                    break;
                default:
                    break;
            }
        }
        private void UpdateDisplayControlData(SystemConfiguration item)
        {
            string value = "";

            // LCD data period
            value = item.LCD_DATA_PERIOD.ToString();
            if (this.labelLcdDataPeriod.Text != value)
                this.labelLcdDataPeriod.Text = value;

            // Chattering
            value = item.CHATTERING.ToString();
            if (this.labelChattering.Text != value)
                this.labelChattering.Text = value;

            // Cut wait
            value = item.JUDGMENT_DELAY_MSEC.ToString();
            if (this.labelCutWait.Text != value)
                this.labelCutWait.Text = value;
            
            // Buzzer
            value = item.BUZZER_OP.ToString();
            if (this.labelBuzzer.Text != value)
                this.labelBuzzer.Text = value;

            // Diff filter
            value = item.SB_DIFF_FILTER.ToString();
            if (this.labelSbDiffFilter.Text != value)
                this.labelSbDiffFilter.Text = value;

            // Equipment Lane
            value = item.EQUIPMENT_LANE.ToString();
            if (this.labelEquipmentLane.Text != value)
                this.labelEquipmentLane.Text = value;

            // Check Lane
            value = item.CHECK_LANE.ToString();
            if (this.labelCheckLane.Text != value)
                this.labelCheckLane.Text = value;
        }

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

            this.UpdateDisplayUser(this.ParentForm.ParentForm.CurrentSystemStatus.CurrentUser);
        }
        #endregion

        #region Event Handler
        private void labelLcdDataPeriod_Click(object sender, EventArgs e)
        {
            string value = "", before = "", after = "";

            before = this.labelLcdDataPeriod.Text;
            DialogFormNumKeyPad myKeypad = new DialogFormNumKeyPad(this.smartLabel14.Text, this.labelLcdDataPeriod.Text, 4, 0, false);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                if (myKeypad.IntValue < 100 || myKeypad.IntValue > 5000)
                {
                    this.MessageBoxRange = "100 ~ 5000";
                    this.labelLcdDataPeriod.Text = before;
                    this.smartTimerMessageShow.Start();
                    return;
                }

                this.labelLcdDataPeriod.Text = myKeypad.StringValue;
                this.ParentForm.ParentForm.SystemConfig.LCD_DATA_PERIOD = myKeypad.IntValue;
                this.ParentForm.ParentForm.SaveSystemConfigurationFile(this.ParentForm.ParentForm.SystemConfig);
                after = myKeypad.StringValue;

                value = Helper.StringBlankFillDigits4(myKeypad.StringValue);
                this.ParentForm.ParentForm.TransferDataStream_Master(CommunicationCommand.Write, CommunicationID.MainBoardMaster, CommunicationAddress._1710_LcdDataPeriod, value);
                
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.Etc_LcdDataPeriod, "", before, after);
            }
        }
        private void labelChattering_Click(object sender, EventArgs e)
        {
            string value = "", before = "", after = "";

            before = this.labelChattering.Text;
            DialogFormNumKeyPad myKeypad = new DialogFormNumKeyPad(this.smartLabel18.Text, this.labelChattering.Text, 4, 0, false);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                if (myKeypad.IntValue < 50 || myKeypad.IntValue > 500)
                {
                    this.MessageBoxRange = "50 ~ 500";
                    this.labelChattering.Text = before;
                    this.smartTimerMessageShow.Start();
                    return;
                }

                this.labelChattering.Text = myKeypad.StringValue;
                this.ParentForm.ParentForm.SystemConfig.CHATTERING = myKeypad.IntValue;
                this.ParentForm.ParentForm.SaveSystemConfigurationFile(this.ParentForm.ParentForm.SystemConfig);
                after = myKeypad.StringValue;

                value = Helper.StringBlankFillDigits4(myKeypad.StringValue);
                this.ParentForm.ParentForm.TransferDataStream_Master(CommunicationCommand.Write, CommunicationID.MainBoardMaster, CommunicationAddress._6006_Chattering, value);
               
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.Etc_Chattering, "", before, after);
            }
        }
        private void labelCutWait_Click(object sender, EventArgs e)
        {
            string value = "", before = "", after = "";

            before = this.labelCutWait.Text;
            DialogFormNumKeyPad myKeypad = new DialogFormNumKeyPad(this.smartLabel20.Text, this.labelCutWait.Text, 4, 0, false);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                if (myKeypad.IntValue < 100 || myKeypad.IntValue > 9999)
                {
                    this.MessageBoxRange = "100 ~ 9999";
                    this.labelCutWait.Text = before;
                    this.smartTimerMessageShow.Start();
                    return;
                }

                this.labelCutWait.Text = myKeypad.StringValue;
                this.ParentForm.ParentForm.SystemConfig.JUDGMENT_DELAY_MSEC = myKeypad.IntValue;
                this.ParentForm.ParentForm.SaveSystemConfigurationFile(this.ParentForm.ParentForm.SystemConfig);
                after = myKeypad.StringValue;

                value = Helper.StringBlankFillDigits4(myKeypad.StringValue);
                this.ParentForm.ParentForm.TransferDataStream_Master(CommunicationCommand.Write, CommunicationID.MainBoardMaster, CommunicationAddress._5508_CuttingWait, value);
                
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.Etc_CutWait, "", before, after);
            }
        }
        private void labelBuzzer_Click(object sender, EventArgs e)
        {
            string value = "", before = "", after = "";

            before = this.labelBuzzer.Text;
            DialogFormNumKeyPad myKeypad = new DialogFormNumKeyPad(this.smartLabel23.Text, this.labelBuzzer.Text, 4, 0, false);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                if (myKeypad.IntValue < 0 || myKeypad.IntValue > 9999)
                {
                    this.MessageBoxRange = "0 ~ 9999";
                    this.labelBuzzer.Text = before;
                    this.smartTimerMessageShow.Start();
                    return;
                }

                this.labelBuzzer.Text = myKeypad.StringValue;
                this.ParentForm.ParentForm.SystemConfig.BUZZER_OP = myKeypad.IntValue;
                this.ParentForm.ParentForm.SaveSystemConfigurationFile(this.ParentForm.ParentForm.SystemConfig);
                after = myKeypad.StringValue;

                value = Helper.StringBlankFillDigits4(myKeypad.StringValue);
                this.ParentForm.ParentForm.TransferDataStream_Master(CommunicationCommand.Write, CommunicationID.MainBoardMaster, CommunicationAddress._6001_BuzzerOP, value);
               
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.Etc_Buzzer, "", before, after);
            }
        }
        private void labelSbDiffFilter_Click(object sender, EventArgs e)
        {
            string value = "", before = "", after = "";

            before = this.labelSbDiffFilter.Text;
            DialogFormNumKeyPad myKeypad = new DialogFormNumKeyPad(this.smartLabel27.Text, this.labelSbDiffFilter.Text, 2, 0, false);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                if (myKeypad.IntValue < 1 || myKeypad.IntValue > 16)
                {
                    this.MessageBoxRange = "1 ~ 16";
                    this.labelSbDiffFilter.Text = before;
                    this.smartTimerMessageShow.Start();
                    return;
                }

                this.labelSbDiffFilter.Text = myKeypad.StringValue;
                this.ParentForm.ParentForm.SystemConfig.SB_DIFF_FILTER = myKeypad.IntValue;
                this.ParentForm.ParentForm.SaveSystemConfigurationFile(this.ParentForm.ParentForm.SystemConfig);
                after = myKeypad.StringValue;

                value = Helper.StringBlankFillDigits4(myKeypad.StringValue);
                this.ParentForm.ParentForm.TransferDataStream_Master(CommunicationCommand.Write, CommunicationID.MainBoardMaster, CommunicationAddress._1706_SbDiffFilter, value);
                
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.Etc_SbDiffFilter, "", before, after);
            }
        }
        private void labelEquipmentLane_Click(object sender, EventArgs e)
        {
            string value = "", before = "", after = "";

            before = this.labelEquipmentLane.Text;
            DialogFormNumKeyPad myKeypad = new DialogFormNumKeyPad(this.smartLabel37.Text, this.labelEquipmentLane.Text, 4, 0, false);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                if (myKeypad.IntValue < 1 || myKeypad.IntValue > 16)
                {
                    this.MessageBoxRange = "1 ~ 16";
                    this.labelEquipmentLane.Text = before;
                    this.smartTimerMessageShow.Start();
                    return;
                }

                this.labelEquipmentLane.Text = myKeypad.StringValue;
                this.ParentForm.ParentForm.SystemConfig.EQUIPMENT_LANE = myKeypad.IntValue;
                this.ParentForm.ParentForm.SaveSystemConfigurationFile(this.ParentForm.ParentForm.SystemConfig);
                after = myKeypad.StringValue;

                value = Helper.StringBlankFillDigits4(myKeypad.StringValue);
                this.ParentForm.ParentForm.TransferDataStream_Master(CommunicationCommand.Write, CommunicationID.MainBoardMaster, CommunicationAddress._1003_EquipmentLane, value);

                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.Etc_EquipmentLane, "", before, after);
            }
        }
        private void labelCheckLane_Click(object sender, EventArgs e)
        {
            string value = "", before = "", after = "";

            before = this.labelCheckLane.Text;
            DialogFormNumKeyPad myKeypad = new DialogFormNumKeyPad(this.smartLabel39.Text, this.labelCheckLane.Text, 4, 0, false);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                if (myKeypad.IntValue < 1 || myKeypad.IntValue > this.ParentForm.ParentForm.SystemConfig.EQUIPMENT_LANE)
                {
                    this.MessageBoxRange = string.Format("1 ~ {0}", this.ParentForm.ParentForm.SystemConfig.EQUIPMENT_LANE);
                    this.labelEquipmentLane.Text = before;
                    this.smartTimerMessageShow.Start();
                    return;
                }

                this.labelCheckLane.Text = myKeypad.StringValue;
                this.ParentForm.ParentForm.SystemConfig.CHECK_LANE = myKeypad.IntValue;
                this.ParentForm.ParentForm.SaveSystemConfigurationFile(this.ParentForm.ParentForm.SystemConfig);
                after = myKeypad.StringValue;

                value = Helper.StringBlankFillDigits4(myKeypad.StringValue);
                this.ParentForm.ParentForm.TransferDataStream_Master(CommunicationCommand.Write, CommunicationID.MainBoardMaster, CommunicationAddress._1711_CheckLane, value);

                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.Etc_CheckLane, "", before, after);
            }
        }

        private void smartTimerMessageShow_Tick(object sender, EventArgs e)
        {
            this.smartTimerMessageShow.Stop();

            DialogFormMessage msg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.LANGUAGE, this.MessageBoxRange);
            msg.ShowDialog();
        }
        #endregion
    }
}
