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.Forms;
using ITC81DB_ImageDll;
using ITC81DB.DialogForms;

namespace ITC81DB.Controls
{
    public partial class ControlConfiEthernetLocalIP : UserControl
    {
        #region Field
        private FormMenu m_ParentForm; 
        #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 == DataStore.LanguageID.English)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korBackgroundBottomEthernet));

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

                this.labelTitleIPConfiguration.Text = "IP Configuration";
                this.labelTitleIPAddress.Text = "IP Address";
                this.labelTitleSubnetMask.Text = "Subnet mask";
                this.labelTitleGateway.Text = "Gateway";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackgroundBottomEthernet));

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

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

                this.labelTitleIPConfiguration.Text = "IP选项";
                this.labelTitleIPAddress.Text = "IP地址";
                this.labelTitleSubnetMask.Text = "子网屏蔽码";
                this.labelTitleGateway.Text = "网关";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.rusBackgroundBottomEthernet));
                this.labelHelp.Text = "※ Изменив IP-конфигурацию, после перезагрузки соединение будет нормальным";

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

                this.labelTitleIPConfiguration.Text = "Настройки";
                this.labelTitleIPAddress.Text = "IP адрес";
                this.labelTitleSubnetMask.Text = "Маска подсети";
                this.labelTitleGateway.Text = "Шлюз";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerBackgroundBottomEthernet));

                this.labelHelp.Text = "※ Wenn die IP-Konfiguration geändert wird, wird die Verbindung nach dem Neustart normal hergestellt.";

                this.labelTitleIPConfiguration.Text = "IP Konfiguration";
                this.labelTitleIPAddress.Text = "IP-Adresse";
                this.labelTitleSubnetMask.Text = "Subnet mask";
                this.labelTitleGateway.Text = "Gateway";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Spanish)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korBackgroundBottomEthernet));

                this.labelHelp.Text = "※ Cuando se cambia la configuración de IP, normalmente está conectada después de reiniciar.";

                this.labelTitleIPConfiguration.Text = "IP Configuration";
                this.labelTitleIPAddress.Text = "IP Address";
                this.labelTitleSubnetMask.Text = "Subnet mask";
                this.labelTitleGateway.Text = "Gateway";
            }
            else
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korBackgroundBottomEthernet));
                //this.labelMessage1.Text = "로컬 데이터 셋팅 후 재부팅 하십시오.";
                this.labelHelp.Text = "※ IP Configuration 변경 시, 재부팅 후 정상적으로 연결됩니다.";

                this.labelTitleIPConfiguration.Text = "IP Configuration";
                this.labelTitleIPAddress.Text = "IP Address";
                this.labelTitleSubnetMask.Text = "Subnet mask";
                this.labelTitleGateway.Text = "Gateway";
            }
        }
        private void DefaultSetting()
        {
            this.buttonSave1.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);
            }
        }

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

            // Local IP
            this.UpdateLocalIPAddressDisplay();
        }
        #endregion

        #region Event Handler
        private void buttonSave1_Click(object sender, EventArgs e)
        {
            if(this.ParentForm.ParentForm.SystemConfig2.EthernetOperationMode == 1)
                this.ParentForm.ParentForm.EthernetServerStop();
            else if (this.ParentForm.ParentForm.SystemConfig2.EthernetOperationMode == 2)
                this.ParentForm.ParentForm.EthernetClientDisconnect();

            this.buttonSave1.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);

            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();
        }

        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.buttonSave1.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.buttonSave1.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.buttonSave1.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.buttonSave1.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.buttonSave1.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.buttonSave1.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.buttonSave1.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.buttonSave1.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.buttonSave1.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.buttonSave1.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.buttonSave1.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.buttonSave1.Enabled = true;
                }
            }
        }

        private void radioButtonLocalIpDHCP_Click(object sender, EventArgs e)
        {
            DialogFormYesNo myDlg;

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

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

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

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

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

                    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();
            }
            else
            {
                this.DisplayRefresh();
            }
        }

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