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 SmartX;
using INT51DB.DialogForms;
using INT51DB.Forms;
using INT51DB_ImageDll;

namespace INT51DB.Controls
{
    public partial class ControlMainDisplayMenu : UserControl
    {
        #region Field
        private FormMainDisplay m_ParentForm;
        #endregion

        #region Constructor
        public ControlMainDisplayMenu(FormMainDisplay parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

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

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

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

            if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
            }
            else
            {
            }
        }
        private void DefaultSetting()
        {
            this.comboBoxSorterA.SelectedIndexChanged -= new EventHandler(this.comboBoxSorterA_SelectedIndexChanged);
            this.comboBoxSorterA.Items.Clear();
            this.comboBoxSorterA.Items.Add("사용안함");
            this.comboBoxSorterA.Items.Add("과량");
            this.comboBoxSorterA.Items.Add("경량");
            this.comboBoxSorterA.Items.Add("불량");
            this.comboBoxSorterA.SelectedIndex = 1;
            this.comboBoxSorterA.SelectedIndexChanged += new EventHandler(this.comboBoxSorterA_SelectedIndexChanged);

            this.comboBoxSorterB.SelectedIndexChanged -= new EventHandler(this.comboBoxSorterB_SelectedIndexChanged);
            this.comboBoxSorterB.Items.Clear();
            this.comboBoxSorterB.Items.Add("사용안함");
            this.comboBoxSorterB.Items.Add("과량");
            this.comboBoxSorterB.Items.Add("경량");
            this.comboBoxSorterB.Items.Add("불량");
            this.comboBoxSorterB.SelectedIndex = 2;
            this.comboBoxSorterB.SelectedIndexChanged += new EventHandler(this.comboBoxSorterB_SelectedIndexChanged);
        }

        private void UpdateControl(JudgmentSetItem item, SystemParameter4 parameter)
        {
            int iValue = 0;
            string value = "";

            #region Judgment
            value = item.UpDelayTime.ToString();
            if (this.labelUpDelayTime.Text != value)
                this.labelUpDelayTime.Text = value;

            value = item.DownDelayTime.ToString();
            if (this.labelDownDelayTime.Text != value)
                this.labelDownDelayTime.Text = value;
            #endregion

            #region Sorter
            this.comboBoxSorterA.SelectedIndexChanged -= new EventHandler(this.comboBoxSorterA_SelectedIndexChanged);
            this.comboBoxSorterB.SelectedIndexChanged -= new EventHandler(this.comboBoxSorterB_SelectedIndexChanged);

            // 선별기A 모드
            iValue = item.Sorter1Mode;
            if (this.comboBoxSorterA.SelectedIndex != iValue)
                this.comboBoxSorterA.SelectedIndex = iValue;

            // 선별기A 지연시간
            iValue = item.Sorter1DelayTime;
            if (this.labelSorterADelayTime.Text != iValue.ToString())
                this.labelSorterADelayTime.Text = iValue.ToString();

            // 선별기A 동작시간
            iValue = item.Sorter1RunTime;
            if (this.labelSorterARunTime.Text != iValue.ToString())
                this.labelSorterARunTime.Text = iValue.ToString();

            // 선별기B 모드
            iValue = item.Sorter2Mode;
            if (this.comboBoxSorterB.SelectedIndex != iValue)
                this.comboBoxSorterB.SelectedIndex = iValue;

            // 선별기B 지연시간
            iValue = item.Sorter2DelayTime;
            if (this.labelSorterBDelayTime.Text != iValue.ToString())
                this.labelSorterBDelayTime.Text = iValue.ToString();

            // 선별기B 동작시간
            iValue = item.Sorter2RunTime;
            if (this.labelSorterBRunTime.Text != iValue.ToString())
                this.labelSorterBRunTime.Text = iValue.ToString(); 
            #endregion
        }
       
        public void DisplayRefresh(SystemStatus status)
        {
            this.UpdateControl(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.CurrentSystemParameter4);
        }
        #endregion

        #region Event Handler
        private void comboBoxSorterA_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        private void labelSorterDelayTime_Click(object sender, EventArgs e)
        {
            string value = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelSorterADelayTime.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 0 || myKeyPad.doubleValue > 9999)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelSorterADelayTime.Text = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1DelayTime = myKeyPad.IntValue;
                    this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig.ProductNumber - 1);

                    value = Helper.StringZeroFillDigits4(this.labelSorterADelayTime.Text);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.Sorter1DelayTime, value);
                }
            }
        }
        private void labelSorterRunTime_Click(object sender, EventArgs e)
        {
            string value = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelSorterARunTime.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 0 || myKeyPad.doubleValue > 9999)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelSorterARunTime.Text = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1RunTime = myKeyPad.IntValue;
                    this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig.ProductNumber - 1);

                    value = Helper.StringZeroFillDigits4(this.labelSorterARunTime.Text);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.Sorter1RunTime, value);
                }
            }
        }

        private void comboBoxSorterB_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        private void labelSorterBDelayTime_Click(object sender, EventArgs e)
        {
            string value = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelSorterBDelayTime.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 0 || myKeyPad.doubleValue > 9999)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelSorterBDelayTime.Text = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2DelayTime = myKeyPad.IntValue;
                    this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig.ProductNumber - 1);

                    value = Helper.StringZeroFillDigits4(this.labelSorterBDelayTime.Text);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.Sorter2DelayTime, value);
                }
            }
        }
        private void labelSorterBRunTime_Click(object sender, EventArgs e)
        {
            string value = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelSorterBRunTime.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 0 || myKeyPad.doubleValue > 9999)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelSorterBRunTime.Text = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2RunTime = myKeyPad.IntValue;
                    this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig.ProductNumber - 1);

                    value = Helper.StringZeroFillDigits4(this.labelSorterBRunTime.Text);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.Sorter2RunTime, value);
                }
            }
        }

        private void labelUpDelayTime_Click(object sender, EventArgs e)
        {
            string value = "", message = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelUpDelayTime.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 0 || myKeyPad.doubleValue > 9999)
                {
                    // 입력범위를 확인하세요
                    message = "0~9999";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language, message);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelUpDelayTime.Text = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.CurrentJudgmentSetItem.UpDelayTime = myKeyPad.IntValue;

                    value = Helper.StringZeroFillDigits4(myKeyPad.StringValue);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.UpDelayTime, value);

                    this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig.ProductNumber - 1);
                }
            }
        }
        private void labelDownDelayTime_Click(object sender, EventArgs e)
        {
            string value = "", message = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelDownDelayTime.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 0 || myKeyPad.doubleValue > 9999)
                {
                    // 입력범위를 확인하세요
                    message = "0~9999";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language, message);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelDownDelayTime.Text = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.CurrentJudgmentSetItem.DownDelayTime = myKeyPad.IntValue;

                    value = Helper.StringZeroFillDigits4(myKeyPad.StringValue);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.DownDelayTime, value);

                    this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig.ProductNumber - 1);
                }
            }
        }
        #endregion
    }
}
