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

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

        #region Constructor
        public ControlCenterSystemSorterSetting(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; }
        }
        #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("NG");
            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("NG");
            this.comboBoxSorterB.SelectedIndex = 2;
            this.comboBoxSorterB.SelectedIndexChanged += new EventHandler(this.comboBoxSorterB_SelectedIndexChanged);

        }

        private void UpdateControlsDisplay(JudgmentSetItem item,SystemParameter1 parameter)
        { 
            int value = 0;

            if (this.labelProductNumber.Text != this.ParentForm.ParentForm.SystemConfig.ProductNumber.ToString())
                this.labelProductNumber.Text = this.ParentForm.ParentForm.SystemConfig.ProductNumber.ToString();

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

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

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

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

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

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

            // 선별기B 동작시간
            value = item.Sorter2RunTime;
            if (this.labelSorterBRunTime.Text != value.ToString())
                this.labelSorterBRunTime.Text = value.ToString();

            // 이중진입
            value = int.Parse(parameter.SorterDoubleEntry);
            if (value == 0)
                this.radioButtonDoubleEntryNone.Checked = true;
            else if (value == 1)
                this.radioButtonDoubleEntrySorterA.Checked = true;
            else if (value == 2)
                this.radioButtonDoubleEntrySorterB.Checked = true;
            else
                this.radioButtonDoubleEntryNone.Checked = true;

            // 외부입력
            value = int.Parse(parameter.SorterExternalNgInput);
            if (value == 0)
                this.radioButtonExternalInputNone.Checked = true;
            else if (value == 1)
                this.radioButtonExternalInputSorterA.Checked = true;
            else if (value == 2)
                this.radioButtonExternalInputSorterB.Checked = true;
            else
                this.radioButtonExternalInputNone.Checked = true;

            this.comboBoxSorterA.SelectedIndexChanged += new EventHandler(this.comboBoxSorterA_SelectedIndexChanged);
            this.comboBoxSorterB.SelectedIndexChanged += new EventHandler(this.comboBoxSorterB_SelectedIndexChanged);

            this.buttonSave.Visible = false;
        }
        public void UpdateEquipmentStatusDisplay(DataStore.EquipmentStatus status)
        {
            if (status == DataStore.EquipmentStatus.Start)
            {

            }
            else
            {

            }
        }

        public void DisplayRefresh(DataStore.UserGroup group)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = DataStore.DisplayStore.SystemSorterSetting;
            this.ParentForm.ParentForm.SetDisplayMode(DataStore.DisplayMode.SorterTest);
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);

            this.UpdateControlsDisplay(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.CurrentSystemParameter1);
        }
        #endregion

        #region Event Handler
        private void buttonSorterA_Click(object sender, EventArgs e)
        {
            //string value = "";

            //if (this.buttonSorterA.ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
            //{
            //    value = "0000";
            //    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.SorterTest, value);
            //}
        }
        private void buttonSorterB_Click(object sender, EventArgs e)
        {
            //string value = "";

            //if (this.buttonSorterB.ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
            //{
            //    value = "0001";
            //    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.SorterTest, value);
            //}
        }
        private void labelSorterADelayTime_Click(object sender, EventArgs e)
        {
            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;

                    if (this.buttonSave.Visible != true)
                        this.buttonSave.Visible = true;
                }
            }
        }
        private void labelSorterARunTime_Click(object sender, EventArgs e)
        {
            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;

                    if (this.buttonSave.Visible != true)
                        this.buttonSave.Visible = true;
                }
            }
        }
        private void labelSorterBDelayTime_Click(object sender, EventArgs e)
        {
            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;

                    if (this.buttonSave.Visible != true)
                        this.buttonSave.Visible = true;
                }
            }
        }
        private void labelSorterBRunTime_Click(object sender, EventArgs e)
        {
            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;

                    if (this.buttonSave.Visible != true)
                        this.buttonSave.Visible = true;
                }
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1Mode.ToString()));
            sb.Append(Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1DelayTime.ToString()));
            sb.Append(Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1RunTime.ToString()));
            sb.Append(Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2Mode.ToString()));
            sb.Append(Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2DelayTime.ToString()));
            sb.Append(Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2RunTime.ToString()));
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.UserDefined1, sb.ToString());

            this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1Mode = this.comboBoxSorterA.SelectedIndex;
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1DelayTime = int.Parse(this.labelSorterADelayTime.Text);
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1RunTime = int.Parse(this.labelSorterARunTime.Text);
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2Mode = this.comboBoxSorterB.SelectedIndex;
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2DelayTime = int.Parse(this.labelSorterBDelayTime.Text);
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2RunTime = int.Parse(this.labelSorterBRunTime.Text);
            this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig.ProductNumber - 1);

            this.buttonSave.Visible = false;
        }

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

            if (this.radioButtonDoubleEntryNone.Checked == true)
                value = "0000";
            else if (this.radioButtonDoubleEntrySorterA.Checked == true)
                value = "0001";
            else if (this.radioButtonDoubleEntrySorterB.Checked == true)
                value = "0002";
            else
                value = "0000";

            this.ParentForm.ParentForm.CurrentSystemParameter1.SorterDoubleEntry = value;
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.DoubleEnter, value);
        }

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

            if (this.radioButtonExternalInputNone.Checked == true)
                value = "0000";
            else if (this.radioButtonExternalInputSorterA.Checked == true)
                value = "0001";
            else if (this.radioButtonExternalInputSorterB.Checked == true)
                value = "0002";
            else
                value = "0000";

            this.ParentForm.ParentForm.CurrentSystemParameter1.SorterExternalNgInput = value;
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.ExternalInput, value);
        }

        private void comboBoxSorterA_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBoxSorterA.Items.Count == 0 || this.comboBoxSorterB.Items.Count == 0)
                return;

            if (this.buttonSave.Visible != true)
                this.buttonSave.Visible = true;

            this.comboBoxSorterB.SelectedIndexChanged -= new EventHandler(this.comboBoxSorterB_SelectedIndexChanged);

            if (this.comboBoxSorterA.SelectedIndex == 1)
                this.comboBoxSorterB.SelectedIndex = 2;
            else if (this.comboBoxSorterA.SelectedIndex == 2)
                this.comboBoxSorterB.SelectedIndex = 1;
            else if (this.comboBoxSorterA.SelectedIndex == 3)
                this.comboBoxSorterB.SelectedIndex = 0;

            this.comboBoxSorterB.SelectedIndexChanged += new EventHandler(this.comboBoxSorterB_SelectedIndexChanged);
        }
        private void comboBoxSorterB_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBoxSorterA.Items.Count == 0 || this.comboBoxSorterB.Items.Count == 0)
                return;

            if (this.buttonSave.Visible != true)
                this.buttonSave.Visible = true;

            this.comboBoxSorterA.SelectedIndexChanged -= new EventHandler(this.comboBoxSorterA_SelectedIndexChanged);

            if (this.comboBoxSorterB.SelectedIndex == 1)
                this.comboBoxSorterA.SelectedIndex = 2;
            else if (this.comboBoxSorterB.SelectedIndex == 2)
                this.comboBoxSorterA.SelectedIndex = 1;
            else if (this.comboBoxSorterB.SelectedIndex == 3)
                this.comboBoxSorterA.SelectedIndex = 0;

            this.comboBoxSorterA.SelectedIndexChanged += new EventHandler(this.comboBoxSorterA_SelectedIndexChanged);
        }
        #endregion

        private void buttonTest_Click(object sender, EventArgs e)
        {

        }
    }
}
