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

namespace INT89DB_26.Controls
{
    public partial class ControlMainDisplay6 : UserControl
    {
        #region Field
        private FormMainDisplay ParentForm;

        private Collection<PictureBox> CollectionPictureBoxZero;
        private Collection<PictureBox> CollectionPictureBoxGap;
        private Collection<SmartLabel> CollectionLabelWeightValue;
        private Collection<SmartLabel> CollectionLabelProductionSpeed;
        private Collection<SmartProgressBar> CollectionProgressBar;
        #endregion

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

            this.ParentForm = parent;

            this.CreateCollection();
            this.InitializeControl();
        }
        #endregion

        #region Property
        #endregion

        #region Method
        private void CreateCollection()
        {
            // PictureBoxZero
            this.CollectionPictureBoxZero = new Collection<PictureBox>();
            this.CollectionPictureBoxZero.Clear();
            this.CollectionPictureBoxZero.Add(this.pictureBoxZero1);
            this.CollectionPictureBoxZero.Add(this.pictureBoxZero2);
            this.CollectionPictureBoxZero.Add(this.pictureBoxZero3);
            this.CollectionPictureBoxZero.Add(this.pictureBoxZero4);
            this.CollectionPictureBoxZero.Add(this.pictureBoxZero5);
            this.CollectionPictureBoxZero.Add(this.pictureBoxZero6);

            // PictureBoxGap
            this.CollectionPictureBoxGap = new Collection<PictureBox>();
            this.CollectionPictureBoxGap.Clear();
            this.CollectionPictureBoxGap.Add(this.pictureBoxGap1);
            this.CollectionPictureBoxGap.Add(this.pictureBoxGap2);
            this.CollectionPictureBoxGap.Add(this.pictureBoxGap3);
            this.CollectionPictureBoxGap.Add(this.pictureBoxGap4);
            this.CollectionPictureBoxGap.Add(this.pictureBoxGap5);
            this.CollectionPictureBoxGap.Add(this.pictureBoxGap6);

            // LabelWeightValue
            this.CollectionLabelWeightValue = new Collection<SmartLabel>();
            this.CollectionLabelWeightValue.Clear();
            this.CollectionLabelWeightValue.Add(this.labelWeightValue1);
            this.CollectionLabelWeightValue.Add(this.labelWeightValue2);
            this.CollectionLabelWeightValue.Add(this.labelWeightValue3);
            this.CollectionLabelWeightValue.Add(this.labelWeightValue4);
            this.CollectionLabelWeightValue.Add(this.labelWeightValue5);
            this.CollectionLabelWeightValue.Add(this.labelWeightValue6);

            // LabelProductionSpeed
            this.CollectionLabelProductionSpeed = new Collection<SmartLabel>();
            this.CollectionLabelProductionSpeed.Clear();
            this.CollectionLabelProductionSpeed.Add(this.labelProductionSpeed1);
            this.CollectionLabelProductionSpeed.Add(this.labelProductionSpeed2);
            this.CollectionLabelProductionSpeed.Add(this.labelProductionSpeed3);
            this.CollectionLabelProductionSpeed.Add(this.labelProductionSpeed4);
            this.CollectionLabelProductionSpeed.Add(this.labelProductionSpeed5);
            this.CollectionLabelProductionSpeed.Add(this.labelProductionSpeed6);

            // ProgressBar
            this.CollectionProgressBar = new Collection<SmartProgressBar>();
            this.CollectionProgressBar.Clear();
            this.CollectionProgressBar.Add(this.progressBar1);
            this.CollectionProgressBar.Add(this.progressBar2);
            this.CollectionProgressBar.Add(this.progressBar3);
            this.CollectionProgressBar.Add(this.progressBar4);
            this.CollectionProgressBar.Add(this.progressBar5);
            this.CollectionProgressBar.Add(this.progressBar6);
        }
        private void InitializeControl()
        {
            foreach (PictureBox picture in this.CollectionPictureBoxGap)
                picture.Visible = false;

            foreach (PictureBox picture in this.CollectionPictureBoxZero)
                picture.Visible = false;

            foreach (SmartLabel label in this.CollectionLabelProductionSpeed)
                label.Text = "0";

            foreach (SmartLabel label in this.CollectionLabelWeightValue)
                label.Text = Helper.StringToDecimalPlaces("0", this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

            foreach (SmartProgressBar bar in this.CollectionProgressBar)
                bar.Value = bar.Minimum;

            this.SetCount(this.ParentForm.ParentForm.CollectionWeightData);
        }
        private void SetCount(Collection<WeightData> weightDatas)
        {
            string value = "";

            #region Line 1
            value = weightDatas[0].UnderCount.ToString();
            if (this.labelUnderCount1.Text != value)
                this.labelUnderCount1.Text = value;

            value = weightDatas[0].PassCount.ToString();
            if (this.labelPassCount1.Text != value)
                this.labelPassCount1.Text = value;

            value = weightDatas[0].OverCount.ToString();
            if (this.labelOverCount1.Text != value)
                this.labelOverCount1.Text = value;

            value = weightDatas[0].ExNGCount.ToString();
            if (this.labelExNGCount1.Text != value)
                this.labelExNGCount1.Text = value;

            value = weightDatas[0].TotalCount.ToString();
            if (this.labelTotalCount1.Text != value)
                this.labelTotalCount1.Text = value;
            #endregion

            #region Line 2
            value = weightDatas[1].UnderCount.ToString();
            if (this.labelUnderCount2.Text != value)
                this.labelUnderCount2.Text = value;

            value = weightDatas[1].PassCount.ToString();
            if (this.labelPassCount2.Text != value)
                this.labelPassCount2.Text = value;

            value = weightDatas[1].OverCount.ToString();
            if (this.labelOverCount2.Text != value)
                this.labelOverCount2.Text = value;

            value = weightDatas[1].ExNGCount.ToString();
            if (this.labelExNGCount2.Text != value)
                this.labelExNGCount2.Text = value;

            value = weightDatas[1].TotalCount.ToString();
            if (this.labelTotalCount2.Text != value)
                this.labelTotalCount2.Text = value;
            #endregion

            #region Line 3
            value = weightDatas[2].UnderCount.ToString();
            if (this.labelUnderCount3.Text != value)
                this.labelUnderCount3.Text = value;

            value = weightDatas[2].PassCount.ToString();
            if (this.labelPassCount3.Text != value)
                this.labelPassCount3.Text = value;

            value = weightDatas[2].OverCount.ToString();
            if (this.labelOverCount3.Text != value)
                this.labelOverCount3.Text = value;

            value = weightDatas[2].ExNGCount.ToString();
            if (this.labelExNGCount3.Text != value)
                this.labelExNGCount3.Text = value;

            value = weightDatas[2].TotalCount.ToString();
            if (this.labelTotalCount3.Text != value)
                this.labelTotalCount3.Text = value;
            #endregion

            #region Line 4
            value = weightDatas[3].UnderCount.ToString();
            if (this.labelUnderCount4.Text != value)
                this.labelUnderCount4.Text = value;

            value = weightDatas[3].PassCount.ToString();
            if (this.labelPassCount4.Text != value)
                this.labelPassCount4.Text = value;

            value = weightDatas[3].OverCount.ToString();
            if (this.labelOverCount4.Text != value)
                this.labelOverCount4.Text = value;

            value = weightDatas[3].ExNGCount.ToString();
            if (this.labelExNGCount4.Text != value)
                this.labelExNGCount4.Text = value;

            value = weightDatas[3].TotalCount.ToString();
            if (this.labelTotalCount4.Text != value)
                this.labelTotalCount4.Text = value;
            #endregion

            #region Line 5
            value = weightDatas[4].UnderCount.ToString();
            if (this.labelUnderCount5.Text != value)
                this.labelUnderCount5.Text = value;

            value = weightDatas[4].PassCount.ToString();
            if (this.labelPassCount5.Text != value)
                this.labelPassCount5.Text = value;

            value = weightDatas[4].OverCount.ToString();
            if (this.labelOverCount5.Text != value)
                this.labelOverCount5.Text = value;

            value = weightDatas[4].ExNGCount.ToString();
            if (this.labelExNGCount5.Text != value)
                this.labelExNGCount5.Text = value;

            value = weightDatas[4].TotalCount.ToString();
            if (this.labelTotalCount5.Text != value)
                this.labelTotalCount5.Text = value;
            #endregion

            #region Line 6
            value = weightDatas[5].UnderCount.ToString();
            if (this.labelUnderCount6.Text != value)
                this.labelUnderCount6.Text = value;

            value = weightDatas[5].PassCount.ToString();
            if (this.labelPassCount6.Text != value)
                this.labelPassCount6.Text = value;

            value = weightDatas[5].OverCount.ToString();
            if (this.labelOverCount6.Text != value)
                this.labelOverCount6.Text = value;

            value = weightDatas[5].ExNGCount.ToString();
            if (this.labelExNGCount6.Text != value)
                this.labelExNGCount6.Text = value;

            value = weightDatas[5].TotalCount.ToString();
            if (this.labelTotalCount6.Text != value)
                this.labelTotalCount6.Text = value;
            #endregion

            this.ParentForm.SetTotalCount();
        }

        public void RescaleProgressBar()
        {
            foreach (SmartProgressBar bar in this.CollectionProgressBar)
            {
                Helper.RescaleProgressBar(bar, this.ParentForm.ParentForm.CurrentProductItem.ProgressBarMaximum, this.ParentForm.ParentForm.CurrentProductItem.ProgressBarMinimum);
                bar.Value = bar.Minimum;
            }
        }
        public void Clear()
        {
            foreach (SmartX.SmartLabel label in this.CollectionLabelWeightValue)
                label.Text = Helper.StringToDecimalPlaces("0", this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
           
            foreach (SmartX.SmartProgressBar bar in this.CollectionProgressBar)
                bar.Value = bar.Minimum;

            foreach (WeightData data in this.ParentForm.ParentForm.CollectionWeightData)
                data.ClearCount();

            this.SetCount(this.ParentForm.ParentForm.CollectionWeightData);

            foreach (SmartX.SmartLabel label in this.CollectionLabelProductionSpeed)
                label.Text = "0";

            this.ParentForm.ParentForm.SaveCounterFile(this.ParentForm.ParentForm.CollectionWeightData, this.ParentForm.ParentForm.SystemConfig.ProductNumber - 1);
        }

        public void UpdateEquipmentStatusDisplay(DataStore.EquipmentStatus status)
        {
            string temp = "";

            foreach (SmartLabel label in this.CollectionLabelWeightValue)
            {
                temp = Helper.StringToDecimalPlaces("0", this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                if (label.Text != temp)
                    label.Text = temp;
            }

            foreach (SmartLabel label in this.CollectionLabelProductionSpeed)
            {
                if (label.Text != "0")
                    label.Text = "0";
            }

            this.RescaleProgressBar();
        }
        public void UpdateCurrentProductDisplay(DataStore.EquipmentStatus status, Collection<WeightData> weightDatas)
        {
            foreach (SmartProgressBar bar in this.CollectionProgressBar)
                bar.Value = bar.Minimum;

            this.SetCount(this.ParentForm.ParentForm.CollectionWeightData);
        }
        public void UpdateStopWeightDisplay(DataStore.EquipmentStatus status, Collection<WeightData> weightDatas)
        {
            string value = "";

            if (this.ParentForm.ParentForm.SystemConfig.EquipmentColumns > weightDatas.Count)
                return;

            if (status == DataStore.EquipmentStatus.Stop)
            {
                for (int i = 0; i < this.ParentForm.ParentForm.SystemConfig.EquipmentColumns; i++)
                {
                    // 중량
                    value = Helper.DoubleToString(weightDatas[i].Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    if (this.CollectionLabelWeightValue[i].Text != value)
                        this.CollectionLabelWeightValue[i].Text = value;

                    // 영점표시
                    if (weightDatas[i].Status == DataStore.WeightStatus.WeightZero)
                        this.CollectionPictureBoxZero[i].Visible = true;
                    else
                        this.CollectionPictureBoxZero[i].Visible = false;

                    // Gap표시
                    this.CollectionPictureBoxGap[i].Visible = false;
                }
            }
            else
            {
                for (int i = 0; i < this.ParentForm.ParentForm.SystemConfig.EquipmentColumns; i++)
                {
                    // 영점표시
                    if (weightDatas[i].Status == DataStore.WeightStatus.WeightZero)
                        this.CollectionPictureBoxZero[i].Visible = true;
                    else
                        this.CollectionPictureBoxZero[i].Visible = false;
                }
            }
        }
        public void UpdateStartWEightDisplay(DataStore.EquipmentStatus status, Collection<WeightData> weightDatas)
        {
            this.UpdateStartWeightDisplay1(status, weightDatas[0]);
            this.UpdateStartWeightDisplay2(status, weightDatas[1]);
            this.UpdateStartWeightDisplay3(status, weightDatas[2]);
            this.UpdateStartWeightDisplay4(status, weightDatas[3]);
            this.UpdateStartWeightDisplay5(status, weightDatas[4]);
            this.UpdateStartWeightDisplay6(status, weightDatas[5]);
        }
        public void UpdateStartWeightDisplay1(DataStore.EquipmentStatus status, WeightData weightData)
        {
            string value = "";

            if (weightData.JudgmentStatus == DataStore.JudgmentStatus.Empty)
                return;

            value = Helper.DoubleToString(weightData.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelWeightValue1.Text != value)
                this.labelWeightValue1.Text = value;

            value = weightData.UnderCount.ToString();
            if (this.labelUnderCount1.Text != value)
                this.labelUnderCount1.Text = value;

            value = weightData.PassCount.ToString();
            if (this.labelPassCount1.Text != value)
                this.labelPassCount1.Text = value;

            value = weightData.OverCount.ToString();
            if (this.labelOverCount1.Text != value)
                this.labelOverCount1.Text = value;

            value = weightData.ExNGCount.ToString();
            if (this.labelExNGCount1.Text != value)
                this.labelExNGCount1.Text = value;

            value = weightData.TotalCount.ToString();
            if (this.labelTotalCount1.Text != value)
                this.labelTotalCount1.Text = value;

            value = weightData.ProductionSpeed.ToString();
            if (this.labelProductionSpeed1.Text != value)
                this.labelProductionSpeed1.Text = value;

            Helper.SetProgressBarValue(this.progressBar1, weightData.Weight, weightData.JudgmentStatus, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

            if (weightData.JudgmentStatus == DataStore.JudgmentStatus.Double)
                this.pictureBoxGap1.Visible = true;
            else
                this.pictureBoxGap1.Visible = false;
        }
        public void UpdateStartWeightDisplay2(DataStore.EquipmentStatus status, WeightData weightData)
        {
            string value = "";

            if (weightData.JudgmentStatus == DataStore.JudgmentStatus.Empty)
                return;

            value = Helper.DoubleToString(weightData.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelWeightValue2.Text != value)
                this.labelWeightValue2.Text = value;

            value = weightData.UnderCount.ToString();
            if (this.labelUnderCount2.Text != value)
                this.labelUnderCount2.Text = value;

            value = weightData.PassCount.ToString();
            if (this.labelPassCount2.Text != value)
                this.labelPassCount2.Text = value;

            value = weightData.OverCount.ToString();
            if (this.labelOverCount2.Text != value)
                this.labelOverCount2.Text = value;

            value = weightData.ExNGCount.ToString();
            if (this.labelExNGCount2.Text != value)
                this.labelExNGCount2.Text = value;

            value = weightData.TotalCount.ToString();
            if (this.labelTotalCount2.Text != value)
                this.labelTotalCount2.Text = value;

            value = weightData.ProductionSpeed.ToString();
            if (this.labelProductionSpeed2.Text != value)
                this.labelProductionSpeed2.Text = value;

            Helper.SetProgressBarValue(this.progressBar2, weightData.Weight, weightData.JudgmentStatus, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

            if (weightData.JudgmentStatus == DataStore.JudgmentStatus.Double)
                this.pictureBoxGap2.Visible = true;
            else
                this.pictureBoxGap2.Visible = false;
        }
        public void UpdateStartWeightDisplay3(DataStore.EquipmentStatus status, WeightData weightData)
        {
            string value = "";

            if (weightData.JudgmentStatus == DataStore.JudgmentStatus.Empty)
                return;

            value = Helper.DoubleToString(weightData.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelWeightValue3.Text != value)
                this.labelWeightValue3.Text = value;

            value = weightData.UnderCount.ToString();
            if (this.labelUnderCount3.Text != value)
                this.labelUnderCount3.Text = value;

            value = weightData.PassCount.ToString();
            if (this.labelPassCount3.Text != value)
                this.labelPassCount3.Text = value;

            value = weightData.OverCount.ToString();
            if (this.labelOverCount3.Text != value)
                this.labelOverCount3.Text = value;

            value = weightData.ExNGCount.ToString();
            if (this.labelExNGCount3.Text != value)
                this.labelExNGCount3.Text = value;

            value = weightData.TotalCount.ToString();
            if (this.labelTotalCount3.Text != value)
                this.labelTotalCount3.Text = value;

            value = weightData.ProductionSpeed.ToString();
            if (this.labelProductionSpeed3.Text != value)
                this.labelProductionSpeed3.Text = value;

            Helper.SetProgressBarValue(this.progressBar3, weightData.Weight, weightData.JudgmentStatus, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

            if (weightData.JudgmentStatus == DataStore.JudgmentStatus.Double)
                this.pictureBoxGap3.Visible = true;
            else
                this.pictureBoxGap3.Visible = false;
        }
        public void UpdateStartWeightDisplay4(DataStore.EquipmentStatus status, WeightData weightData)
        {
            string value = "";

            if (weightData.JudgmentStatus == DataStore.JudgmentStatus.Empty)
                return;

            value = Helper.DoubleToString(weightData.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelWeightValue4.Text != value)
                this.labelWeightValue4.Text = value;

            value = weightData.UnderCount.ToString();
            if (this.labelUnderCount4.Text != value)
                this.labelUnderCount4.Text = value;

            value = weightData.PassCount.ToString();
            if (this.labelPassCount4.Text != value)
                this.labelPassCount4.Text = value;

            value = weightData.OverCount.ToString();
            if (this.labelOverCount4.Text != value)
                this.labelOverCount4.Text = value;

            value = weightData.ExNGCount.ToString();
            if (this.labelExNGCount4.Text != value)
                this.labelExNGCount4.Text = value;

            value = weightData.TotalCount.ToString();
            if (this.labelTotalCount4.Text != value)
                this.labelTotalCount4.Text = value;

            value = weightData.ProductionSpeed.ToString();
            if (this.labelProductionSpeed4.Text != value)
                this.labelProductionSpeed4.Text = value;

            Helper.SetProgressBarValue(this.progressBar4, weightData.Weight, weightData.JudgmentStatus, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

            if (weightData.JudgmentStatus == DataStore.JudgmentStatus.Double)
                this.pictureBoxGap4.Visible = true;
            else
                this.pictureBoxGap4.Visible = false;
        }
        public void UpdateStartWeightDisplay5(DataStore.EquipmentStatus status, WeightData weightData)
        {
            string value = "";

            if (weightData.JudgmentStatus == DataStore.JudgmentStatus.Empty)
                return;

            value = Helper.DoubleToString(weightData.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelWeightValue5.Text != value)
                this.labelWeightValue5.Text = value;

            value = weightData.UnderCount.ToString();
            if (this.labelUnderCount5.Text != value)
                this.labelUnderCount5.Text = value;

            value = weightData.PassCount.ToString();
            if (this.labelPassCount5.Text != value)
                this.labelPassCount5.Text = value;

            value = weightData.OverCount.ToString();
            if (this.labelOverCount5.Text != value)
                this.labelOverCount5.Text = value;

            value = weightData.ExNGCount.ToString();
            if (this.labelExNGCount5.Text != value)
                this.labelExNGCount5.Text = value;

            value = weightData.TotalCount.ToString();
            if (this.labelTotalCount5.Text != value)
                this.labelTotalCount5.Text = value;

            value = weightData.ProductionSpeed.ToString();
            if (this.labelProductionSpeed5.Text != value)
                this.labelProductionSpeed5.Text = value;

            Helper.SetProgressBarValue(this.progressBar5, weightData.Weight, weightData.JudgmentStatus, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

            if (weightData.JudgmentStatus == DataStore.JudgmentStatus.Double)
                this.pictureBoxGap5.Visible = true;
            else
                this.pictureBoxGap5.Visible = false;
        }
        public void UpdateStartWeightDisplay6(DataStore.EquipmentStatus status, WeightData weightData)
        {
            string value = "";

            if (weightData.JudgmentStatus == DataStore.JudgmentStatus.Empty)
                return;

            value = Helper.DoubleToString(weightData.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelWeightValue6.Text != value)
                this.labelWeightValue6.Text = value;

            value = weightData.UnderCount.ToString();
            if (this.labelUnderCount6.Text != value)
                this.labelUnderCount6.Text = value;

            value = weightData.PassCount.ToString();
            if (this.labelPassCount6.Text != value)
                this.labelPassCount6.Text = value;

            value = weightData.OverCount.ToString();
            if (this.labelOverCount6.Text != value)
                this.labelOverCount6.Text = value;

            value = weightData.ExNGCount.ToString();
            if (this.labelExNGCount6.Text != value)
                this.labelExNGCount6.Text = value;

            value = weightData.TotalCount.ToString();
            if (this.labelTotalCount6.Text != value)
                this.labelTotalCount6.Text = value;

            value = weightData.ProductionSpeed.ToString();
            if (this.labelProductionSpeed6.Text != value)
                this.labelProductionSpeed6.Text = value;

            Helper.SetProgressBarValue(this.progressBar6, weightData.Weight, weightData.JudgmentStatus, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

            if (weightData.JudgmentStatus == DataStore.JudgmentStatus.Double)
                this.pictureBoxGap6.Visible = true;
            else
                this.pictureBoxGap6.Visible = false;
        }
        #endregion

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

            if (button == null)
                return;

            if (button == this.buttonZero1)
                this.ParentForm.ParentForm.TransferData(CommunicationCommand.Zero, CommunicationID.SubBoard1);
            else if (button == this.buttonZero2)
                this.ParentForm.ParentForm.TransferData(CommunicationCommand.Zero, CommunicationID.SubBoard2);
            else if (button == this.buttonZero3)
                this.ParentForm.ParentForm.TransferData(CommunicationCommand.Zero, CommunicationID.SubBoard3);
            else if (button == this.buttonZero4)
                this.ParentForm.ParentForm.TransferData(CommunicationCommand.Zero, CommunicationID.SubBoard4);
            else if (button == this.buttonZero5)
                this.ParentForm.ParentForm.TransferData(CommunicationCommand.Zero, CommunicationID.SubBoard5);
            else
                this.ParentForm.ParentForm.TransferData(CommunicationCommand.Zero, CommunicationID.SubBoard6);
        }
        #endregion
    }
}
