using System;
using System.Linq;
using System.Collections.ObjectModel;
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 SmartX;

namespace ITC81DB.Controls
{
    public partial class ControlMainModbus : UserControl
    {
        #region Field
        private FormMainDisplay m_ParentForm;
        private UInt32 ReceiveCount;
        private static int ModbusCount = 17;

        private Collection<SmartLabel> CollectionOffset;
        #endregion
        
        #region Constructor
        public ControlMainModbus(FormMainDisplay parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

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

        #region Property
        public FormMainDisplay 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.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engMainModbusScreen));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainModbusScreen));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainModbusScreen));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainModbusScreen));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Spanish)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engMainModbusScreen));
            }
            else
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korMainModbusScreen));
            }
        }
        private void DefaultSetting()
        {
            this.CollectionOffset = new Collection<SmartLabel>();
            this.CollectionOffset.Clear();
            this.CollectionOffset.Add(this.labelOffset1);
            this.CollectionOffset.Add(this.labelOffset2);
            this.CollectionOffset.Add(this.labelOffset3);
            this.CollectionOffset.Add(this.labelOffset4);
            this.CollectionOffset.Add(this.labelOffset5);
            this.CollectionOffset.Add(this.labelOffset6);
            this.CollectionOffset.Add(this.labelOffset7);
            this.CollectionOffset.Add(this.labelOffset8);
            this.CollectionOffset.Add(this.labelOffset9);
            this.CollectionOffset.Add(this.labelOffset10);
            this.CollectionOffset.Add(this.labelOffset11);
            this.CollectionOffset.Add(this.labelOffset12);
            this.CollectionOffset.Add(this.labelOffset13);
            this.CollectionOffset.Add(this.labelOffset14);
            this.CollectionOffset.Add(this.labelOffset15);
            this.CollectionOffset.Add(this.labelOffset16);
            this.CollectionOffset.Add(this.labelOffset17);

            this.buttonBypassDirection.ButtonUp();

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

            this.labelUpdateFlag.Text = "";
            this.labelEquipmentID.Text = "";
            this.labelCurrentNo.Text = "";

            this.labelUnder.Text = "";
            this.labelPass.Text = "";
            this.labelOver.Text = "";
            this.labelTare.Text = "";

            this.labelUnderCount.Text = "";
            this.labelPassCount.Text = "";
            this.labelOverCount.Text = "";
            this.labelNGCount.Text = "";
            this.labelExNGCount.Text = "";
            this.labelTotalCount.Text = "";

            this.labelGrade.Text = "";
            this.labelWeight.Text = "";
            this.labelOperationStatus.Text = "";
            this.labelAlarmStatus.Text = "";
        }

        public void UpdateEquipmentStatusDisplay(DataStore.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)
        {
            if (this.buttonBypassDirection.ButtonStatus == SmartX.SmartButton.BUTSTATUS.UP)
            {
                this.labelUpdateFlag.Text = mData.IsUpdate.ToString();
                this.labelEquipmentID.Text = mData.EquipmentID.ToString();
                this.labelCurrentNo.Text = mData.ProductNo.ToString();

                if(mData.UnderValue.ToString().Contains('.') == false)
                    this.labelUnder.Text = Helper.StringToDecimalPlacesPlusZero(mData.UnderValue.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                else
                    this.labelUnder.Text = mData.UnderValue.ToString();
                if (mData.PassValue.ToString().Contains('.') == false)
                    this.labelPass.Text = Helper.StringToDecimalPlacesPlusZero(mData.PassValue.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                else
                    this.labelPass.Text = mData.PassValue.ToString();
                if (mData.OverValue.ToString().Contains('.') == false)
                    this.labelOver.Text = Helper.StringToDecimalPlacesPlusZero(mData.OverValue.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                else
                    this.labelOver.Text = mData.OverValue.ToString();
                if (mData.TareValue.ToString().Contains('.') == false)
                    this.labelTare.Text = Helper.StringToDecimalPlacesPlusZero(mData.TareValue.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                else
                    this.labelTare.Text = mData.TareValue.ToString();

                this.labelUnderCount.Text = mData.UnderCount.ToString();
                this.labelPassCount.Text = mData.PassCount.ToString();
                this.labelOverCount.Text = mData.OverCount.ToString();
                this.labelNGCount.Text = mData.NGCount.ToString();
                this.labelExNGCount.Text = mData.ExNGCount.ToString();
                this.labelTotalCount.Text = mData.TotalCount.ToString();

                this.labelGrade.Text = mData.Grade.ToString();
                if (mData.Weight.ToString().Contains('.') == false)
                    this.labelWeight.Text = Helper.StringToDecimalPlacesPlusZero(mData.Weight.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                else
                    this.labelWeight.Text = mData.Weight.ToString();
                this.labelOperationStatus.Text = mData.OperationStatus.ToString();
                this.labelAlarmStatus.Text = mData.AlarmStatus.ToString();
            }
            else
            {
                this.labelUpdateFlag.Text = mData.HexStringIsUpdate;
                this.labelEquipmentID.Text = mData.HexStringEquipmentID;
                this.labelCurrentNo.Text = mData.HexStringProductNo;

                this.labelUnder.Text = mData.HexStringUnderValue;
                this.labelPass.Text = mData.HexStringPassValue;
                this.labelOver.Text = mData.HexStringOverValue;
                this.labelTare.Text = mData.HexStringTareValue;

                this.labelUnderCount.Text = mData.HexStringUnderCount;
                this.labelPassCount.Text = mData.HexStringPassCount;
                this.labelOverCount.Text = mData.HexStringOverCount;
                this.labelNGCount.Text = mData.HexStringNGCount;
                this.labelExNGCount.Text = mData.HexStringExNGCount;
                this.labelTotalCount.Text = mData.HexStringTotalCount;

                this.labelGrade.Text = mData.HexStringGrade;
                this.labelWeight.Text = mData.HexStringWeight;
                this.labelOperationStatus.Text = mData.HexStringOperationStatus;
                this.labelAlarmStatus.Text = mData.HexStringAlarmStatus;
            }
        }
        public void UpdateOffset(bool master, int startAddress)
        {
            if (master == true)
            {
                for (int i = 0; i < ModbusCount; i++)
                {
                    this.CollectionOffset[i].Text = startAddress.ToString();
                    startAddress += 2;
                }
            }
            else
            {
                int address = 10;
                for (int i = 0; i < ModbusCount; i++)
                {
                    this.CollectionOffset[i].Text = "0x" + address.ToString("X4");
                    address += 2;
                }
            }
        }
        public void UpdateDisplay(string portName)
        {
            switch (portName)
            {
                case "COM1":
                    if (this.ParentForm.ParentForm.SystemConfig2.ModbusRTUSelectFunction == (int)DataStore.ModbusFunction._04_ReadInputRegister)
                    {
                        this.labelStartAddress.Text = "-";
                        this.UpdateOffset(false, this.ParentForm.ParentForm.SystemConfig3.ModbusRTUStartAddressCOM1);
                    }
                    else
                    {
                        this.labelStartAddress.Text = this.ParentForm.ParentForm.SystemConfig3.ModbusRTUStartAddressCOM1.ToString();
                        this.UpdateOffset(true, this.ParentForm.ParentForm.SystemConfig3.ModbusRTUStartAddressCOM1);
                    }
                    break;
                case "COM3":
                    if (this.ParentForm.ParentForm.SystemConfig2.ModbusRTUSelectFunction == (int)DataStore.ModbusFunction._04_ReadInputRegister)
                    {
                        this.labelStartAddress.Text = "-";
                        this.UpdateOffset(false, this.ParentForm.ParentForm.SystemConfig3.ModbusRTUStartAddressCOM3);
                    }
                    else
                    {
                        this.labelStartAddress.Text = this.ParentForm.ParentForm.SystemConfig3.ModbusRTUStartAddressCOM3.ToString();
                        this.UpdateOffset(true, this.ParentForm.ParentForm.SystemConfig3.ModbusRTUStartAddressCOM3);
                    }
                    break;
                case "COM4":
                    if (this.ParentForm.ParentForm.SystemConfig2.ModbusRTUSelectFunction == (int)DataStore.ModbusFunction._04_ReadInputRegister)
                    {
                        this.labelStartAddress.Text = "-";
                        this.UpdateOffset(false, this.ParentForm.ParentForm.SystemConfig3.ModbusRTUStartAddressCOM4);
                    }
                    else
                    {
                        this.labelStartAddress.Text = this.ParentForm.ParentForm.SystemConfig3.ModbusRTUStartAddressCOM4.ToString();
                        this.UpdateOffset(true, this.ParentForm.ParentForm.SystemConfig3.ModbusRTUStartAddressCOM4);
                    }
                    break;
                case "TCP":
                    if (this.ParentForm.ParentForm.SystemConfig2.ModbusTCPSelectFunction == (int)DataStore.ModbusFunction._04_ReadInputRegister)
                    {
                        this.labelStartAddress.Text = "-";
                        this.UpdateOffset(false, this.ParentForm.ParentForm.SystemConfig3.ModbusTCPStartAddress);
                    }
                    else
                    {
                        this.labelStartAddress.Text = this.ParentForm.ParentForm.SystemConfig3.ModbusTCPStartAddress.ToString();
                        this.UpdateOffset(true, this.ParentForm.ParentForm.SystemConfig3.ModbusTCPStartAddress);
                    }
                    break;
                default:
                    break;
            }
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.comboBoxPort.Items.Clear();
            if (this.ParentForm.ParentForm.SystemConfig1.SerialCOM1Mode == (int)DataStore.SerialMode.f22_Modbus_RTU
                || this.ParentForm.ParentForm.SystemConfig1.SerialCOM1Mode == (int)DataStore.SerialMode.f16_OPC)
                this.comboBoxPort.Items.Add("COM1");
            if (this.ParentForm.ParentForm.SystemConfig1.SerialCOM3Mode == (int)DataStore.SerialMode.f22_Modbus_RTU
                || this.ParentForm.ParentForm.SystemConfig1.SerialCOM3Mode == (int)DataStore.SerialMode.f16_OPC)
                this.comboBoxPort.Items.Add("COM3");
            if (this.ParentForm.ParentForm.SystemConfig1.SerialCOM4Mode == (int)DataStore.SerialMode.f22_Modbus_RTU
                || this.ParentForm.ParentForm.SystemConfig1.SerialCOM4Mode == (int)DataStore.SerialMode.f16_OPC)
                this.comboBoxPort.Items.Add("COM4");
            if (this.ParentForm.ParentForm.SystemConfig2.EthernetCommMode == (int)DataStore.EthernetMode.f22_Modbus_TCP
                || this.ParentForm.ParentForm.SystemConfig2.EthernetCommMode == (int)DataStore.EthernetMode.f16_OPC)
                this.comboBoxPort.Items.Add("TCP");
            this.comboBoxPort.SelectedIndex = 0;

            this.UpdateDisplay(this.comboBoxPort.SelectedItem.ToString());
            this.UpdateData(this.ParentForm.ParentForm.Current30000ModbusData);
        }
        #endregion
              
        #region Event Handler
        private void buttonBypassDirection_Click(object sender, EventArgs e)
        {
            this.UpdateData(this.ParentForm.ParentForm.Current30000ModbusData);
        }
        private void comboBoxPort_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.UpdateDisplay(this.comboBoxPort.SelectedItem.ToString());
        }
        #endregion
    }
}
