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 INT99DC_7C.Forms;
using INT99DC_7C.Int_Modbus;

namespace INT99DC_7C.Controls
{
    public partial class ControlModbusDisplayCommonData : UserControl
    {
        #region Field
        private FormMainDisplay m_ParentForm;
        private UInt32 ReceiveCount;        
        #endregion

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

            this.ParentForm = parent;

            this.Initialize();
        } 
        #endregion

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

        #region Method
        private void Initialize()
        {
            this.ReceiveCount = 0;
            this.labelReceiveCount.Text = this.ReceiveCount.ToString();

            switch (this.ParentForm.ParentForm.SystemConfig.EquipmentColumns)
            {
                case 7:
                    this.button7to12.Text = "7";
                    break;
                case 8:
                    this.button7to12.Text = "7 to 8";
                    break;
                case 10:
                    this.button7to12.Text = "7 to 10";
                    break;
                case 12:
                case 15:
                    this.button7to12.Text = "7 to 12";
                    break;
                default:
                    break;
            }
        }

        public void UpdateData(Modbus_30000_Data mData)
        {
            if (this.ParentForm.FlagModbusDec == true)
            {
                this.labelUnderRange.Text = Helper.DoubleToString(mData.UnderValue, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                this.labelPassRange.Text = Helper.DoubleToString(mData.PassValue, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                this.labelOverRange.Text = Helper.DoubleToString(mData.OverValue, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                this.labelTareRange.Text = Helper.DoubleToString(mData.TareValue, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            }
            else
            {
                this.labelUnderRange.Text = mData.HexStringUnderValue;
                this.labelPassRange.Text = mData.HexStringPassValue;
                this.labelOverRange.Text = mData.HexStringOverValue;
                this.labelTareRange.Text = mData.HexStringTareValue;
            }
        }
        public void UpdateReceiveCount()
        {
            if (this.ReceiveCount > 999999999)
                this.ReceiveCount = 0;
            else
            {
                this.ReceiveCount++;
                this.labelReceiveCount.Text = this.ReceiveCount.ToString();
            }
        }

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

        #region Event Handler
        private void buttonValueDecToHex_Click(object sender, EventArgs e)
        {
            // Automatic Logout Reset
            if (this.ParentForm.ParentForm.SystemConfig.IsPart11 == true)
                this.ParentForm.Part11AutomaticLogoutReset();

            if (this.buttonValueDecToHex.ButtonStatus == SmartX.SmartButton.BUTSTATUS.UP)
                this.ParentForm.FlagModbusDec = true;
            else
                this.ParentForm.FlagModbusDec = false;

            this.UpdateData(this.ParentForm.ParentForm.Current30000ModbusData);
        }

        private void buttonCommon_Click(object sender, EventArgs e)
        {
            // Automatic Logout Reset
            if (this.ParentForm.ParentForm.SystemConfig.IsPart11 == true)
                this.ParentForm.Part11AutomaticLogoutReset();
        }
        private void button1to6_Click(object sender, EventArgs e)
        {
            // Automatic Logout Reset
            if (this.ParentForm.ParentForm.SystemConfig.IsPart11 == true)
                this.ParentForm.Part11AutomaticLogoutReset();

            this.ParentForm.UpdateModbusDisplay(DataStore.E_ModbusDisplay.Display1);
        }
        private void button7to12_Click(object sender, EventArgs e)
        {
            // Automatic Logout Reset
            if (this.ParentForm.ParentForm.SystemConfig.IsPart11 == true)
                this.ParentForm.Part11AutomaticLogoutReset();

            this.ParentForm.UpdateModbusDisplay(DataStore.E_ModbusDisplay.Display2);
        }
        private void button13to15_Click(object sender, EventArgs e)
        {
            // Automatic Logout Reset
            if (this.ParentForm.ParentForm.SystemConfig.IsPart11 == true)
                this.ParentForm.Part11AutomaticLogoutReset();

            this.ParentForm.UpdateModbusDisplay(DataStore.E_ModbusDisplay.Display3);
        }
        #endregion
    }
}
