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_LKD_2.DataStore;
using INT_LKD_2.Forms;
using SmartX;

namespace INT_LKD_2.Controls
{
    public partial class ControlMainModbus : UserControl
    {
        #region Field
        private FormMainDisplay m_ParentForm;
        private UInt32 ReceiveCount;
        private int m_ModbusCount;

        private Collection<SmartLabel> CollectionOffset;
        private Collection<SmartLabel> CollectionContents;
        private Collection<SmartLabel> CollectionDecIsProcessing;
        private Collection<SmartLabel> CollectionDecIsLeak;
        private Collection<SmartLabel> CollectionHexaIsProcessing;
        private Collection<SmartLabel> CollectionHexaIsLeak;
        #endregion

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

            this.ParentForm = parent;

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

        #region Property
        public FormMainDisplay ParentForm
        {
            get { return this.m_ParentForm; }
            private set { this.m_ParentForm = value; }
        }
        
        public int ModbusCount
        {
            get { return this.m_ModbusCount; }
            private set { this.m_ModbusCount = value; }
        }
        #endregion

        #region Method
        public void InitializeDesign()
        {
            if (this.ParentForm.ParentForm.SystemConfig.LANGUAGE == Define.E_LanguageID.English)
            {
                #region 영문
                this.labelTitleOffset.Text = "Offset";
                this.labelTitleContents.Text = "Contents";
                this.labelTitleValueDecimal.Text = "Value(Decimal)";
                this.labelTitleValueHexa.Text = "Value(Hexa)";

                for (int i = 1; i <= this.ModbusCount; i++)
                {
                    if (i % 2 == 1)
                        this.CollectionContents[i - 1].Text = "Processing";
                    else
                        this.CollectionContents[i - 1].Text = "Result";
                }
                #endregion
            }
            else
            {
                #region 한글
                this.labelTitleOffset.Text = "Offset";
                this.labelTitleContents.Text = "항목";
                this.labelTitleValueDecimal.Text = "값(Decimal)";
                this.labelTitleValueHexa.Text = "값(Hexa)";

                for (int i = 1; i <= this.ModbusCount; i++)
                {
                    if (i % 2 == 1)
                        this.CollectionContents[i - 1].Text = "진행 여부";
                    else
                        this.CollectionContents[i - 1].Text = "결과";
                }
                #endregion
            }
        }
        private void DefaultSetting()
        {
            this.ModbusCount = this.ParentForm.ParentForm.SystemConfig.EQUIPMENT_LANE * 2;

            this.CollectionOffset = new Collection<SmartLabel>();
            this.CollectionContents = new Collection<SmartLabel>();
            this.CollectionDecIsProcessing = new Collection<SmartLabel>();
            this.CollectionDecIsLeak = new Collection<SmartLabel>();
            this.CollectionHexaIsProcessing = new Collection<SmartLabel>();
            this.CollectionHexaIsLeak = new Collection<SmartLabel>();

            for (int i = 1; i <= this.ModbusCount; i++)
            {
                this.CollectionOffset.Add(FindByNameUtil.SmartLabel("labelOffset" + i, this));
                this.CollectionContents.Add(FindByNameUtil.SmartLabel("labelContents" + i, this));
            }

            for (int i = 1; i <= this.ParentForm.ParentForm.SystemConfig.EQUIPMENT_LANE; i++)
            {
                this.CollectionDecIsProcessing.Add(FindByNameUtil.SmartLabel("labelValueDecIsProcessing" + i, this));
                this.CollectionDecIsLeak.Add(FindByNameUtil.SmartLabel("labelValueDecIsLeak" + i, this));
                this.CollectionHexaIsProcessing.Add(FindByNameUtil.SmartLabel("labelValueHexaIsProcessing" + i, this));
                this.CollectionHexaIsLeak.Add(FindByNameUtil.SmartLabel("labelValueHexaIsLeak" + i, this));
            }

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

        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)
        {
            mData.SettingHexa();

            for (int i = 0; i < this.ParentForm.ParentForm.SystemConfig.EQUIPMENT_LANE; i++)
            {
                this.CollectionDecIsProcessing[i].Text = mData.CollectionIsProcessing[i].ToString();
                this.CollectionDecIsLeak[i].Text = mData.CollectionIsLeak[i].ToString();
                this.CollectionHexaIsProcessing[i].Text = mData.CollectionIsProcessingHexa[i].ToString();
                this.CollectionHexaIsLeak[i].Text = mData.CollectionIsLeakHexa[i].ToString();
            }

            // 로그
            if (this.ParentForm.ParentForm.IsCommunicationLogOpen == true)
                this.ParentForm.ParentForm.smartFileCommunicationLog.StringType.Write(string.Format("UpdateData ({0:yyyy-MM-dd HH:mm:ss})", DateTime.Now));
        }
        public void UpdateOffset(bool master, int startAddress)
        {
            if (master == true)
            {
                for (int i = 0; i < this.ModbusCount; i++)
                {
                    this.CollectionOffset[i].Text = (3000 + startAddress).ToString();
                    startAddress += 2;
                }
            }
            else
            {
                int address = 3010;
                for (int i = 0; i < this.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 ClearData()
        {
            this.ParentForm.ParentForm.Current30000ModbusData.Initialization();

            for (int i = 0; i < this.ParentForm.ParentForm.SystemConfig.EQUIPMENT_LANE; i++)
            {
                this.CollectionDecIsProcessing[i].Text = this.ParentForm.ParentForm.Current30000ModbusData.CollectionIsProcessing[i].ToString();
                this.CollectionDecIsLeak[i].Text = this.ParentForm.ParentForm.Current30000ModbusData.CollectionIsLeak[i].ToString();
                this.CollectionHexaIsProcessing[i].Text = this.ParentForm.ParentForm.Current30000ModbusData.CollectionIsProcessingHexa[i].ToString();
                this.CollectionHexaIsLeak[i].Text = this.ParentForm.ParentForm.Current30000ModbusData.CollectionIsLeakHexa[i].ToString();
            }
        }

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

        #region Event Handler
        #endregion
    }
}
