using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using INT_PT002.DataStore;
using INT_PT002.Forms;
using SmartX;

namespace INT_PT002.Controls
{
    public partial class ControlMainModbus : UserControl
    {
        #region Field
        private FormMainDisplay3 m_ParentForm;
        private UInt32 ReceiveCount;
        private static int ModbusCount = 20;

        private Collection<SmartLabel> CollectionOffset;
        private Collection<SmartLabel> CollectionContents;
        private Collection<SmartLabel> CollectionDec;
        private Collection<SmartLabel> CollectionHexa;
        #endregion

        #region Constructor
        public ControlMainModbus(FormMainDisplay3 parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

            this.DefaultSetting();
            this.InitializeDesign();
        }
        #endregion

        #region Property
        public FormMainDisplay3 ParentForm
        {
            get { return this.m_ParentForm; }
            private set { this.m_ParentForm = value; }
        }
        #endregion

        #region Method
        public void InitializeDesign()
        {
            if (this.ParentForm.ParentForm.SystemConfig.LANGUAGE == Define.E_LanguageID.English)
            {
                #region 영문
                this.labelTitlePort.Text = "Port";
                this.labelTitleStartAddr1.Text = "Start";
                this.labelTitleStartAddr2.Text = "Address";
                this.labelTitleReceiveCount1.Text = "Receive";
                this.labelTitleReceiveCount2.Text = "Count";

                this.labelTitleOffset.Text = "Offset";
                this.labelTitleContents.Text = "Contents";
                this.labelTitleValueDecimal.Text = "Value(Decimal)";
                this.labelTitleValueHexa.Text = "Value(Hexa)";

                for (int i = 1; i <= ModbusCount; i++)
                    this.CollectionOffset[i - 1].Font = new Font("새굴림", 9, FontStyle.Regular);

                for (int i = 1; i <= ModbusCount; i++)
                {
                    if (i % 2 == 1)
                        this.CollectionContents[i - 1].Text = "Processing";
                    else
                        this.CollectionContents[i - 1].Text = "Result";
                }
                #endregion
            }
            else
            {
                #region 한글
                this.labelTitlePort.Text = "Port";
                this.labelTitleStartAddr1.Text = "Start";
                this.labelTitleStartAddr2.Text = "Address";
                this.labelTitleReceiveCount1.Text = "Receive";
                this.labelTitleReceiveCount2.Text = "Count";

                this.labelTitleOffset.Text = "Offset";
                this.labelTitleContents.Text = "항목";
                this.labelTitleValueDecimal.Text = "값(Decimal)";
                this.labelTitleValueHexa.Text = "값(Hexa)";

                for (int i = 1; i <= ModbusCount; i++)
                    this.CollectionOffset[i - 1].Font = new Font("새굴림", 9, FontStyle.Regular);

                for (int i = 1; i <= ModbusCount; i++)
                {
                    if (i % 2 == 1)
                        this.CollectionContents[i - 1].Text = "진행 여부";
                    else
                        this.CollectionContents[i - 1].Text = "결과";
                }
                #endregion
            }
        }
        private void DefaultSetting()
        {
            this.CollectionOffset = new Collection<SmartLabel>();
            this.CollectionContents = new Collection<SmartLabel>();
            this.CollectionDec = new Collection<SmartLabel>();
            this.CollectionHexa = new Collection<SmartLabel>();

            for (int i = 1; i <= ModbusCount; i++)
            {
                this.CollectionOffset.Add(FindByNameUtil.SmartLabel("labelOffset" + i, this));
                this.CollectionContents.Add(FindByNameUtil.SmartLabel("labelContents" + i, this));
                this.CollectionDec.Add(FindByNameUtil.SmartLabel("labelValueDec" + i, this));
                this.CollectionHexa.Add(FindByNameUtil.SmartLabel("labelValueHexa" + i, this));
                //Console.Write("\n" + string.Format("DefaultSetting {0:yyyy-MM-dd HH:mm:ss}: {1}", DateTime.Now, 4));
            }

            this.ReceiveCount = 0;
            this.labelReceiveCount.Text = this.ReceiveCount.ToString();
        }

