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 ControlMainDisplayEachBarGraph15 : UserControl
    {
        #region Field
        private FormMainDisplay m_ParentForm;

        private Collection<SmartLabel> CollectionLabelPassCNT;
        private Collection<SmartLabel> CollectionLabelNgCNT;
        private Collection<SmartLabel> CollectionLabelWeightValue;
        private Collection<SmartProgressBar> CollectionProgressBar;
        private Collection<PictureBox> CollectionPictureBoxZero;
        private Collection<PictureBox> CollectionPictureBoxBypass;
        #endregion

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

            this.ParentForm = parent;

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

        #region Property
        public FormMainDisplay 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.CollectionLabelPassCNT = new Collection<SmartLabel>();
            this.CollectionLabelNgCNT = new Collection<SmartLabel>();
            this.CollectionLabelWeightValue = new Collection<SmartLabel>();
            this.CollectionPictureBoxZero = new Collection<PictureBox>();
            this.CollectionPictureBoxBypass = new Collection<PictureBox>();
            this.CollectionProgressBar = new Collection<SmartProgressBar>();

            for (int i = 1; i <= this.EquipmentColumns; i++)
            {
                this.CollectionLabelPassCNT.Add(FindByNameUtil.SmartLabel("labelPassCount" + i, this));
                this.CollectionLabelNgCNT.Add(FindByNameUtil.SmartLabel("labelNGCount" + i, this));
                this.CollectionLabelWeightValue.Add(FindByNameUtil.SmartLabel("labelWeightValue" + i, this));
                this.CollectionPictureBoxZero.Add(FindByNameUtil.PictureBox("pictureBox" + i, this));
                this.CollectionPictureBoxBypass.Add(FindByNameUtil.PictureBox("pictureBoxBypass" + i, this));
                this.CollectionProgressBar.Add(FindByNameUtil.SmartProgressBar("progressBar" + i, this));
            }
        }
        private void InitializeControl()
        {
            for (int i = 0; i < this.EquipmentColumns; i++)
            {
                // Label PassCount
                this.CollectionLabelPassCNT[i].Text = "0";
                // Label NgCount
                this.CollectionLabelNgCNT[i].Text = "0";
                // Label Weight
                this.CollectionLabelWeightValue[i].Text = Helper.StringToDecimalPlaces("0", this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                // ProgressBar
                this.CollectionProgressBar[i].Value = this.CollectionProgressBar[i].Minimum;
                // PictureBox Zero
                this.CollectionPictureBoxZero[i].Visible = false;
                // PictureBox Bypass
                this.CollectionPictureBoxBypass[i].Visible = false;
            }

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

            for (int i = 0; i < this.EquipmentColumns; i++)
            {
                // Pass Count
                value = weightdatas[i].PassCount.ToString();
                if (this.CollectionLabelPassCNT[i].Text != value)
                    this.CollectionLabelPassCNT[i].Text = value;

                // NG Count
                value = weightdatas[i].TotalNGCount.ToString();
                if (this.CollectionLabelNgCNT[i].Text != value)
                    this.CollectionLabelNgCNT[i].Text = value;
            }

            this.ParentForm.SetTotalCount();
        }

        public void RescaleControl()
        {
            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()
        {
            for (int i = 0; i < this.EquipmentColumns; i++)
            {
                // Label WeightValue
                this.CollectionLabelWeightValue[i].Text = Helper.StringToDecimalPlaces("0", this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                this.CollectionLabelWeightValue[i].ForeColor = Color.Black;
                // ProgressBar
                this.CollectionProgressBar[i].Value = this.CollectionProgressBar[i].Minimum;
                // WeightData
                this.ParentForm.ParentForm.CollectionWeightData[i].ClearCount();
            }

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

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

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

            for (int i = 0; i < this.EquipmentColumns; i++)
            {
                // Label WeightValue
                value = Helper.StringToDecimalPlaces("0", this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                if (this.CollectionLabelWeightValue[i].Text != value)
                    this.CollectionLabelWeightValue[i].Text = value;
                // ProgressBar
                this.CollectionProgressBar[i].Value = this.CollectionProgressBar[i].Minimum;
            }
        }
        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 UpdateBypassDisplay(Collection<WeightData> weightData)
        {
            for (int i = 0; i < this.EquipmentColumns; i++)
            {
                if (weightData[i].IsBypassMode == true)
                    this.CollectionPictureBoxBypass[i].Visible = true;
                else
                    this.CollectionPictureBoxBypass[i].Visible = false;
            }
        }
        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.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 (this.CollectionLabelWeightValue[i].ForeColor != Color.Black)
                        this.CollectionLabelWeightValue[i].ForeColor = Color.Black;

                    // 영점표시
                    if (weightDatas[i].Status == DataStore.WeightStatus.WeightZero)
                        this.CollectionPictureBoxZero[i].Visible = true;
                    else
                        this.CollectionPictureBoxZero[i].Visible = false;
                }
            }
            else
            {
                for (int i = 0; i < this.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)
        {
            string value = "";
            bool empty = false;

            for (int i = 0; i < this.EquipmentColumns; i++)
            {
                if (weightDatas[i].JudgmentStatus == DataStore.JudgmentStatus.None)
                    continue;

                if (weightDatas[i].JudgmentStatus == DataStore.JudgmentStatus.Empty)
                    empty = true;
                else
                    empty = false;

                // Weight Value
                value = Helper.DoubleToString(weightDatas[i].Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                if (this.CollectionLabelWeightValue[i].Text != value)
                    this.CollectionLabelWeightValue[i].Text = value;

                if (empty == false)
                {
                    // Weight
                    this.CollectionLabelWeightValue[i].ForeColor = Color.Black;
                    // Pass Count
                    value = weightDatas[i].PassCount.ToString();
                    if (this.CollectionLabelPassCNT[i].Text != value)
                        this.CollectionLabelPassCNT[i].Text = value;
                    // Ng Count
                    value = weightDatas[i].TotalNGCount.ToString();
                    if (this.CollectionLabelNgCNT[i].Text != value)
                        this.CollectionLabelNgCNT[i].Text = value;
                    // ProgressBar
                    Helper.SetProgressBarValue(this.CollectionProgressBar[i], weightDatas[i].Weight, weightDatas[i].JudgmentStatus, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                }
                else
                {
                    this.CollectionLabelWeightValue[i].ForeColor = Color.Gray;
                    this.CollectionProgressBar[i].Value = this.CollectionProgressBar[i].Minimum;
                    this.CollectionProgressBar[i].BarColor1 = Color.Lime;
                }
            }
        }
        #endregion
    }
}
