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 SmartX;
using INT63DC_2C.Forms;

namespace INT63DC_2C.Controls
{
    public partial class ControlCalibration12 : UserControl
    {
        #region Field
        private FormCalibration m_ParentForm;

        private Color NormalColor = Color.Black;
        private Color FinishColor = Color.Blue;

        private Collection<SmartButton> CollectionButtonLane;
        private Collection<SmartLabel> CollectionLabelWeight;
        private Collection<SmartLabel> CollectionLabelADC;
        private Collection<SmartLabel> CollectionLabelConstant;
        private Collection<CalibrationStatus> CollCalStatus;
        #endregion

        #region Constructor
        public ControlCalibration12(FormCalibration parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

            this.CreateCollection();
            this.InitilizeControls();
            this.InitializeDesign();
            this.DisplayRefresh();
        }
        #endregion

        #region Property
        public FormCalibration ParentForm
        {
            get { return this.m_ParentForm; }
            private set { this.m_ParentForm = value; }
        }
        private int EquipmentColumns
        { 
            get { return this.ParentForm.ParentForm.SystemConfig.EquipmentColumns; } 
        }
        #endregion

        #region Method
        private void CreateCollection()
        {
            this.CollectionButtonLane = new Collection<SmartButton>();
            this.CollectionLabelWeight = new Collection<SmartLabel>();
            this.CollectionLabelADC = new Collection<SmartLabel>();
            this.CollectionLabelConstant = new Collection<SmartLabel>();
            this.CollCalStatus = new Collection<CalibrationStatus>();

            for (int i = 1; i <= this.EquipmentColumns; i++)
            {
                this.CollectionButtonLane.Add(FindByNameUtil.SmartButton("buttonLane" + i, this));
                this.CollectionLabelWeight.Add(FindByNameUtil.SmartLabel("labelWeight" + i, this));
                this.CollectionLabelADC.Add(FindByNameUtil.SmartLabel("labelADC" + i, this));
                this.CollectionLabelConstant.Add(FindByNameUtil.SmartLabel("labelConstant" + i, this));
                this.CollCalStatus.Add(new CalibrationStatus());
            }
        }
        private void InitilizeControls()
        {
            for (int i = 0; i < this.ParentForm.ParentForm.SystemConfig.EquipmentColumns; i++)
            {
                this.CollectionLabelWeight[i].Text = "0.0";
                this.CollectionLabelADC[i].Text = "0.0";
                this.CollectionLabelConstant[i].Text = "0.0";
            }
        }
        private void InitializeDesign()
        {
            
        }

        public void DisplayRefresh()
        {
            if (this.CollectionButtonLane == null || this.CollectionButtonLane.Count == 0)
                return;

            for (int i = 0; i < this.CollectionButtonLane.Count; i++)
            {
                this.CollectionButtonLane[i].ButtonUp();
                this.CollCalStatus[i].Initialize();
            }
        }

