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 ITC81DB_2.Forms;
using ITC81DB_2_ImageDll;
using ITC81DB_2.DialogForms;

namespace ITC81DB_2.Controls
{
    public partial class ControlConfiEthernetLocalIP : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;

        string[] HexArray;
        #endregion

        #region Constructor
        public ControlConfiEthernetLocalIP(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 == Define.E_LanguageID.English)
            {
                this.labelHelp.Text = "※ After setup, reboot is required.";

                this.labelTitleIPConfiguration.Text = "IP Configuration";
                this.labelTitleIPAddress.Text = "IP Address";
                this.labelTitleSubnetMask.Text = "Subnet mask";
                this.labelTitleGateway.Text = "Gateway";
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Chinese)
            {
                this.labelHelp.Text = "※ 设置完成后需要重启";

                this.buttonAddon.Text = "服务器/客户端";

                this.labelTitleIPConfiguration.Text = "IP选项";
                this.labelTitleIPAddress.Text = "IP地址";
                this.labelTitleSubnetMask.Text = "子网屏蔽码";
                this.labelTitleGateway.Text = "网关";
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Russian)
            {
                this.labelHelp.Text = "※ После установки требуется перезагрузка";

                this.buttonAddon.Text = "Сервер/Клиент";

                this.labelTitleIPConfiguration.Text = "Настройки";
                this.labelTitleIPAddress.Text = "IP адрес";
                this.labelTitleSubnetMask.Text = "Маска подсети";
                this.labelTitleGateway.Text = "Шлюз";
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.German)
            {
                this.labelHelp.Text = "※ Nach der Einrichtung ist ein Neustart erforderlich";

                this.labelTitleIPConfiguration.Text = "IP Konfiguration";
                this.labelTitleIPAddress.Text = "IP-Adresse";
                this.labelTitleSubnetMask.Text = "Subnet mask";
                this.labelTitleGateway.Text = "Gateway";
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
            {
                this.labelHelp.Text = "※ Después de la configuración, es necesario reiniciar";

                this.labelTitleIPConfiguration.Text = "IP Configuration";
                this.labelTitleIPAddress.Text = "IP Address";
                this.labelTitleSubnetMask.Text = "Subnet mask";
                this.labelTitleGateway.Text = "Gateway";
            }
            else
            {
                //this.labelMessage1.Text = "로컬 데이터 셋팅 후 재부팅 하십시오.";
                this.labelHelp.Text = "※ 설정 후, 재부팅 필수";

                this.labelTitleIPConfiguration.Text = "IP Configuration";
                this.labelTitleIPAddress.Text = "IP Address";
                this.labelTitleSubnetMask.Text = "Subnet mask";
                this.labelTitleGateway.Text = "Gateway";
            }
        }
        private void DefaultSetting()
        {
            this.buttonSave.Enabled = false;
        }

        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.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.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.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];
            }
        }
        private void UpdateLocalIPConfigurationDisplay()
        {
            // Local IP DHCP
            if (this.ParentForm.ParentForm.smartConfigs.IPSettings.DHCPEnable == 1)
            {
                this.radioButtonLocalIpDHCP.Checked = true;
                this.LocalIPEnable(false);
            }
            else
            {
                this.radioButtonLocalIpStatic.Checked = true;
                this.LocalIPEnable(true);
            }
        }
        private void UpdateMACAddressDisplay()
        {
            this.buttonSave2.Enabled = false;

            byte[] currentMAC = this.ParentForm.ParentForm.smartConfigs.IPSettings.GetMACAddress();
            string hex = BitConverter.ToString(currentMAC);
            this.HexArray = hex.Split('-');

            if (this.HexArray.Length >= 6)
            {
                this.labelMACAddress1.Text = this.HexArray[0] + ":" + this.HexArray[1] + ":" + this.HexArray[2] + ":";
                this.labelMACAddress4.Text = this.HexArray[3];
                this.labelMACAddress5.Text = this.HexArray[4];
                this.labelMACAddress6.Text = this.HexArray[5];
            }
        }

        public void DisplayRefresh()
        {
            // Local IP DHCP
            this.UpdateLocalIPConfigurationDisplay();

            // Local IP
            this.UpdateLocalIPAddressDisplay();

            // MAC Address
            this.UpdateMACAddressDisplay();
        }
        #endregion

        #region Event Handler
        private void buttonSave1_Click(object sender, EventArgs e)
        {
            string beforeIP = "", afterIP = "";
            string beforeSubnetMask = "", afterSubnetMask = "";
            string beforeGateway = "", afterGateway = "";

            beforeIP = this.ParentForm.ParentForm.smartConfigs.IPSettings.DeviceIP;
            beforeSubnetMask = this.ParentForm.ParentForm.smartConfigs.IPSettings.SubNetMask;
            beforeGateway = this.ParentForm.ParentForm.smartConfigs.IPSettings.GateWay;

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

            this.buttonSave.Enabled = false;

            this.ParentForm.ParentForm.smartConfigs.IPSettings.DeviceIP = string.Format("{0}.{1}.{2}.{3}",
                this.labelLocalIP1.Text, this.labelLocalIP2.Text, this.labelLocalIP3.Text, this.labelLocalIP4.Text);
            this.ParentForm.ParentForm.smartConfigs.IPSettings.SubNetMask = string.Format("{0}.{1}.{2}.{3}",
                this.labelLocalSubnetMask1.Text, this.labelLocalSubnetMask2.Text, this.labelLocalSubnetMask3.Text, this.labelLocalSubnetMask4.Text);
            this.ParentForm.ParentForm.smartConfigs.IPSettings.GateWay = string.Format("{0}.{1}.{2}.{3}",
                this.labelLocalGateway1.Text, this.labelLocalGateway2.Text, this.labelLocalGateway3.Text, this.labelLocalGateway4.Text);

            afterIP = this.ParentForm.ParentForm.smartConfigs.IPSettings.DeviceIP;
            afterSubnetMask = this.ParentForm.ParentForm.smartConfigs.IPSettings.SubNetMask;
            afterGateway = this.ParentForm.ParentForm.smartConfigs.IPSettings.GateWay;

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

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

            //if (beforeIP != afterIP)
            //    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.TCPLocalIPAddress, "", beforeIP, afterIP);
            //if (beforeSubnetMask != afterSubnetMask)
            //    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.TCPLocalSubnetMask, "", beforeSubnetMask, afterSubnetMask);
            //if (beforeGateway != afterGateway)
            //    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.TCPLocalGateway, "", beforeGateway, afterGateway);
        }

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

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

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

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

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

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

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

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalIP4.Text = myKeyPad.StringValue;
                    this.buttonSave.Enabled = true;
                }
            }
        }

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

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

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

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

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

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

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

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalSubnetMask4.Text = myKeyPad.StringValue;
                    this.buttonSave.Enabled = true;
                }
            }
        }

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

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

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

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

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

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

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

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalGateway4.Text = myKeyPad.StringValue;
                    this.buttonSave.Enabled = true;
                }
            }
        }

        private void radioButtonLocalIpDHCP_Click(object sender, EventArgs e)
        {
            string before = "", after = "";
            DialogFormYesNo myDlg;

            if (this.ParentForm.ParentForm.smartConfigs.IPSettings.DHCPEnable == 0)
                before = "Static";
            else
                before = "DHCP";

            if (this.radioButtonLocalIpStatic.Checked == true)
                myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig.Language, 21); // 로컬IP주소 수동으로 설정하시겠습니까?
            else
                myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig.Language, 22); // 로컬IP주소 자동으로 설정하시겠습니까?

            if (myDlg.ShowDialog() == DialogResult.Yes)
            {
                if (this.radioButtonLocalIpDHCP.Checked == true)
                {
                    this.ParentForm.ParentForm.smartConfigs.IPSettings.DHCPEnable = 1;

                    this.LocalIPEnable(false);
                    this.UpdateLocalIPAddressDisplay();
                }
                else
                {
                    this.ParentForm.ParentForm.smartConfigs.IPSettings.DHCPEnable = 0;

                    this.LocalIPEnable(true);

                    try
                    {
                        this.UpdateLocalIPAddressDisplay();
                    }
                    catch
                    {
                        this.ParentForm.ParentForm.smartConfigs.IPSettings.DeviceIP = "192.168.3.10";
                        this.ParentForm.ParentForm.smartConfigs.IPSettings.SubNetMask = "255.255.255.0";
                        this.ParentForm.ParentForm.smartConfigs.IPSettings.GateWay = "192.168.3.1";

                        this.UpdateLocalIPAddressDisplay();
                    }
                }

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

                //if (this.ParentForm.ParentForm.smartConfigs.IPSettings.DHCPEnable == 0)
                //    after = "Static";
                //else
                //    after = "DHCP";

                //this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.TCPLocalIPConfig, "", before, after);
            }
            else
            {
                this.DisplayRefresh();
            }
        }

        private void buttonAddon_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        private void labelMACAddress_Click(object sender, EventArgs e)
        {
            SmartX.SmartLabel label = sender as SmartX.SmartLabel;

            DialogFormHexKeyPad myKeyPad = new DialogFormHexKeyPad(label.Text, 2, this.ParentForm.ParentForm.SystemConfig.Language);
            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                label.Text = myKeyPad.StringValue;
                this.buttonSave2.Enabled = true;
            }
        }
        private void buttonSave2_Click(object sender, EventArgs e)
        {
            byte[] changeMAC = new byte[6];

            changeMAC[0] = Convert.ToByte(this.HexArray[0], 16);
            changeMAC[1] = Convert.ToByte(this.HexArray[1], 16);
            changeMAC[2] = Convert.ToByte(this.HexArray[2], 16);
            changeMAC[3] = Convert.ToByte(this.labelMACAddress4.Text, 16);
            changeMAC[4] = Convert.ToByte(this.labelMACAddress5.Text, 16);
            changeMAC[5] = Convert.ToByte(this.labelMACAddress6.Text, 16);

            this.ParentForm.ParentForm.smartConfigs.IPSettings.SetMACAddress(changeMAC);

            this.buttonSave2.Enabled = false;
        }
        #endregion

        private void comboBoxSelectLane_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