        public void Clear()
        {
            this.UpdateData(this.ParentForm.ParentForm.Current30000ModbusData);
        }
        public void UpdateEquipmentStatusDisplay(Define.E_EquipmentStatus status)
        {

        }
        public void UpdateReceiveCount()
        {
            if (this.ReceiveCount > 999999999)
                this.ReceiveCount = 0;
            else
            {
                this.ReceiveCount++;
                this.labelReceiveCount.Text = this.ReceiveCount.ToString();
            }
        }
        public void UpdateData(_30000_ModbusData mData)
        {
            this.CollectionDec[0].Text = mData._1_IsProcessing.ToString();
            this.CollectionDec[1].Text = mData._1_IsLeak.ToString();
            this.CollectionDec[2].Text = mData._2_IsProcessing.ToString();
            this.CollectionDec[3].Text = mData._2_IsLeak.ToString();
            this.CollectionDec[4].Text = mData._3_IsProcessing.ToString();
            this.CollectionDec[5].Text = mData._3_IsLeak.ToString();
            this.CollectionDec[6].Text = mData._4_IsProcessing.ToString();
            this.CollectionDec[7].Text = mData._4_IsLeak.ToString();
            this.CollectionDec[8].Text = mData._5_IsProcessing.ToString();
            this.CollectionDec[9].Text = mData._5_IsLeak.ToString();
            this.CollectionDec[10].Text = mData._6_IsProcessing.ToString();
            this.CollectionDec[11].Text = mData._6_IsLeak.ToString();
            this.CollectionDec[12].Text = mData._7_IsProcessing.ToString();
            this.CollectionDec[13].Text = mData._7_IsLeak.ToString();
            this.CollectionDec[14].Text = mData._8_IsProcessing.ToString();
            this.CollectionDec[15].Text = mData._8_IsLeak.ToString();
            this.CollectionDec[16].Text = mData._9_IsProcessing.ToString();
            this.CollectionDec[17].Text = mData._9_IsLeak.ToString();
            this.CollectionDec[18].Text = mData._10_IsProcessing.ToString();
            this.CollectionDec[19].Text = mData._10_IsLeak.ToString();

            this.CollectionHexa[0].Text = mData.HexString_1_IsProcessing;
            this.CollectionHexa[1].Text = mData.HexString_1_IsLeak;
            this.CollectionHexa[2].Text = mData.HexString_2_IsProcessing;
            this.CollectionHexa[3].Text = mData.HexString_2_IsLeak;
            this.CollectionHexa[4].Text = mData.HexString_3_IsProcessing;
            this.CollectionHexa[5].Text = mData.HexString_3_IsLeak;
            this.CollectionHexa[6].Text = mData.HexString_4_IsProcessing;
            this.CollectionHexa[7].Text = mData.HexString_4_IsLeak;
            this.CollectionHexa[8].Text = mData.HexString_5_IsProcessing;
            this.CollectionHexa[9].Text = mData.HexString_5_IsLeak;
            this.CollectionHexa[10].Text = mData.HexString_6_IsProcessing;
            this.CollectionHexa[11].Text = mData.HexString_6_IsLeak;
            this.CollectionHexa[12].Text = mData.HexString_7_IsProcessing;
            this.CollectionHexa[13].Text = mData.HexString_7_IsLeak;
            this.CollectionHexa[14].Text = mData.HexString_8_IsProcessing;
            this.CollectionHexa[15].Text = mData.HexString_8_IsLeak;
            this.CollectionHexa[16].Text = mData.HexString_9_IsProcessing;
            this.CollectionHexa[17].Text = mData.HexString_9_IsLeak;
            this.CollectionHexa[18].Text = mData.HexString_10_IsProcessing;
            this.CollectionHexa[19].Text = mData.HexString_10_IsLeak;
        }
        public void UpdateOffset(bool master, int startAddress)
        {
            if (master == true)
            {
                for (int i = 0; i < ModbusCount; i++)
                {
                    this.CollectionOffset[i].Text = (3000 + startAddress).ToString();
                    startAddress += 2;
                }
            }
            else
            {
                int address = 3010;
                for (int i = 0; i < ModbusCount; i++)
                {
                    this.CollectionOffset[i].Text = "0x" + address.ToString("X4");
                    address += 2;
                }
            }
        }
        public void UpdateDisplay()
        {
            if (this.ParentForm.ParentForm.SystemConfig.MODBUS_TCP_SELECTFUNCTION == (int)Define.E_ModbusFunction._04_ReadInputRegister)
            {
                this.labelStartAddress.Text = "-";
                this.UpdateOffset(false, this.ParentForm.ParentForm.SystemConfig.MODBUS_TCP_START_ADDRESS);
            }
            else
            {
                this.labelStartAddress.Text = this.ParentForm.ParentForm.SystemConfig.MODBUS_TCP_START_ADDRESS.ToString();
                this.UpdateOffset(true, this.ParentForm.ParentForm.SystemConfig.MODBUS_TCP_START_ADDRESS);
            }
        }

        public void DisplayRefresh()
        {
            this.UpdateDisplay();
            this.UpdateData(this.ParentForm.ParentForm.Current30000ModbusData);
        }
        #endregion

        #region Event Handler
        #endregion
    }
}
