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_6CH.Forms;

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

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

        private Collection<SmartButton> CollButtonLane;
        private Collection<SmartLabel> CollLabelWeight;
        private Collection<CalibrationStatus> CollCalStatus;
        #endregion

        #region Constructor
        public ControlCalibration10(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; }
        }
        #endregion

        #region Method
        private void CreateCollection()
        {
            this.CollButtonLane = new Collection<SmartButton>();
            this.CollButtonLane.Clear();
            this.CollButtonLane.Add(this.buttonLane1);
            this.CollButtonLane.Add(this.buttonLane2);
            this.CollButtonLane.Add(this.buttonLane3);
            this.CollButtonLane.Add(this.buttonLane4);
            this.CollButtonLane.Add(this.buttonLane5);
            this.CollButtonLane.Add(this.buttonLane6);
            this.CollButtonLane.Add(this.buttonLane7);
            this.CollButtonLane.Add(this.buttonLane8);
            this.CollButtonLane.Add(this.buttonLane9);
            this.CollButtonLane.Add(this.buttonLane10);

            this.CollLabelWeight = new Collection<SmartLabel>();
            this.CollLabelWeight.Clear();
            this.CollLabelWeight.Add(this.labelWeight1);
            this.CollLabelWeight.Add(this.labelWeight2);
            this.CollLabelWeight.Add(this.labelWeight3);
            this.CollLabelWeight.Add(this.labelWeight4);
            this.CollLabelWeight.Add(this.labelWeight5);
            this.CollLabelWeight.Add(this.labelWeight6);
            this.CollLabelWeight.Add(this.labelWeight7);
            this.CollLabelWeight.Add(this.labelWeight8);
            this.CollLabelWeight.Add(this.labelWeight9);
            this.CollLabelWeight.Add(this.labelWeight10);

            this.CollCalStatus = new Collection<CalibrationStatus>();
            this.CollCalStatus.Clear();
            for (int i = 0; i < 10; i++)
                this.CollCalStatus.Add(new CalibrationStatus());
        }
        private void InitilizeControls()
        {
            for (int i = 0; i < this.ParentForm.ParentForm.SystemConfig.EquipmentColumns; i++)
            {
                this.CollLabelWeight[i].Text = Helper.DoubleToString(0, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            }
        }
        private void InitializeDesign()
        {
           
        }

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

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

        public void CalibrationBalance()
        {
            // Help
            this.ParentForm.UpdateDisplayHelpBalance();
            
            foreach (SmartButton bt in this.CollButtonLane)
            {
                if (bt.ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                    this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationBalance, bt.Tag.ToString());
            }

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

            // Help
            this.ParentForm.UpdateDisplayHelpStart();

            for (int i = 0; i < this.CollButtonLane.Count; i++)
            {
                if (this.CollButtonLane[i].ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                {
                    if (autoMode == true)
                        this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationInternalBalance, this.CollButtonLane[i].Tag.ToString());
                    else
                        this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationStart, this.CollButtonLane[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.CollButtonLane[i].Enabled = false;
            }

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

            // Help
            this.ParentForm.UpdateDisplayHelpCancel();

            for (int i = 0; i < this.CollButtonLane.Count; i++)
            {
                if (this.CollButtonLane[i].ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                {
                    this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationCancel, this.CollButtonLane[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.CollButtonLane[i].Enabled = true;
            }

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

        public void UpdateWeightDisplay(DataStore.EquipmentStatus status, Collection<WeightData> weights)
        {
            if (this.CollLabelWeight == null || weights.Count < this.CollLabelWeight.Count || status == DataStore.EquipmentStatus.Start)
                return;

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

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

                        // 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.ParentForm.BalanceWeight);
                        labelWeight.ForeColor = this.NormalColor;
                        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.CollButtonLane)
                            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.StringToDecimalPlaces(weightData.WeightString, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    labelWeight.ForeColor = this.FinishColor;
                    break;
                case DataStore.WeightStatus.CalError:
                    if (rbChecked == true)
                    {
                        // Help
                        this.ParentForm.UpdateDisplayHelpCalError();
                      
                        this.ParentForm.CalibrationButtonEnable(true, false, false);
                        foreach (SmartX.SmartButton bt in this.CollButtonLane)
                            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.StringToDecimalPlaces(weightData.WeightString, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    labelWeight.ForeColor = this.NormalColor;
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region Event Handler
        private void buttonLane_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
    }
}
