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_2H.Forms;
using ITC81DB_2H_ImageDll;
using ITC81DB_2H.DialogForms;
using ITC81DB_2H.Datastore;

namespace ITC81DB_2H.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.SystemConfig1.Language == Define.E_LanguageID.English)
            {
                this.labelHelp.Text = "※ After setup, reboot is required.";

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

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

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

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

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

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

                this.labelTitleIPAddress.Text = "IP Address";
                this.labelTitleSubnetMask.Text = "Subnet mask";
                this.labelTitleGateway.Text = "Gateway";
            }
            else
            {
                this.labelHelp.Text = "※ 설정 후, 재부팅 필수";

                this.labelTitleIPAddress.Text = "IP Address";
                this.labelTitleSubnetMask.Text = "Subnet mask";
                this.labelTitleGateway.Text = "Gateway";
            }
        }
        private void DefaultSetting()
        {
            if (this.ParentForm.ParentForm.smartConfigs.IPSettings.DHCPEnable != 0)
            {
                this.ParentForm.ParentForm.smartConfigs.IPSettings.DHCPEnable = 0;
                this.ParentForm.ParentForm.smartConfigs.IPSettings.Save();
                this.ParentForm.ParentForm.smartConfigs.IPSettings.SetApply();
            }

            this.buttonSave.Enabled = false;
        }

        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 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()
        {
            try
            {
                // Local IP
                this.UpdateLocalIPAddressDisplay();
            }
            catch
            {
                this.ParentForm.ParentForm.smartConfigs.IPSettings.DeviceIP = "192.168.11.205";
                this.ParentForm.ParentForm.smartConfigs.IPSettings.SubNetMask = "255.255.255.0";
                this.ParentForm.ParentForm.smartConfigs.IPSettings.GateWay = "192.168.11.1";
            }

            // 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.SystemConfig2.EthernetOperationMode == 1)
                this.ParentForm.ParentForm.EthernetServerStop();
            else if (this.ParentForm.ParentForm.SystemConfig2.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.SystemConfig2.EthernetOperationMode == 1)
                this.ParentForm.ParentForm.EthernetServerStart();
            else if (this.ParentForm.ParentForm.SystemConfig2.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.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.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.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.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.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.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.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.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.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.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.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.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.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.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.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.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.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.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.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.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.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.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.SystemConfig1.Language);

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

        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.SystemConfig1.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
    }
}
