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 SmartX;
using ITC81DB_2.DialogForms;
using ITC81DB_2.Forms;
using ITC81DB_2_ImageDll;

namespace ITC81DB_2.Controls
{
    public partial class ControlMainModbus : UserControl
    {
        #region Field
        private UInt32 ReceiveCount;
        private FormMainDisplay m_ParentForm;
        #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; }
            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)
            {

            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Chinese)
            {

            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
            {

            }
            else
            {

            }
        }
        private void DefaultSetting()
        {
            this.ReceiveCount = 0;
            this.labelReceiveCount.Text = this.ReceiveCount.ToString();
        }

        public void Clear()
        {
            this.UpdateData(this.ParentForm.ParentForm.Current30000ModbusData1);
        }
        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)
        {
            if (this.buttonSelectDecHexa.ButtonStatus == SmartX.SmartButton.BUTSTATUS.UP)
            {
                this.labelUpdateFlag1.Text = mData.IsUpdate.ToString();
                this.labelEquipmentID1.Text = mData.EquipmentID.ToString();
                this.labelCurrentNo1.Text = mData.ProductNo.ToString();

                this.labelUnder1.Text = mData.UnderValue.ToString();
                this.labelPass1.Text = mData.PassValue.ToString();
                this.labelOver1.Text = mData.OverValue.ToString();
                this.labelTare1.Text = mData.TareValue.ToString();

                this.labelUnderCount1.Text = mData.UnderCount.ToString();
                this.labelPassCount1.Text = mData.PassCount.ToString();
                this.labelOverCount1.Text = mData.OverCount.ToString();
                this.labelNGCount1.Text = mData.NGCount.ToString();
                this.labelExNGCount1.Text = mData.ExNGCount.ToString();
                this.labelTotalCount1.Text = mData.TotalCount.ToString();

                this.labelGrade1.Text = mData.Grade.ToString();
                this.labelWeight1.Text = mData.Weight.ToString();
                this.labelOperationStatus1.Text = mData.OperationStatus.ToString();
                this.labelAlarmStatus1.Text = mData.AlarmStatus.ToString();
                if (mData.Speed <= 500)
                {
                    string value = mData.Speed.ToString();
                    if (this.labelSpeed1.Text != value)
                        this.labelSpeed1.Text = value;
                }
                this.labelMetal1.Text = mData.MetalDetectionCount.ToString();
                this.labelDouble1.Text = mData.DoubleEntryCount.ToString();
            }
            else
            {
                this.labelUpdateFlag1.Text = mData.HexStringIsUpdate;
                this.labelEquipmentID1.Text = mData.HexStringEquipmentID;
                this.labelCurrentNo1.Text = mData.HexStringProductNo;

                this.labelUnder1.Text = mData.HexStringUnderValue;
                this.labelPass1.Text = mData.HexStringPassValue;
                this.labelOver1.Text = mData.HexStringOverValue;
                this.labelTare1.Text = mData.HexStringTareValue;

                this.labelUnderCount1.Text = mData.HexStringUnderCount;
                this.labelPassCount1.Text = mData.HexStringPassCount;
                this.labelOverCount1.Text = mData.HexStringOverCount;
                this.labelNGCount1.Text = mData.HexStringNGCount;
                this.labelExNGCount1.Text = mData.HexStringExNGCount;
                this.labelTotalCount1.Text = mData.HexStringTotalCount;

                this.labelGrade1.Text = mData.HexStringGrade;
                this.labelWeight1.Text = mData.HexStringWeight;
                this.labelOperationStatus1.Text = mData.HexStringOperationStatus;
                this.labelAlarmStatus1.Text = mData.HexStringAlarmStatus;
                if (mData.Speed <= 500)
                {
                    string value = mData.HexStringSpeed;
                    if (this.labelSpeed1.Text != value)
                        this.labelSpeed1.Text = value;
                }
                this.labelMetal1.Text = mData.HexStringMetalDetectionCount;
                this.labelDouble1.Text = mData.HexStringDoubleEntryCount;
            }
        }
        public void UpdateAlarmData(_30000_ModbusData mData)
        {

        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.comboBoxPort.Items.Clear();
            if (this.ParentForm.ParentForm.SystemConfig.SerialCOM1Mode == (int)Define.E_CommMode.f99_Modbus)
                this.comboBoxPort.Items.Add("COM1");
            if (this.ParentForm.ParentForm.SystemConfig.SerialCOM3Mode == (int)Define.E_CommMode.f99_Modbus)
                this.comboBoxPort.Items.Add("COM3");
            if (this.ParentForm.ParentForm.SystemConfig.SerialCOM4Mode == (int)Define.E_CommMode.f99_Modbus)
                this.comboBoxPort.Items.Add("COM4");
            if (this.ParentForm.ParentForm.SystemConfig.EthernetCommMode == (int)Define.E_CommMode.f99_Modbus)
                this.comboBoxPort.Items.Add("TCP");
            this.comboBoxPort.SelectedIndex = 0;

            this.UpdateData(this.ParentForm.ParentForm.Current30000ModbusData1);
        }
        #endregion

        private void comboBoxPort_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void buttonBypassDirection_Click(object sender, EventArgs e)
        {

        }

        #region Event Handler
        #endregion
    }
}
