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
{
    public partial class ControlConfiModbus : UserControl
    {
        #region Field
        private FormMenu m_ParentForm; 
        #endregion

        #region Constructor
        public ControlConfiModbus(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.engBackgroundBottomAddOn));
                this.labelDiscription2.Text = "- Master(Client) : 16(Write on judgment), Slave(Server) : 04(Send on Master request)";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackgroundBottomAddOn));
                this.labelDiscription2.Text = "- Master(Client) : 16(Write on judgment), Slave(Server) : 04(Send on Master request)";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
            {
                this.labelDiscription2.Text = "- Master(Client) : 16(Write on judgment), Slave(Server) : 04(Send on Master request)";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.rusBackgroundBottomAddOn));
                this.labelDiscription2.Text = "- Master(Client) : 16(Write on judgment), Slave(Server) : 04(Send on Master request)";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerBackgroundBottomAddOn));
                this.labelDiscription2.Text = "- Master(Client) : 16(Write on judgment), Slave(Server) : 04(Send on Master request)";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Spanish)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engBackgroundBottomAddOn));
                this.labelDiscription2.Text = "- Master(Client) : 16(Write on judgment), Slave(Server) : 04(Send on Master request)";
            }
            else
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korBackgroundBottomAddOn));
                this.labelDiscription2.Text = "- Master(Client) : 16(중량판정 시), Slave(Server) : 04(Master 읽기 요청 시)";
            }
        }
        private void DefaultSetting()
        {
            this.comboBoxRole.Items.Clear();
            this.comboBoxRole.Items.Add("Slave");
            this.comboBoxRole.Items.Add("Master");
        }

        public void SerialDefaultSettings()
        {
            this.ParentForm.ParentForm.ChildFormMainDisplay.MainBottomModbusIconAdd(false);
        }
        public void ModbusRTUDefaultSettings()
        {
            this.comboBoxRole.Enabled = true;
            this.labelSlaveID.Enabled = true;

            this.ParentForm.ParentForm.ChildFormMainDisplay.MainBottomModbusIconAdd(true);
        }
        public void OPCSettings()
        {
            this.comboBoxRole.SelectedIndexChanged -= new EventHandler(this.comboBoxRole_SelectedIndexChanged);
            this.comboBoxRole.SelectedIndex = (int)DataStore.ModbusFunction._04_ReadInputRegister;
            this.comboBoxRole.SelectedIndexChanged += new EventHandler(this.comboBoxRole_SelectedIndexChanged);
            this.comboBoxRole.Enabled = false;
            this.ParentForm.ParentForm.SystemConfig2.ModbusRTUSelectFunction = 0;
            if (this.ParentForm.CurrentConfigurationMenu() == DataStore.MenuBottomConfiguration.SerialCOM1)
                this.ParentForm.ParentForm.SystemConfig2.ModbusSlaveIDCOM1 = 1;
            else if (this.ParentForm.CurrentConfigurationMenu() == DataStore.MenuBottomConfiguration.SerialCOM3)
                this.ParentForm.ParentForm.SystemConfig2.ModbusSlaveIDCOM3 = 1;
            else if (this.ParentForm.CurrentConfigurationMenu() == DataStore.MenuBottomConfiguration.SerialCOM4)
                this.ParentForm.ParentForm.SystemConfig2.ModbusSlaveIDCOM4 = 1;
            this.labelSlaveID.Text = "1";
            this.labelSlaveID.Enabled = false;
            this.ParentForm.ParentForm.SaveSystemConfigurationFile2(this.ParentForm.ParentForm.SystemConfig2);

            this.ParentForm.ParentForm.ChildFormMainDisplay.MainBottomModbusIconAdd(true);
        }
        public void ModbusRTU04Addon()
        {
            this.labelFunctions.Text = "04: Read input register";

            this.SlaveIDControlVisible(true);
            this.StartAddressControlVisible(false);
        }
        public void ModbusRTU16Addon(DataStore.MenuBottomConfiguration menu)
        {
            this.labelFunctions.Text = "16: Write multiple register";

            this.labelTitleStartAddress.Location = new Point(134, 116);
            this.labelStartAddress.Location = new Point(288, 116);
            this.SlaveIDControlVisible(true);
            this.StartAddressControlVisible(true);
            if (menu == DataStore.MenuBottomConfiguration.SerialCOM1)
                {
                    this.labelSlaveID.Text = this.ParentForm.ParentForm.SystemConfig2.ModbusSlaveIDCOM1.ToString();
                    this.labelStartAddress.Text = this.ParentForm.ParentForm.SystemConfig3.ModbusRTUStartAddressCOM1.ToString();
                }
                else if (menu == DataStore.MenuBottomConfiguration.SerialCOM3)
                {
                    this.labelSlaveID.Text = this.ParentForm.ParentForm.SystemConfig2.ModbusSlaveIDCOM3.ToString();
                    this.labelStartAddress.Text = this.ParentForm.ParentForm.SystemConfig3.ModbusRTUStartAddressCOM3.ToString();
                }
                else if (menu == DataStore.MenuBottomConfiguration.SerialCOM4)
                {
                    this.labelSlaveID.Text = this.ParentForm.ParentForm.SystemConfig2.ModbusSlaveIDCOM4.ToString();
                    this.labelStartAddress.Text = this.ParentForm.ParentForm.SystemConfig3.ModbusRTUStartAddressCOM4.ToString();
                }
        }
        public void ModbusTCP04Addon()
        {
            this.labelFunctions.Text = "04: Read input register";

            this.SlaveIDControlVisible(false);
            this.StartAddressControlVisible(false);
        }
        public void ModbusTCP16Addon()
        {
            this.labelFunctions.Text = "16: Write multiple register";

            this.labelTitleStartAddress.Location = new Point(134, 80);
            this.labelStartAddress.Location = new Point(288, 80);
            this.SlaveIDControlVisible(false);
            this.StartAddressControlVisible(true);
            this.labelStartAddress.Text = this.ParentForm.ParentForm.SystemConfig3.ModbusTCPStartAddress.ToString();
        }

        private void SlaveIDControlVisible(bool visible)
        {
            if (visible == true)
            {
                this.labelTitleSlaveID.Visible = true;
                this.labelSlaveID.Visible = true;
            }
            else
            {
                this.labelTitleSlaveID.Visible = false;
                this.labelSlaveID.Visible = false;
            }
        }
        private void StartAddressControlVisible(bool visible)
        {
            if (visible == true)
            {
                this.labelTitleStartAddress.Visible = true;
                this.labelStartAddress.Visible = true;
            }
            else
            {
                this.labelTitleStartAddress.Visible = false;
                this.labelStartAddress.Visible = false;
            }
        }

        public void DisplayRefresh(DataStore.MenuBottomConfiguration menu)
        {
            this.comboBoxRole.SelectedIndexChanged -= new EventHandler(this.comboBoxRole_SelectedIndexChanged);
            if (menu == DataStore.MenuBottomConfiguration.Ethernet)
            {
                if (this.ParentForm.ParentForm.SystemConfig2.EthernetCommMode == (int)DataStore.EthernetMode.f16_OPC)
                {
                    this.ParentForm.ParentForm.SystemConfig2.ModbusTCPSelectFunction = (int)DataStore.ModbusFunction._04_ReadInputRegister;
                    this.comboBoxRole.SelectedIndex = this.ParentForm.ParentForm.SystemConfig2.ModbusTCPSelectFunction;
                    this.ModbusTCP04Addon();
                }
                else
                {
                    if (this.ParentForm.ParentForm.SystemConfig2.EthernetOperationMode == 1)
                        this.ParentForm.ParentForm.SystemConfig2.ModbusTCPSelectFunction = (int)DataStore.ModbusFunction._04_ReadInputRegister;
                    else if (this.ParentForm.ParentForm.SystemConfig2.EthernetOperationMode == 2)
                        this.ParentForm.ParentForm.SystemConfig2.ModbusTCPSelectFunction = (int)DataStore.ModbusFunction._16_WriteMultipleRegister;
                    this.comboBoxRole.SelectedIndex = this.ParentForm.ParentForm.SystemConfig2.ModbusTCPSelectFunction;

                    if (this.ParentForm.ParentForm.SystemConfig2.ModbusTCPSelectFunction == (int)DataStore.ModbusFunction._04_ReadInputRegister)
                        this.ModbusTCP04Addon();
                    else
                        this.ModbusTCP16Addon();
                }

                this.labelDiscription1.Visible = false;
                this.comboBoxRole.Enabled = false;
            }
            else
            {
                if (this.ParentForm.ParentForm.SystemConfig1.SerialCOM1Mode == (int)DataStore.EthernetMode.f16_OPC
                    || this.ParentForm.ParentForm.SystemConfig1.SerialCOM3Mode == (int)DataStore.EthernetMode.f16_OPC
                    || this.ParentForm.ParentForm.SystemConfig1.SerialCOM4Mode == (int)DataStore.EthernetMode.f16_OPC)
                {
                    this.ParentForm.ParentForm.SystemConfig2.ModbusRTUSelectFunction = (int)DataStore.ModbusFunction._04_ReadInputRegister;
                    this.comboBoxRole.SelectedIndex = this.ParentForm.ParentForm.SystemConfig2.ModbusRTUSelectFunction;
                    this.comboBoxRole.Enabled = false;
                    this.ModbusRTU04Addon();
                }
                else
                {
                    this.comboBoxRole.SelectedIndex = this.ParentForm.ParentForm.SystemConfig2.ModbusRTUSelectFunction;
                    this.comboBoxRole.Enabled = true;

                    if (this.ParentForm.ParentForm.SystemConfig2.ModbusRTUSelectFunction == (int)DataStore.ModbusFunction._04_ReadInputRegister)
                        this.ModbusRTU04Addon();
                    else
                        this.ModbusRTU16Addon(menu);
                }
                this.labelDiscription1.Visible = true;
            }
            this.comboBoxRole.SelectedIndexChanged += new EventHandler(this.comboBoxRole_SelectedIndexChanged);
        }        
        #endregion

        #region Event Handler
        // Slave ID는 Modbus TCP에서는 0x01로 고정
        private void labelSlaveID_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelSlaveID.Text, 3, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

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

                    if (this.ParentForm.CurrentConfigurationMenu() == DataStore.MenuBottomConfiguration.SerialCOM1)
                        this.ParentForm.ParentForm.SystemConfig2.ModbusSlaveIDCOM1 = myKeyPad.IntValue;
                    else if (this.ParentForm.CurrentConfigurationMenu() == DataStore.MenuBottomConfiguration.SerialCOM3)
                        this.ParentForm.ParentForm.SystemConfig2.ModbusSlaveIDCOM3 = myKeyPad.IntValue;
                    else if (this.ParentForm.CurrentConfigurationMenu() == DataStore.MenuBottomConfiguration.SerialCOM4)
                        this.ParentForm.ParentForm.SystemConfig2.ModbusSlaveIDCOM4 = myKeyPad.IntValue;

                    this.ParentForm.ParentForm.SaveSystemConfigurationFile2(this.ParentForm.ParentForm.SystemConfig2);
                }
            }
        }

        private void labelStartAddress_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelStartAddress.Text, 5, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

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

                    if (this.ParentForm.CurrentConfigurationMenu() == DataStore.MenuBottomConfiguration.SerialCOM1)
                        this.ParentForm.ParentForm.SystemConfig3.ModbusRTUStartAddressCOM1 = myKeyPad.IntValue;
                    else if (this.ParentForm.CurrentConfigurationMenu() == DataStore.MenuBottomConfiguration.SerialCOM3)
                        this.ParentForm.ParentForm.SystemConfig3.ModbusRTUStartAddressCOM3 = myKeyPad.IntValue;
                    else if (this.ParentForm.CurrentConfigurationMenu() == DataStore.MenuBottomConfiguration.SerialCOM4)
                        this.ParentForm.ParentForm.SystemConfig3.ModbusRTUStartAddressCOM4 = myKeyPad.IntValue;
                    else
                        this.ParentForm.ParentForm.SystemConfig3.ModbusTCPStartAddress = myKeyPad.IntValue;

                    this.ParentForm.ParentForm.SaveSystemConfigurationFile3(this.ParentForm.ParentForm.SystemConfig3);
                }
            }
        }

        private void comboBoxRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataStore.MenuBottomConfiguration menu = this.ParentForm.CurrentConfigurationMenu();

            if (menu == DataStore.MenuBottomConfiguration.Ethernet)
            {
                this.ParentForm.ParentForm.SystemConfig2.ModbusTCPSelectFunction = this.comboBoxRole.SelectedIndex;

                if (this.comboBoxRole.SelectedIndex == (int)DataStore.ModbusFunction._04_ReadInputRegister)
                    this.ModbusTCP04Addon();
                else
                    this.ModbusTCP16Addon();
            }
            else
            {
                this.ParentForm.ParentForm.SystemConfig2.ModbusRTUSelectFunction = this.comboBoxRole.SelectedIndex;

                if (this.comboBoxRole.SelectedIndex == (int)DataStore.ModbusFunction._04_ReadInputRegister)
                    this.ModbusRTU04Addon();
                else
                    this.ModbusRTU16Addon(menu);
            }

            this.ParentForm.ParentForm.SaveSystemConfigurationFile2(this.ParentForm.ParentForm.SystemConfig2);
        }

        private void labelReadTimeout_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelReadTimeout.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

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

                    // 코드 완성 안됨. 추후 사용하게 되면 추가 작업해야함
                }
            }
        }
        #endregion
    }
}