        public void CalibrationBalance()
        {
            string detail = "";

            // Help
            this.ParentForm.UpdateDisplayHelpBalance();

            for (int i = 0; i < this.CollectionButtonLane.Count; i++)
            {
                if (this.CollectionButtonLane[i].ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                {
                    this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationBalance, this.CollectionButtonLane[i].Tag.ToString());

                    // Part 11
                    if (this.ParentForm.ParentForm.SystemConfig.IsPart11 == true)
                    {
                        detail = string.Format("{0} Lane Balance", i + 1);
                        this.ParentForm.ParentForm.SetTrackingHistoryData(DataStore.E_TrackingOperation.Calibration, detail);
                    }
                }
            }

            this.ParentForm.CalibrationButtonEnable(false, false, true);
        }
        public void CalibrationStart()
        {
            string detail = "";

            // Help
            this.ParentForm.UpdateDisplayHelpStart();

            for (int i = 0; i < this.CollectionButtonLane.Count; i++)
            {
                if (this.CollectionButtonLane[i].ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                {
                    this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationStart, this.CollectionButtonLane[i].Tag.ToString());

                    // Part 11
                    if (this.ParentForm.ParentForm.SystemConfig.IsPart11 == true)
                    {
                        this.CollCalStatus[i].Initialize();
                        detail = string.Format("{0} Lane Start", i + 1);
                        this.ParentForm.ParentForm.SetTrackingHistoryData(DataStore.E_TrackingOperation.Calibration, detail);
                    }
                }

                this.CollectionButtonLane[i].Enabled = false;
            }

            this.ParentForm.CalibrationButtonEnable(false, false, false);
        }
        public void CalibrationCancel()
        {
            string detail = "";

            // Help
            this.ParentForm.UpdateDisplayHelpCancel();

            for (int i = 0; i < this.CollectionButtonLane.Count; i++)
            {
                if (this.CollectionButtonLane[i].ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                {
                    this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationCancel, this.CollectionButtonLane[i].Tag.ToString());

                    // Part 11
                    if (this.ParentForm.ParentForm.SystemConfig.IsPart11 == true)
                    {
                        if (this.CollCalStatus[i].IsCancel == false)
                        {
                            this.CollCalStatus[i].IsCancel = true;
                            detail = string.Format("{0} Lane Cancel", i + 1);
                            this.ParentForm.ParentForm.SetTrackingHistoryData(DataStore.E_TrackingOperation.Calibration, detail);
                        }
                    }
                }

                this.CollectionButtonLane[i].Enabled = true;
            }

            this.ParentForm.CalibrationButtonEnable(true, false, false);
        }

