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 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 == Define.E_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 == Define.E_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 == Define.E_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 == Define.E_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 == Define.E_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 == Define.E_LanguageID.Spanish)
            {

            }
            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)Define.E_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() == Define.E_MenuBottomConfiguration.SerialCOM1)
                this.ParentForm.ParentForm.SystemConfig2.ModbusSlaveIDCOM1 = 1;
            else if (this.ParentForm.CurrentConfigurationMenu() == Define.E_MenuBottomConfiguration.SerialCOM3)
                this.ParentForm.ParentForm.SystemConfig2.ModbusSlaveIDCOM3 = 1;
            else if (this.ParentForm.CurrentConfigurationMenu() == Define.E_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(Define.E_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 == Define.E_MenuBottomConfiguration.SerialCOM1)
                {
                    this.labelSlaveID.Text = this.ParentForm.ParentForm.SystemConfig2.ModbusSlaveIDCOM1.ToString();
                    this.labelStartAddress.Text = this.ParentForm.ParentForm.SystemConfig2.ModbusRTUStartAddressCOM1.ToString();
                }
                else if (menu == Define.E_MenuBottomConfiguration.SerialCOM3)
                {
                    this.labelSlaveID.Text = this.ParentForm.ParentForm.SystemConfig2.ModbusSlaveIDCOM3.ToString();
                    this.labelStartAddress.Text = this.ParentForm.ParentForm.SystemConfig2.ModbusRTUStartAddressCOM3.ToString();
                }
                else if (menu == Define.E_MenuBottomConfiguration.SerialCOM4)
                {
                    this.labelSlaveID.Text = this.ParentForm.ParentForm.SystemConfig2.ModbusSlaveIDCOM4.ToString();
                    this.labelStartAddress.Text = this.ParentForm.ParentForm.SystemConfig2.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.SystemConfig2.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(Define.E_MenuBottomConfiguration menu)
        {
            this.comboBoxRole.SelectedIndexChanged -= new EventHandler(this.comboBoxRole_SelectedIndexChanged);
            if (menu == Define.E_MenuBottomConfiguration.Ethernet)
            {
                if (this.ParentForm.ParentForm.SystemConfig2.EthernetCommMode == (int)Define.E_CommMode.f16_OPC)
                {
                    this.ParentForm.ParentForm.SystemConfig2.ModbusTCPSelectFunction = (int)Define.E_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)Define.E_ModbusFunction._04_ReadInputRegister;
                    else if (this.ParentForm.ParentForm.SystemConfig2.EthernetOperationMode == 2)
                        this.ParentForm.ParentForm.SystemConfig2.ModbusTCPSelectFunction = (int)Define.E_ModbusFunction._16_WriteMultipleRegister;
                    this.comboBoxRole.SelectedIndex = this.ParentForm.ParentForm.SystemConfig2.ModbusTCPSelectFunction;

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

                this.labelDiscription1.Visible = false;
                this.comboBoxRole.Enabled = false;
            }
            else
            {
                if (this.ParentForm.ParentForm.SystemConfig1.SerialCOM1Mode == (int)Define.E_CommMode.f16_OPC
                    || this.ParentForm.ParentForm.SystemConfig1.SerialCOM3Mode == (int)Define.E_CommMode.f16_OPC
                    || this.ParentForm.ParentForm.SystemConfig1.SerialCOM4Mode == (int)Define.E_CommMode.f16_OPC)
                {
                    this.ParentForm.ParentForm.SystemConfig2.ModbusRTUSelectFunction = (int)Define.E_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)Define.E_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)
        {
            string before = "", after = "";

            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() == Define.E_MenuBottomConfiguration.SerialCOM1)
                    {
                        before = this.ParentForm.ParentForm.SystemConfig2.ModbusSlaveIDCOM1.ToString();
                        this.ParentForm.ParentForm.SystemConfig2.ModbusSlaveIDCOM1 = myKeyPad.IntValue;
                        after = myKeyPad.StringValue;

                        if (before != after)
                            this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.Mod_SlaveID, "COM1", before, after);
                    }
                    else if (this.ParentForm.CurrentConfigurationMenu() == Define.E_MenuBottomConfiguration.SerialCOM3)
                    {
                        before = this.ParentForm.ParentForm.SystemConfig2.ModbusSlaveIDCOM3.ToString();
                        this.ParentForm.ParentForm.SystemConfig2.ModbusSlaveIDCOM3 = myKeyPad.IntValue;
                        after = myKeyPad.StringValue;

                        if (before != after)
                            this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.Mod_SlaveID, "COM3", before, after);
                    }
                    else if (this.ParentForm.CurrentConfigurationMenu() == Define.E_MenuBottomConfiguration.SerialCOM4)
                    {
                        before = this.ParentForm.ParentForm.SystemConfig2.ModbusSlaveIDCOM4.ToString();
                        this.ParentForm.ParentForm.SystemConfig2.ModbusSlaveIDCOM4 = myKeyPad.IntValue;
                        after = myKeyPad.StringValue;

                        if (before != after)
                            this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.Mod_SlaveID, "COM4", before, after);
                    }

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

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

            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() == Define.E_MenuBottomConfiguration.SerialCOM1)
                    {
                        before = this.ParentForm.ParentForm.SystemConfig2.ModbusRTUStartAddressCOM1.ToString();
                        this.ParentForm.ParentForm.SystemConfig2.ModbusRTUStartAddressCOM1 = myKeyPad.IntValue;
                        after = myKeyPad.StringValue;

                        if (before != after)
                            this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.Mod_StartAddr, "COM1", before, after);
                    }
                    else if (this.ParentForm.CurrentConfigurationMenu() == Define.E_MenuBottomConfiguration.SerialCOM3)
                    {
                        before = this.ParentForm.ParentForm.SystemConfig2.ModbusRTUStartAddressCOM3.ToString();
                        this.ParentForm.ParentForm.SystemConfig2.ModbusRTUStartAddressCOM3 = myKeyPad.IntValue;
                        after = myKeyPad.StringValue;

                        if (before != after)
                            this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.Mod_StartAddr, "COM3", before, after);
                    }
                    else if (this.ParentForm.CurrentConfigurationMenu() == Define.E_MenuBottomConfiguration.SerialCOM4)
                    {
                        before = this.ParentForm.ParentForm.SystemConfig2.ModbusRTUStartAddressCOM4.ToString();
                        this.ParentForm.ParentForm.SystemConfig2.ModbusRTUStartAddressCOM4 = myKeyPad.IntValue;
                        after = myKeyPad.StringValue;

                        if (before != after)
                            this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.Mod_StartAddr, "COM4", before, after);
                    }
                    else
                    {
                        before = this.ParentForm.ParentForm.SystemConfig2.ModbusTCPStartAddress.ToString();
                        this.ParentForm.ParentForm.SystemConfig2.ModbusTCPStartAddress = myKeyPad.IntValue;
                        after = myKeyPad.StringValue;

                        if (before != after)
                            this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.Mod_StartAddr, "TCP", before, after);
                    }

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

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

            Define.E_MenuBottomConfiguration menu = this.ParentForm.CurrentConfigurationMenu();

            if (menu == Define.E_MenuBottomConfiguration.Ethernet)
            {
                before = this.ParentForm.ParentForm.SystemConfig2.ModbusTCPSelectFunction.ToString();

                this.ParentForm.ParentForm.SystemConfig2.ModbusTCPSelectFunction = this.comboBoxRole.SelectedIndex;

                if (this.comboBoxRole.SelectedIndex == (int)Define.E_ModbusFunction._04_ReadInputRegister)
                    this.ModbusTCP04Addon();
                else
                    this.ModbusTCP16Addon();

                after = this.ParentForm.ParentForm.SystemConfig2.ModbusTCPSelectFunction.ToString();

                if (before != after)
                    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.Mod_Role, "TCP", before, after);
            }
            else
            {
                before = this.ParentForm.ParentForm.SystemConfig2.ModbusRTUSelectFunction.ToString();

                this.ParentForm.ParentForm.SystemConfig2.ModbusRTUSelectFunction = this.comboBoxRole.SelectedIndex;

                if (this.comboBoxRole.SelectedIndex == (int)Define.E_ModbusFunction._04_ReadInputRegister)
                    this.ModbusRTU04Addon();
                else
                    this.ModbusRTU16Addon(menu);

                after = this.ParentForm.ParentForm.SystemConfig2.ModbusRTUSelectFunction.ToString();

                if (before != after)
                    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.Mod_Role, "RTU", before, after);
            }

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

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

            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
                {
                    before = this.labelReadTimeout.Text;
                    after = this.labelReadTimeout.Text = myKeyPad.StringValue;

                    if (before != after)
                        this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.Mod_Timeout, "", before, after);

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