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

using SmartX;
using INT89DB_26_ImageDll;
using INT89DB_26.Controls;
using INT89DB_26.DialogForms;

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

        private delegate void ConnetedIPStatusCallback(string value);
        #endregion

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

            this.ParentForm = parent;

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

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

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

            if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Korean)
            {

            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.English)
            {
                this.labelTitle.Text = "Communication";

                this.labelStaticTcpOperationMode.Text = "Operation";
                this.labelStaticTcpMode.Text = "Mode";

                this.labelStaticBaudrate.Text = "Baudrate";
                this.smartLabel4.Text = "Mode";

                this.labelHelp.Text = "※ When IP configuration is changed, it is normally connected after rebooting.";


                this.buttonBack.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engBackUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Chinese)
            {
                this.labelTitle.Text = "选项";

                this.labelStaticTcpOperationMode.Text = "运转模式";
                this.labelStaticTcpMode.Text = "通信模式";

                this.groupBoxSerial.Text = "通信";
                this.labelStaticBaudrate.Text = "波特率";
                this.smartLabel4.Text = "模式";

                this.labelProtocolDescriptionCOM3_1.Text = "COM3协议说明";
                this.labelProtocolDescriptionCOM3_2.Text = "COM3协议说明";

                this.buttonSave2.Text = "保存";
                this.buttonSerialSend.Text = "发送";

                this.labelHelp.Text = "※ 更改IP配置后，通常在重新启动后即可连接。";

                this.buttonBack.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Czech)
            {

            }
            else
            {

            }
        }
        private void InitializeControl()
        {
            this.comboBoxTCPOperationMode.Items.Clear();
            this.comboBoxTCPOperationMode.SelectedIndexChanged -= new EventHandler(this.comboBoxOperationMode_SelectedIndexChanged);
            this.comboBoxTCPOperationMode.Items.Add("-");
            this.comboBoxTCPOperationMode.Items.Add("Server");
            this.comboBoxTCPOperationMode.Items.Add("Client");
            this.comboBoxTCPOperationMode.SelectedIndex = 0;
            this.comboBoxTCPOperationMode.SelectedIndexChanged += new EventHandler(this.comboBoxOperationMode_SelectedIndexChanged);

            this.comboBoxTCPMode.Items.Clear();
            this.comboBoxTCPMode.SelectedIndexChanged -= new EventHandler(this.comboBoxMode_SelectedIndexChanged);
            this.comboBoxTCPMode.Items.Add("-");
            this.comboBoxTCPMode.Items.Add("STD1");                     // index : 1
            this.comboBoxTCPMode.Items.Add("STD2");                     // index : 2
            this.comboBoxTCPMode.Items.Add("Modbus");                   // index : 99
            this.comboBoxTCPMode.SelectedIndex = 0;
            this.comboBoxTCPMode.SelectedIndexChanged += new EventHandler(this.comboBoxMode_SelectedIndexChanged);

            this.comboBoxSerial3BaudRate.Items.Clear();
            this.comboBoxSerial3BaudRate.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3BaudRate_SelectedIndexChanged);
            this.comboBoxSerial3BaudRate.Items.Add(9600);
            this.comboBoxSerial3BaudRate.Items.Add(19200);
            this.comboBoxSerial3BaudRate.Items.Add(38400);
            this.comboBoxSerial3BaudRate.Items.Add(115200);
            this.comboBoxSerial3BaudRate.SelectedIndex = 0;
            this.comboBoxSerial3BaudRate.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3BaudRate_SelectedIndexChanged);

            this.comboBoxSerial3Mode.Items.Clear();
            this.comboBoxSerial3Mode.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3Mode_SelectedIndexChanged);
            this.comboBoxSerial3Mode.Items.Add("-");
            this.comboBoxSerial3Mode.Items.Add("STD1");                 // index : 1
            this.comboBoxSerial3Mode.Items.Add("STD2");                 // index : 2
            if (this.ParentForm.SystemConfig.IsFeedbackBoard == true)
                this.comboBoxSerial3Mode.Items.Add("FB_Board");         // index : 98
            this.comboBoxSerial3Mode.SelectedIndex = 0;
            this.comboBoxSerial3Mode.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3Mode_SelectedIndexChanged);
        }
        private void DefaultSetting()
        {
            //string[] sValue;

            //#region TCP
            //if (this.ParentForm.SystemConfig.IsEthernetEnable == true)
            //{
            //    this.buttonEthernetStart.Enabled = true;
            //    this.buttonEthernetStop.Enabled = true;
            //    this.buttonEthernetSend.Enabled = true;
            //}
            //else
            //{
            //    this.buttonEthernetStart.Enabled = false;
            //    this.buttonEthernetStop.Enabled = false;
            //    this.buttonEthernetSend.Enabled = false;
            //}

            //this.labelServerStatus.Text = "-";
            //this.labelClientStatus.Text = "-";
            //this.labelConnectedIP.Text = "-";

            // Host IP
            if (this.ParentForm.SystemConfig.EthernetServerAddress == "")
            {
                this.ParentForm.SystemConfig.EthernetServerAddress = "0.0.0.0";
                this.ParentForm.SaveSystemConfigurationFile(this.ParentForm.SystemConfig);
            }
            //sValue = this.ParentForm.SystemConfig.EthernetServerAddress.Split('.');
            //this.labelHostIP1.Text = sValue[0];
            //this.labelHostIP2.Text = sValue[1];
            //this.labelHostIP3.Text = sValue[2];
            //this.labelHostIP4.Text = sValue[3];

            //this.labelHostPort.Text = this.ParentForm.SystemConfig.EthernetServerPort.ToString();
            //this.labelLocalPort.Text = this.ParentForm.SystemConfig.EthernetLocalPort.ToString();

            //this.buttonTcpSave1.Enabled = false;
            //this.buttonTcpSave2.Enabled = false;
            //#endregion

            //#region Serial
            //this.UpdateDisplayCom3();
            //#endregion
        }
        
        private void EthernetStart()
        {
            this.buttonEthernetStart.Enabled = true;
            this.buttonEthernetStop.Enabled = true;
            this.buttonEthernetSend.Enabled = true;

            if (this.ParentForm.SystemConfig.EthernetOperationMode == 1)
            {
                if (string.Compare(this.ParentForm.smartConfigs.IPSettings.DeviceIP, "0.0.0.0") == 0)
                {
                    if (this.labelServerStatus.Text != ">Addr Err")
                        this.labelServerStatus.Text = ">Addr Err";

                    this.timerServer.Enabled = false;
                }
                else
                {
                    this.ParentForm.EthernetServerStart();

                    if (this.timerServer.Enabled == false)
                        this.timerServer.Enabled = true;

                    this.ParentForm.ChildFormMainDisplay.TimerServer(true);
                }
            }
            else if (this.ParentForm.SystemConfig.EthernetOperationMode == 2)
            {
                this.ParentForm.EthernetClientConnect();
            }
        }
        private void EthernetStop()
        {
            if (this.ParentForm.SystemConfig.EthernetOperationMode == 1)
            {
                this.timerServer.Enabled = false;
                this.ParentForm.EthernetServerStop();

                if (this.labelServerStatus.Text != ">Stop")
                    this.labelServerStatus.Text = ">Stop";
            }
            else if (this.ParentForm.SystemConfig.EthernetOperationMode == 2)
            {
                this.ParentForm.EthernetClientDisconnect();
            }
        }
        private void UpdateDisplayCom3()
        {
            this.comboBoxSerial3BaudRate.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3BaudRate_SelectedIndexChanged);
            this.comboBoxSerial3BaudRate.SelectedItem = this.ParentForm.SystemConfig.Serial3BaudRate;
            this.comboBoxSerial3BaudRate.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3BaudRate_SelectedIndexChanged);

            this.comboBoxSerial3Mode.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3Mode_SelectedIndexChanged);
            switch (this.ParentForm.SystemConfig.Serial3Mode)
            {
                case (int)DataStore.CommunicationMode.f1_STD1:
                    this.comboBoxSerial3Mode.SelectedItem = "STD1";
                    break;
                case (int)DataStore.CommunicationMode.f2_STD2:
                    this.comboBoxSerial3Mode.SelectedItem = "STD2";
                    break;
                case (int)DataStore.CommunicationMode.f98_FB_Board:
                    this.comboBoxSerial3Mode.SelectedItem = "FB_Board";
                    this.comboBoxSerial3BaudRate.Enabled = false;
                    this.comboBoxSerial3Mode.Enabled = false;
                    break;
                default:
                    break;
            }
            this.comboBoxSerial3Mode.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3Mode_SelectedIndexChanged);

            this.buttonSave2.Enabled = false;

            if (this.ParentForm.SystemConfig.Serial3Mode != 0)
                this.buttonSerialSend.Enabled = true;
            else
                this.buttonSerialSend.Enabled = false;

            this.UpdateProtocolDescriptionCOM3();
        }
        private void UpdateDisplayTCP()
        {
            // Local IP
            this.UpdateLocalIPAddressDisplay();

            this.comboBoxTCPMode.SelectedIndexChanged -= new EventHandler(this.comboBoxMode_SelectedIndexChanged);
            this.comboBoxTCPOperationMode.SelectedIndexChanged -= new EventHandler(this.comboBoxOperationMode_SelectedIndexChanged);

            // 통신모드
            if (this.ParentForm.SystemConfig.EthernetMode == (int)DataStore.CommunicationMode.f99_Modbus)
                this.comboBoxTCPMode.SelectedItem = "Modbus";
            else
                this.comboBoxTCPMode.SelectedIndex = this.ParentForm.SystemConfig.EthernetMode;
            // 동작모드
            this.comboBoxTCPOperationMode.SelectedIndex = this.ParentForm.SystemConfig.EthernetOperationMode;

            this.comboBoxTCPMode.SelectedIndexChanged += new EventHandler(this.comboBoxMode_SelectedIndexChanged);
            this.comboBoxTCPOperationMode.SelectedIndexChanged += new EventHandler(this.comboBoxOperationMode_SelectedIndexChanged);

            // Format
            this.UpdateProtocolDescriptionEthernet();

            this.labelHostPort.Text = this.ParentForm.SystemConfig.EthernetServerPort.ToString();
            this.labelLocalPort.Text = this.ParentForm.SystemConfig.EthernetLocalPort.ToString();

            if (this.comboBoxTCPOperationMode.SelectedIndex == 1)
            {
                this.buttonEthernetStart.Enabled = true;
                this.buttonEthernetStop.Enabled = true;
                this.buttonEthernetSend.Enabled = true;

                if (string.Compare(this.ParentForm.smartConfigs.IPSettings.DeviceIP, "0.0.0.0") == 0)
                {
                    if (this.labelServerStatus.Text != ">Addr Err")
                        this.labelServerStatus.Text = ">Addr Err";

                    this.timerServer.Enabled = false;
                }
                else
                {
                    this.ParentForm.EthernetServerStart();

                    if (this.timerServer.Enabled != true)
                        this.timerServer.Enabled = true;
                }
            }
            else if (this.comboBoxTCPOperationMode.SelectedIndex == 2)
            {
                this.buttonEthernetStart.Enabled = true;
                this.buttonEthernetStop.Enabled = true;
                this.buttonEthernetSend.Enabled = true;

                this.labelConnectedIP.Text = "-";

                this.ParentForm.EthernetClientConnect();
            }
            else
            {
                this.buttonEthernetStart.Enabled = false;
                this.buttonEthernetStop.Enabled = false;
                this.buttonEthernetSend.Enabled = false;

                this.labelServerStatus.Text = "-";
                this.labelClientStatus.Text = "-";
                this.labelConnectedIP.Text = "-";
            }

            this.buttonTcpSave1.Enabled = false;
            this.buttonTcpSave2.Enabled = false;
        }

        private void UpdateProtocolDescriptionCOM3()
        {
            if (this.comboBoxSerial3Mode.SelectedIndex == 1)
            {
                this.labelProtocolDescriptionCOM3_1.Text = "STX[1],ID[5],NO[4],LANE[2],GRD[1],";
                this.labelProtocolDescriptionCOM3_2.Text = "WEIGHT[6],ETX[1]";
            }
            else if (this.comboBoxSerial3Mode.SelectedIndex == 2)
            {
                this.labelProtocolDescriptionCOM3_1.Text = "STX[1],ID[5],NO[4],LANE[2],SIGN[1],";
                this.labelProtocolDescriptionCOM3_2.Text = "GAP WEIGHT[6],ETX[1]";
            }
            else
            {
                this.labelProtocolDescriptionCOM3_1.Text = "";
                this.labelProtocolDescriptionCOM3_2.Text = "";
            }
        }
        private void UpdateProtocolDescriptionEthernet()
        {
            if (this.comboBoxTCPMode.SelectedIndex == (int)DataStore.CommunicationMode.f1_STD1)
            {
                this.labelProtocolDescriptionEthernet_1.Text = "STX[1],ID[5],NO[4],LANE[2],GRD[1],";
                this.labelProtocolDescriptionEthernet_2.Text = "WEIGHT[6],ETX[1]";
            }
            else if (this.comboBoxTCPMode.SelectedIndex == (int)DataStore.CommunicationMode.f2_STD2)
            {

                this.labelProtocolDescriptionEthernet_1.Text = "STX[1],ID[5],NO[4],LANE[2],SIGN[1],";
                this.labelProtocolDescriptionEthernet_2.Text = "GAP WEIGHT[6],ETX[1]";
            }
            else
            {
                this.labelProtocolDescriptionEthernet_1.Text = "";
                this.labelProtocolDescriptionEthernet_2.Text = "";
            }
        }

        private void UpdateDisplaySettingData(int index)
        {
            switch (index)
            {
                case 99: // Modbus
                    if (this.ParentForm.SystemConfig.EthernetOperationMode == 2) // Client일 때만 Start Address 사용
                    {
                        this.labelTitleStartAddress.Visible = true;
                        this.labelStartAddress.Visible = true;
                        this.labelStartAddress.Text = this.ParentForm.SystemConfig.ModbusTcpStartAddress.ToString();
                    }
                    break;
                default:
                    this.labelTitleStartAddress.Visible = false;
                    this.labelStartAddress.Visible = false;
                    break;
            }
        }

        private void ConnectedIPStatusOut(string value)
        {
            this.labelConnectedIP.Text = value;
        }
        public void DelegateConnectedIPStatusOut(string value)
        {
            this.Invoke(new ConnetedIPStatusCallback(this.ConnectedIPStatusOut), value);
        }

        private void LocalIPEnable(bool enable)
        {
            Color tempColor;

            if (enable == true)
                tempColor = Color.Black;
            else
                tempColor = Color.Gray;

            #region Color
            this.labelLocalIP1.ForeColor = tempColor;
            this.labelLocalIP2.ForeColor = tempColor;
            this.labelLocalIP3.ForeColor = tempColor;
            this.labelLocalIP4.ForeColor = tempColor;

            this.labelLocalSubnetMask1.ForeColor = tempColor;
            this.labelLocalSubnetMask2.ForeColor = tempColor;
            this.labelLocalSubnetMask3.ForeColor = tempColor;
            this.labelLocalSubnetMask4.ForeColor = tempColor;

            this.labelLocalGateway1.ForeColor = tempColor;
            this.labelLocalGateway2.ForeColor = tempColor;
            this.labelLocalGateway3.ForeColor = tempColor;
            this.labelLocalGateway4.ForeColor = tempColor;
            #endregion

            #region Enable
            this.labelLocalIP1.Enabled = enable;
            this.labelLocalIP2.Enabled = enable;
            this.labelLocalIP3.Enabled = enable;
            this.labelLocalIP4.Enabled = enable;

            this.labelLocalSubnetMask1.Enabled = enable;
            this.labelLocalSubnetMask2.Enabled = enable;
            this.labelLocalSubnetMask3.Enabled = enable;
            this.labelLocalSubnetMask4.Enabled = enable;

            this.labelLocalGateway1.Enabled = enable;
            this.labelLocalGateway2.Enabled = enable;
            this.labelLocalGateway3.Enabled = enable;
            this.labelLocalGateway4.Enabled = enable;
            #endregion
        }
        private void UpdateLocalIPAddressDisplay()
        {
            string[] sValue;


            // Local IP
            sValue = this.ParentForm.smartConfigs.IPSettings.DeviceIP.Split('.');
            if (sValue.Length == 4)
            {
                this.labelLocalIP1.Text = sValue[0];
                this.labelLocalIP2.Text = sValue[1];
                this.labelLocalIP3.Text = sValue[2];
                this.labelLocalIP4.Text = sValue[3];
            }

            // Local Subnet Mask
            sValue = this.ParentForm.smartConfigs.IPSettings.SubNetMask.Split('.');
            if (sValue.Length == 4)
            {
                this.labelLocalSubnetMask1.Text = sValue[0];
                this.labelLocalSubnetMask2.Text = sValue[1];
                this.labelLocalSubnetMask3.Text = sValue[2];
                this.labelLocalSubnetMask4.Text = sValue[3];
            }

            // Local GateWay
            sValue = this.ParentForm.smartConfigs.IPSettings.GateWay.Split('.');
            if (sValue.Length == 4)
            {
                this.labelLocalGateway1.Text = sValue[0];
                this.labelLocalGateway2.Text = sValue[1];
                this.labelLocalGateway3.Text = sValue[2];
                this.labelLocalGateway4.Text = sValue[3];
            }
        }
        public void DisplayLabelLocalPort(bool bValue)
        {
            if (bValue == false)
            {
                this.labelLocalPort.Enabled = false;
                this.labelLocalPort.BackColor = Color.Silver;
            }
            else
            {
                this.labelLocalPort.Enabled = true;
                this.labelLocalPort.BackColor = Color.White;
            }
        }
        public void UpdateEthernetStatusDisplay(string value)
        {
            if (this.labelClientStatus.Text != value)
                this.labelClientStatus.Text = value;
        }

        public void DisplayRefresh()
        {
            this.ParentForm.SystemConfig.CurrentForm = DataStore.E_FormStore.FormCommunication;

            this.UpdateDisplayTCP();
            this.UpdateDisplayCom3();
        }
        #endregion

        #region Event Handler

        #region Ethernet
        private void buttonEthernetStart_Click(object sender, EventArgs e)
        {
            this.EthernetStart();
        }
        private void buttonEthernetStop_Click(object sender, EventArgs e)
        {
            this.EthernetStop();
        }
        private void buttonEthernetSend_Click(object sender, EventArgs e)
        {
            string data = "";

            if (this.ParentForm.SystemConfig.EthernetMode == (int)DataStore.CommunicationMode.f1_STD1)
                data = this.ParentForm.Protocol_STD1(this.ParentForm.SystemConfig, this.ParentForm.CollectionWeightData[0], 1);
            else if (this.ParentForm.SystemConfig.EthernetMode == (int)DataStore.CommunicationMode.f2_STD2)
                data = this.ParentForm.Protocol_STD2(this.ParentForm.SystemConfig, this.ParentForm.CollectionWeightData[0], 1);

            switch (this.ParentForm.SystemConfig.EthernetOperationMode)
            {
                case 1:
                    if (this.ParentForm.smartTCPMultiServer.ClientInfoList != null)
                    {
                        foreach (SmartTCPMultiServer.CLIENTSINFOS info in this.ParentForm.smartTCPMultiServer.ClientInfoList)
                            this.ParentForm.EthernetServerSendMessage(info, data);
                    }
                    break;
                case 2:
                    this.ParentForm.EthernetClientSendMessage(data);
                    break;
                default:
                    break;
            }
        }

        private void timerServer_Tick(object sender, EventArgs e)
        {
            if (this.ParentForm.smartTCPMultiServer.IsStart == true)
            {
                if (this.labelServerStatus.Text != ">Start")
                    this.labelServerStatus.Text = ">Start";

                this.labelConnectedIP.Text = "-";

                if (this.ParentForm.smartTCPMultiServer.ClientInfoList != null)
                    this.labelConnectedIP.Text = this.ParentForm.smartTCPMultiServer.ClientInfoList[0].strIPAddress;
            }
            else
            {
                if (this.labelServerStatus.Text != ">Stop")
                    this.labelServerStatus.Text = ">Stop";
            }
        }

        private void comboBoxOperationMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBoxTCPOperationMode.SelectedIndex == 1)
            {
                #region Server
                this.ParentForm.EthernetClientDisconnect();
                this.ParentForm.ChildFormMainDisplay.TimerServer(true);

                this.buttonEthernetStart.Enabled = true;
                this.buttonEthernetStop.Enabled = true;
                this.buttonEthernetSend.Enabled = true;
                #endregion
            }
            else if (this.comboBoxTCPOperationMode.SelectedIndex == 2)
            {
                #region Client
                this.labelServerStatus.Text = "-";
                this.timerServer.Enabled = false;
                this.ParentForm.EthernetServerStop();
                this.ParentForm.EthernetClientDisconnect();
                this.ParentForm.ChildFormMainDisplay.TimerServer(false);

                this.buttonEthernetStart.Enabled = true;
                this.buttonEthernetStop.Enabled = true;
                this.buttonEthernetSend.Enabled = true;
                #endregion
            }
            else
            {
                #region None
                this.timerServer.Enabled = false;
                this.ParentForm.EthernetServerStop();
                this.ParentForm.EthernetClientDisconnect();

                this.buttonEthernetStart.Enabled = false;
                this.buttonEthernetStop.Enabled = false;
                this.buttonEthernetSend.Enabled = false;

                this.labelServerStatus.Text = "-";
                this.labelClientStatus.Text = "-";
                this.labelConnectedIP.Text = "-";
                #endregion
            }

            this.ParentForm.SystemConfig.EthernetOperationMode = this.comboBoxTCPOperationMode.SelectedIndex;
            this.ParentForm.SaveSystemConfigurationFile(this.ParentForm.SystemConfig);
        }
        private void comboBoxMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (this.comboBoxTCPMode.SelectedItem.ToString())
            {
                case "-":
                    this.ParentForm.SystemConfig.EthernetMode = 0;
                    break;
                case "STD1":
                    this.ParentForm.SystemConfig.EthernetMode = (int)DataStore.CommunicationMode.f1_STD1;
                    break;
                case "STD2":
                    this.ParentForm.SystemConfig.EthernetMode = (int)DataStore.CommunicationMode.f2_STD2;
                    break;
                case "Modbus":
                    this.ParentForm.SystemConfig.EthernetMode = (int)DataStore.CommunicationMode.f99_Modbus;
                    break;
                default:
                    break;
            }

            this.UpdateDisplaySettingData(this.ParentForm.SystemConfig.EthernetMode);

            this.ParentForm.SaveSystemConfigurationFile(this.ParentForm.SystemConfig);
            this.UpdateProtocolDescriptionEthernet();
        }

        private void labelLocalIP1_Click(object sender, EventArgs e)
        {
            string temp = "";

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalIP1.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalIP2_Click(object sender, EventArgs e)
        {
            string temp = "";

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalIP2.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalIP3_Click(object sender, EventArgs e)
        {
            string temp = "";

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalIP3.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalIP4_Click(object sender, EventArgs e)
        {
            string temp = "";

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalIP4.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalSubnetMask1_Click(object sender, EventArgs e)
        {
            string temp = "";

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalSubnetMask1.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalSubnetMask2_Click(object sender, EventArgs e)
        {
            string temp = "";

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalSubnetMask2.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalSubnetMask3_Click(object sender, EventArgs e)
        {
            string temp = "";

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalSubnetMask3.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalSubnetMask4_Click(object sender, EventArgs e)
        {
            string temp = "";

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalSubnetMask4.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalGateway1_Click(object sender, EventArgs e)
        {
            string temp = "";

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalGateway1.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalGateway2_Click(object sender, EventArgs e)
        {
            string temp = "";

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalGateway2.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalGateway3_Click(object sender, EventArgs e)
        {
            string temp = "";

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalGateway3.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalGateway4_Click(object sender, EventArgs e)
        {
            string temp = "";

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalGateway4.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelStartAddress_Click(object sender, EventArgs e)
        {
            string before = "", after = "";

            before = this.labelStartAddress.Text;

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

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

                    this.ParentForm.SaveSystemConfigurationFile(this.ParentForm.SystemConfig);
                }
            }
        }
        private void buttonTcpSave1_Click(object sender, EventArgs e)
        {
            this.buttonTcpSave1.Enabled = false;

            if (this.ParentForm.SystemConfig.EthernetOperationMode == 1)
                this.ParentForm.EthernetServerStop();
            else if (this.ParentForm.SystemConfig.EthernetOperationMode == 2)
                this.ParentForm.EthernetClientDisconnect();

            this.ParentForm.smartConfigs.IPSettings.DeviceIP = string.Format("{0}.{1}.{2}.{3}",
                this.labelLocalIP1.Text, this.labelLocalIP2.Text, this.labelLocalIP3.Text, this.labelLocalIP4.Text);

            this.ParentForm.smartConfigs.IPSettings.SubNetMask = string.Format("{0}.{1}.{2}.{3}",
                this.labelLocalSubnetMask1.Text, this.labelLocalSubnetMask2.Text, this.labelLocalSubnetMask3.Text, this.labelLocalSubnetMask4.Text);

            this.ParentForm.smartConfigs.IPSettings.GateWay = string.Format("{0}.{1}.{2}.{3}",
                this.labelLocalGateway1.Text, this.labelLocalGateway2.Text, this.labelLocalGateway3.Text, this.labelLocalGateway4.Text);

            this.ParentForm.smartConfigs.IPSettings.Save();
            this.ParentForm.smartConfigs.IPSettings.SetApply();

            if (this.ParentForm.SystemConfig.EthernetOperationMode == 1)
                this.ParentForm.EthernetServerStart();
            else if (this.ParentForm.SystemConfig.EthernetOperationMode == 2)
                this.ParentForm.EthernetClientConnect();
        }

        private void labelLocalPort_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelLocalPort.Text, 4, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                this.ParentForm.EthernetServerStop();

                this.labelLocalPort.Text = myKeyPad.StringValue;
                this.ParentForm.smartTCPMultiServer.Port = myKeyPad.IntValue;
                this.ParentForm.SystemConfig.EthernetLocalPort = myKeyPad.IntValue;
                this.ParentForm.SaveSystemConfigurationFile(this.ParentForm.SystemConfig);
            }
        }
        private void labelHostIP1_Click(object sender, EventArgs e)
        {
            string temp = "";

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelHostIP1.Text = myKeyPad.StringValue;
                    this.buttonTcpSave2.Enabled = true;
                }
            }
        }
        private void labelHostIP2_Click(object sender, EventArgs e)
        {
            string temp = "";

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelHostIP2.Text = myKeyPad.StringValue;
                    this.buttonTcpSave2.Enabled = true;
                }
            }
        }
        private void labelHostIP3_Click(object sender, EventArgs e)
        {
            string temp = "";

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelHostIP3.Text = myKeyPad.StringValue;
                    this.buttonTcpSave2.Enabled = true;
                }
            }
        }
        private void labelHostIP4_Click(object sender, EventArgs e)
        {
            string temp = "";

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelHostIP4.Text = myKeyPad.StringValue;
                    this.buttonTcpSave2.Enabled = true;
                }
            }
        }
        private void labelHostPort_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelHostPort.Text, 4, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                this.labelHostPort.Text = myKeyPad.StringValue;
                this.buttonTcpSave2.Enabled = true;
            }
        }
        private void buttonTcpSave2_Click(object sender, EventArgs e)
        {
            // Server Port
            this.ParentForm.SystemConfig.EthernetServerPort = int.Parse(this.labelHostPort.Text);
            // Server Address
            this.ParentForm.SystemConfig.EthernetServerAddress = string.Format("{0}.{1}.{2}.{3}",
                this.labelHostIP1.Text, this.labelHostIP2.Text, this.labelHostIP3.Text, this.labelHostIP4.Text);

            this.ParentForm.SaveSystemConfigurationFile(this.ParentForm.SystemConfig);
            this.ParentForm.EthernetClientDisconnect();
            this.buttonTcpSave2.Enabled = false;
        }
        #endregion

        #region Serial
        private void buttonSave2_Click(object sender, EventArgs e)
        {
            // Serial3 BaudRate
            this.ParentForm.SystemConfig.Serial3BaudRate = (int)this.comboBoxSerial3BaudRate.SelectedItem;
            // Serial3 Mode
            switch (this.comboBoxSerial3Mode.SelectedItem.ToString())
            {
                case "-":
                    this.ParentForm.SystemConfig.Serial3Mode = 0;
                    break;
                case "STD1":
                    this.ParentForm.SystemConfig.Serial3Mode = (int)DataStore.CommunicationMode.f1_STD1;
                    break;
                case "STD2":
                    this.ParentForm.SystemConfig.Serial3Mode = (int)DataStore.CommunicationMode.f2_STD2;
                    break;
                case "FB_Board":
                    this.ParentForm.SystemConfig.Serial3Mode = (int)DataStore.CommunicationMode.f98_FB_Board;
                    break;
                default:
                    break;
            }
        
            // Save SystemConfiguration
            this.ParentForm.SaveSystemConfigurationFile(this.ParentForm.SystemConfig);

            if (this.ParentForm.SystemConfig.Serial3Mode != 0)
            {
                this.ParentForm.SerialPort3_Open();
                this.buttonSerialSend.Enabled = true;
            }
            else
            {
                this.ParentForm.SerialPort3_Close();
                this.buttonSerialSend.Enabled = false;
            }
            
            this.buttonSave2.Enabled = false;
        }
        private void buttonSerialSend_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.SystemConfig.Serial3Mode == (int)DataStore.CommunicationMode.f1_STD1)
                this.ParentForm.SerialPort3_Write(this.ParentForm.Protocol_STD1_NotStxEtx(this.ParentForm.SystemConfig, this.ParentForm.CollectionWeightData[0], 1));
            else if (this.ParentForm.SystemConfig.Serial3Mode == (int)DataStore.CommunicationMode.f2_STD2)
                this.ParentForm.SerialPort3_Write(this.ParentForm.Protocol_STD2_NotStxEtx(this.ParentForm.SystemConfig, this.ParentForm.CollectionWeightData[0], 1));
            else if (this.ParentForm.SystemConfig.Serial3Mode == (int)DataStore.CommunicationMode.f98_FB_Board)
                this.ParentForm.SerialPort3_FbBoardTestSend(1);
        }

        private void comboBoxSerial3BaudRate_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.buttonSave2.Enabled = true;
        }
        private void comboBoxSerial3Mode_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.UpdateProtocolDescriptionCOM3();
            this.buttonSave2.Enabled = true;
        } 
        #endregion

        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.ParentForm.ChildFormMenu.DisplayRefresh();
            ((FormMain)(Owner)).smartForm.Show((int)DataStore.E_FormStore.FormMenu);
        }
        #endregion
    }
}