        public void UpdateConfiguration(DataStore.EquipmentStatus status, CalibrationItem config)
        {
            string value = "";

            value = Helper.StringToDecimalPlaces(config.Constant1.Trim(), 6);
            if (this.labelConstant1.Text != value)
                this.labelConstant1.Text = value;

            value = Helper.StringToDecimalPlaces(config.Constant2.Trim(), 6);
            if (this.labelConstant2.Text != value)
                this.labelConstant2.Text = value;

            value = Helper.StringToDecimalPlaces(config.Constant3.Trim(), 6);
            if (this.labelConstant3.Text != value)
                this.labelConstant3.Text = value;

            value = Helper.StringToDecimalPlaces(config.Constant4.Trim(), 6);
            if (this.labelConstant4.Text != value)
                this.labelConstant4.Text = value;

            value = Helper.StringToDecimalPlaces(config.Constant5.Trim(), 6);
            if (this.labelConstant5.Text != value)
                this.labelConstant5.Text = value;

            value = Helper.StringToDecimalPlaces(config.Constant6.Trim(), 6);
            if (this.labelConstant6.Text != value)
                this.labelConstant6.Text = value;

            value = Helper.StringToDecimalPlaces(config.Constant7.Trim(), 6);
            if (this.labelConstant7.Text != value)
                this.labelConstant7.Text = value;

            value = Helper.StringToDecimalPlaces(config.Constant8.Trim(), 6);
            if (this.labelConstant8.Text != value)
                this.labelConstant8.Text = value;

            value = Helper.StringToDecimalPlaces(config.Constant9.Trim(), 6);
            if (this.labelConstant9.Text != value)
                this.labelConstant9.Text = value;

            value = Helper.StringToDecimalPlaces(config.Constant10.Trim(), 6);
            if (this.labelConstant10.Text != value)
                this.labelConstant10.Text = value;

            value = Helper.StringToDecimalPlaces(config.Constant11.Trim(), 6);
            if (this.labelConstant11.Text != value)
                this.labelConstant11.Text = value;

            value = Helper.StringToDecimalPlaces(config.Constant12.Trim(), 6);
            if (this.labelConstant12.Text != value)
                this.labelConstant12.Text = value;
        }
        public void UpdateWeightDisplay(DataStore.EquipmentStatus status, Collection<WeightData> weights)
        {
            if (this.CollectionLabelWeight == null || weights.Count < this.CollectionLabelWeight.Count || status == DataStore.EquipmentStatus.Start)
                return;

            for (int i = 0; i < weights.Count; i++)
            {
                if (this.CollectionButtonLane[i].ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                {
                    this.UpdateLabelWeight(i, this.CollectionButtonLane[i], this.CollectionLabelWeight[i], this.CollectionLabelADC[i], weights[i], true);
                }
                else
                {
                    this.UpdateLabelWeight(i, this.CollectionButtonLane[i], this.CollectionLabelWeight[i], this.CollectionLabelADC[i], weights[i], false);
                }
            }
        }
        private void UpdateLabelWeight(int index, SmartButton buttonLane, SmartLabel labelWeight, SmartLabel labelADC, WeightData weightData, bool rbChecked)
        {
            string detail = "";

            switch (weightData.Status)
            {
                case DataStore.WeightStatus.CalNomal:
                    labelWeight.Text = Helper.DoubleToString(weightData.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    labelWeight.ForeColor = this.NormalColor;
                    labelADC.Text = weightData.ADCValue;
                    break;
                case DataStore.WeightStatus.CalStandby:
                    if (rbChecked == true)
                    {
                        // 분동중량 표시
                        labelWeight.Text = string.Format("-{0}", this.ParentForm.labelBalanceWeight.Text);
                        labelWeight.ForeColor = this.NormalColor;
                        labelADC.Text = weightData.ADCValue;

                        // Help
                        this.ParentForm.UpdateDisplayHelpCalStandby();

                        this.ParentForm.CalibrationButtonEnable(false, true, true);
                    }
                    break;
                case DataStore.WeightStatus.CalBalans:
                    if (rbChecked == true)
                    {
                        // 분동중량표시
                        labelWeight.Text = string.Format("-{0}", this.ParentForm.labelBalanceWeight.Text);
                        labelWeight.ForeColor = this.NormalColor;
                        labelADC.Text = weightData.ADCValue;
                        this.ParentForm.CalibrationButtonEnable(false, false, true);
                    }
                    break;
                case DataStore.WeightStatus.CalFinish:
                    if (rbChecked == true)
                    {
                        // Help
                        this.ParentForm.UpdateDisplayHelpCalFinish();

                        this.ParentForm.CalibrationButtonEnable(true, false, false);
                        foreach (SmartX.SmartButton bt in this.CollectionButtonLane)
                            bt.Enabled = true;

                        // Part11
                        if (this.ParentForm.ParentForm.SystemConfig.IsPart11 == true)
                        {
                            if (this.CollCalStatus[index].IsFinish == false)
                            {
                                this.CollCalStatus[index].IsFinish = true;
                                detail = string.Format("{0} Lane Success", index + 1);
                                this.ParentForm.ParentForm.SetTrackingHistoryData(DataStore.E_TrackingOperation.Calibration, detail);
                            }
                        }
                    }
                    labelWeight.Text = Helper.DoubleToString(weightData.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    labelWeight.ForeColor = this.FinishColor;
                    labelADC.Text = weightData.ADCValue;
                    break;
                case DataStore.WeightStatus.CalError:
                    if (rbChecked == true)
                    {
                        // Help
                        this.ParentForm.UpdateDisplayHelpCalError();

                        this.ParentForm.CalibrationButtonEnable(true, false, false);
                        foreach (SmartX.SmartButton bt in this.CollectionButtonLane)
                            bt.Enabled = true;

                        // Part11
                        if (this.ParentForm.ParentForm.SystemConfig.IsPart11 == true)
                        {
                            if (this.CollCalStatus[index].IsError == false)
                            {
                                this.CollCalStatus[index].IsError = true;
                                detail = string.Format("{0} Lane Error", index + 1);
                                this.ParentForm.ParentForm.SetTrackingHistoryData(DataStore.E_TrackingOperation.Calibration, detail);
                            }
                        }
                    }
                    labelWeight.Text = Helper.DoubleToString(weightData.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    labelWeight.ForeColor = this.NormalColor;
                    labelADC.Text = weightData.ADCValue;
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region Event Handler
        private void buttonLine_Click(object sender, EventArgs e)
        {
            SmartButton bt = sender as SmartButton;

            if (bt == null)
                return;

            // Help
            this.ParentForm.UpdateDisplayHelpInit();

            this.ParentForm.CalibrationButtonEnable(true, false, false);
        }
        #endregion
    }
}
