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 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.SystemConfig.Language == Define.E_LanguageID.English)
            {
                this.labelTitleAddon.Text = "Add-on";
                this.labelDiscription2.Text = "- Master(Client) : 16(Write on judgment), Slave(Server) : 04(Send on Master request)";
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Chinese)
            {
                this.labelTitleAddon.Text = "附加功能";
                this.labelDiscription2.Text = "- Master(Client) : 16(Write on judgment), Slave(Server) : 04(Send on Master request)";
            }
            else if (this.ParentForm.ParentForm.SystemConfig.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.SystemConfig.Language == Define.E_LanguageID.Russian)
            {
                this.labelTitleAddon.Text = "Дополнения";
                this.labelDiscription2.Text = "- Master(Client) : 16(Write on judgment), Slave(Server) : 04(Send on Master request)";
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.German)
            {
                this.labelTitleAddon.Text = "Erweiterung";
                this.labelDiscription2.Text = "- Master(Client) : 16(Write on judgment), Slave(Server) : 04(Send on Master request)";
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
            {
                this.labelTitleAddon.Text = "Complementos";
                this.labelDiscription2.Text = "- Master(Client) : 16(Write on judgment), Slave(Server) : 04(Send on Master request)";
            }
            else
            {
                this.labelTitleAddon.Text = "추가설정";
                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.ParentForm.ParentForm.SystemConfig.ModbusRTUSelectFunction = (int)Define.E_ModbusFunction._04_ReadInputRegister;
            this.comboBoxRole.Enabled = false;
            this.ParentForm.ParentForm.SystemConfig.ModbusRTUSelectFunction = 0;
            if (this.ParentForm.CurrentConfigurationMenu() == Define.E_MenuBottomConfiguration.SerialCOM1)
                this.ParentForm.ParentForm.SystemConfig.ModbusRTUSlaveIDCOM1 = 1;
            else if (this.ParentForm.CurrentConfigurationMenu() == Define.E_MenuBottomConfiguration.SerialCOM3)
                this.ParentForm.ParentForm.SystemConfig.ModbusRTUSlaveIDCOM3 = 1;
            else if (this.ParentForm.CurrentConfigurationMenu() == Define.E_MenuBottomConfiguration.SerialCOM4)
                this.ParentForm.ParentForm.SystemConfig.ModbusRTUSlaveIDCOM4 = 1;
            this.labelSlaveID.Text = "1";
            this.labelSlaveID.Enabled = false;
            this.ParentForm.ParentForm.SaveSystemConfigurationFile2(this.ParentForm.ParentForm.SystemConfig);

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

            this.SlaveIDControlVisible(true);
            this.StartAddressControlVisible(false);
            this.UsingConverterControlVisible(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);
            this.UsingConverterControlVisible(true);
            if (menu == Define.E_MenuBottomConfiguration.SerialCOM1)
                {
                    this.labelSlaveID.Text = this.ParentForm.ParentForm.SystemConfig.ModbusRTUSlaveIDCOM1.ToString();
                    this.labelStartAddress.Text = this.ParentForm.ParentForm.SystemConfig.ModbusRTUStartAddressCOM1.ToString();
                    if (this.ParentForm.ParentForm.SystemConfig.IsModbusRTUUsingConverterCOM1 == true)
                        this.buttonUsingConverter.ButtonDown();
                    else
                        this.buttonUsingConverter.ButtonUp();
                }
                else if (menu == Define.E_MenuBottomConfiguration.SerialCOM3)
                {
                    this.labelSlaveID.Text = this.ParentForm.ParentForm.SystemConfig.ModbusRTUSlaveIDCOM3.ToString();
                    this.labelStartAddress.Text = this.ParentForm.ParentForm.SystemConfig.ModbusRTUStartAddressCOM3.ToString();
                    if (this.ParentForm.ParentForm.SystemConfig.IsModbusRTUUsingConverterCOM3 == true)
                        this.buttonUsingConverter.ButtonDown();
                    else
                        this.buttonUsingConverter.ButtonUp();
                }
                else if (menu == Define.E_MenuBottomConfiguration.SerialCOM4)
                {
                    this.labelSlaveID.Text = this.ParentForm.ParentForm.SystemConfig.ModbusRTUSlaveIDCOM4.ToString();
                    this.labelStartAddress.Text = this.ParentForm.ParentForm.SystemConfig.ModbusRTUStartAddressCOM4.ToString();
                    if (this.ParentForm.ParentForm.SystemConfig.IsModbusRTUUsingConverterCOM4 == true)
                        this.buttonUsingConverter.ButtonDown();
                    else
                        this.buttonUsingConverter.ButtonUp();
                }
        }
        public void ModbusTCP04Addon()
        {
            this.labelFunctions.Text = "04: Read input register";

            this.SlaveIDControlVisible(false);
            this.StartAddressControlVisible(false);
            this.UsingConverterControlVisible(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.UsingConverterControlVisible(false);
            this.labelStartAddress.Text = this.ParentForm.ParentForm.SystemConfig.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 UsingConverterControlVisible(bool visible)
        {
            if (visible == true)
            {
                this.labelTitleUsingConverter.Visible = true;
                this.buttonUsingConverter.Visible = true;
            }
            else
            {
                this.labelTitleUsingConverter.Visible = false;
                this.buttonUsingConverter.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)
            {
                this.comboBoxRole.SelectedIndex = this.ParentForm.ParentForm.SystemConfig.ModbusTCPSelectFunction;

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

                this.labelDiscription1.Visible = false;
                this.comboBoxRole.Enabled = false;
            }
            else
            {
                this.comboBoxRole.SelectedIndex = this.ParentForm.ParentForm.SystemConfig.ModbusRTUSelectFunction;
                this.comboBoxRole.Enabled = true;

                if (this.ParentForm.ParentForm.SystemConfig.ModbusRTUSelectFunction == (int)Define.E_ModbusFunction._04_ReadInputRegister)
                    this.ModbusRTU04Addon();
                else
                    this.ModbusRTU16Addon(menu);

                this.labelDiscription1.Visible = true;
            }

            this.ParentForm.ParentForm.SaveSystemConfigurationFile2(this.ParentForm.ParentForm.SystemConfig);
            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.SystemConfig.Language);

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

                    if (this.ParentForm.CurrentConfigurationMenu() == Define.E_MenuBottomConfiguration.SerialCOM1)
                    {
                        before = this.ParentForm.ParentForm.SystemConfig.ModbusRTUSlaveIDCOM1.ToString();
                        this.ParentForm.ParentForm.SystemConfig.ModbusRTUSlaveIDCOM1 = 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.SystemConfig.ModbusRTUSlaveIDCOM3.ToString();
                        this.ParentForm.ParentForm.SystemConfig.ModbusRTUSlaveIDCOM3 = 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.SystemConfig.ModbusRTUSlaveIDCOM4.ToString();
                        this.ParentForm.ParentForm.SystemConfig.ModbusRTUSlaveIDCOM4 = 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.SystemConfig);
                }
            }
        }
        private void labelStartAddress_Click(object sender, EventArgs e)
        {
            string before = "", after = "";

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

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

                    if (this.ParentForm.CurrentConfigurationMenu() == Define.E_MenuBottomConfiguration.SerialCOM1)
                    {
                        before = this.ParentForm.ParentForm.SystemConfig.ModbusRTUStartAddressCOM1.ToString();
                        this.ParentForm.ParentForm.SystemConfig.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.SystemConfig.ModbusRTUStartAddressCOM3.ToString();
                        this.ParentForm.ParentForm.SystemConfig.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.SystemConfig.ModbusRTUStartAddressCOM4.ToString();
                        this.ParentForm.ParentForm.SystemConfig.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.SystemConfig.ModbusTCPStartAddress.ToString();
                        this.ParentForm.ParentForm.SystemConfig.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.SystemConfig);
                }
            }
        }
        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.SystemConfig.ModbusTCPSelectFunction.ToString();

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

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

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

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

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

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

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

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

            this.ParentForm.ParentForm.SaveSystemConfigurationFile2(this.ParentForm.ParentForm.SystemConfig);
        }
        private void buttonUsingConverter_Click(object sender, EventArgs e)
        {
            Define.E_MenuBottomConfiguration temp = this.ParentForm.CurrentConfigurationMenu();
            if (this.buttonUsingConverter.ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
            {
                if (temp == Define.E_MenuBottomConfiguration.SerialCOM1)
                {
                    this.ParentForm.ParentForm.SystemConfig.IsModbusRTUUsingConverterCOM1 = true;
                    //this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingOperation.Mod_UsingConverter, "COM1 - ON");
                }
                else if (temp == Define.E_MenuBottomConfiguration.SerialCOM3)
                {
                    this.ParentForm.ParentForm.SystemConfig.IsModbusRTUUsingConverterCOM3 = true;
                    //this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingOperation.Mod_UsingConverter, "COM3 - ON");
                }
                else if (temp == Define.E_MenuBottomConfiguration.SerialCOM4)
                {
                    this.ParentForm.ParentForm.SystemConfig.IsModbusRTUUsingConverterCOM4 = true;
                    //this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingOperation.Mod_UsingConverter, "COM4 - ON");
                }
            }
            else
            {
                if (temp == Define.E_MenuBottomConfiguration.SerialCOM1)
                {
                    this.ParentForm.ParentForm.SystemConfig.IsModbusRTUUsingConverterCOM1 = false;
                    //this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingOperation.Mod_UsingConverter, "COM1 - OFF");
                }
                else if (temp == Define.E_MenuBottomConfiguration.SerialCOM3)
                {
                    this.ParentForm.ParentForm.SystemConfig.IsModbusRTUUsingConverterCOM3 = false;
                    //this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingOperation.Mod_UsingConverter, "COM3 - OFF");
                }
                else if (temp == Define.E_MenuBottomConfiguration.SerialCOM4)
                {
                    this.ParentForm.ParentForm.SystemConfig.IsModbusRTUUsingConverterCOM4 = false;
                    //this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingOperation.Mod_UsingConverter, "COM4 - OFF");
                }
            }

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

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

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 0 || myKeyPad.doubleValue > 9999)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.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
    }
}
