using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using SmartX;
using INT69DB_2A.Forms;

namespace INT69DB_2A.Controls
{
    public partial class ControlMainDisplayTable2 : UserControl
    {
        #region Field
        private FormMainDisplay m_ParentForm;

        private Color WeightOverColor = Color.Red;
        private Color WeightStandardColor = Color.Black;
        private Color WeightUnderColor = Color.Red;
        private Color WeightExNGColor = Color.Blue;

        private Collection<PictureBox> CollectionPictureBoxZero;
        private Collection<PictureBox> CollectionPictureBoxBypass;
        private Collection<SmartLabel> CollectionLabelWeightValue1;
        private Collection<SmartLabel> CollectionLabelWeightValue2;
        private Collection<WeightStorageItem> CollectionWeightStorageItem;
        #endregion

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

        #region Method
        private void CreateCollection()
        {
            // PictureBoxZero
            this.CollectionPictureBoxZero = new Collection<PictureBox>();
            this.CollectionPictureBoxZero.Clear();
            this.CollectionPictureBoxZero.Add(this.pictureBox1);
            this.CollectionPictureBoxZero.Add(this.pictureBox2);

            // PictureBoxBypass
            this.CollectionPictureBoxBypass = new Collection<PictureBox>();
            this.CollectionPictureBoxBypass.Clear();
            this.CollectionPictureBoxBypass.Add(this.pictureBoxBypass1);
            this.CollectionPictureBoxBypass.Add(this.pictureBoxBypass2);

            // WeightStorage
            this.CollectionWeightStorageItem = new Collection<WeightStorageItem>();
            this.CollectionWeightStorageItem.Clear();
            for (int i = 0; i < 3; i++)
                this.CollectionWeightStorageItem.Add(new WeightStorageItem());

            #region Add Label
            this.CollectionLabelWeightValue1 = new Collection<SmartLabel>();
            this.CollectionLabelWeightValue1.Clear();
            this.CollectionLabelWeightValue1.Add(this.label1Col1Row);
            this.CollectionLabelWeightValue1.Add(this.label1Col2Row);
            this.CollectionLabelWeightValue1.Add(this.label1Col3Row);
            this.CollectionLabelWeightValue1.Add(this.label1Col4Row);
            this.CollectionLabelWeightValue1.Add(this.label1Col5Row);
            this.CollectionLabelWeightValue1.Add(this.label1Col6Row);
            this.CollectionLabelWeightValue1.Add(this.label1Col7Row);
            this.CollectionLabelWeightValue1.Add(this.label1Col8Row);
            this.CollectionLabelWeightValue1.Add(this.label1Col9Row);
            this.CollectionLabelWeightValue1.Add(this.label1Col10Row);
            this.CollectionLabelWeightValue1.Add(this.label1Col5Avg);
            this.CollectionLabelWeightValue1.Add(this.label1AllAvg);

            this.CollectionLabelWeightValue2 = new Collection<SmartLabel>();
            this.CollectionLabelWeightValue2.Clear();
            this.CollectionLabelWeightValue2.Add(this.label2Col1Row);
            this.CollectionLabelWeightValue2.Add(this.label2Col2Row);
            this.CollectionLabelWeightValue2.Add(this.label2Col3Row);
            this.CollectionLabelWeightValue2.Add(this.label2Col4Row);
            this.CollectionLabelWeightValue2.Add(this.label2Col5Row);
            this.CollectionLabelWeightValue2.Add(this.label2Col6Row);
            this.CollectionLabelWeightValue2.Add(this.label2Col7Row);
            this.CollectionLabelWeightValue2.Add(this.label2Col8Row);
            this.CollectionLabelWeightValue2.Add(this.label2Col9Row);
            this.CollectionLabelWeightValue2.Add(this.label2Col10Row);
            this.CollectionLabelWeightValue2.Add(this.label2Col5Avg);
            this.CollectionLabelWeightValue2.Add(this.label2AllAvg);
            #endregion
        }
        private void InitializeControl()
        {
            foreach (PictureBox picture in this.CollectionPictureBoxZero)
                picture.Visible = false;

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

            for (int i = 0; i < 12; i++)
            {
                this.SetLabelProperty(this.CollectionLabelWeightValue1[i]);
                this.SetLabelProperty(this.CollectionLabelWeightValue2[i]);
            }

            this.UpdateLabelDisplay();
        }
        private void SetLabelProperty(Control label)
        {
            label.Font = new Font("Tahoma", 16, FontStyle.Bold);
        }
        private void SetLabelText(SmartLabel label, double value, DataStore.JudgmentStatus judge)
        {
            string sValue = "";
            Color labelColor = Color.Black;

            //if (value < Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem.UnderRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces))
            //{
            //    if (label.ForeColor != this.WeightUnderColor)
            //        label.ForeColor = this.WeightUnderColor;
            //}
            //else if (value > Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem.OverRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces))
            //{
            //    if (label.ForeColor != this.WeightOverColor)
            //        label.ForeColor = this.WeightOverColor;
            //}
            //else
            //{
            //    if (label.ForeColor != this.WeightStandardColor)
            //        label.ForeColor = this.WeightStandardColor;
            //}

            if (judge == DataStore.JudgmentStatus.Over)
                labelColor = this.WeightOverColor;
            else if (judge == DataStore.JudgmentStatus.Under)
                labelColor = this.WeightUnderColor;
            else if (judge == DataStore.JudgmentStatus.EXNg)
                labelColor = this.WeightExNGColor;
            else
                labelColor = this.WeightStandardColor;

            sValue = Helper.DoubleToString(value, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

            if (label.ForeColor != labelColor)
                label.ForeColor = labelColor;

            if (label.Text != sValue)
                label.Text = sValue;
        }

        public void Clear()
        {
            foreach (WeightStorageItem weight in this.CollectionWeightStorageItem)
                weight.AllClear();

            this.UpdateLabelDisplay();
        }

        private void UpdateLabelDisplay()
        {
            this.SetLabelText(this.label1Col1Row, this.CollectionWeightStorageItem[0].Weight1, this.CollectionWeightStorageItem[0].Grade1);
            this.SetLabelText(this.label1Col2Row, this.CollectionWeightStorageItem[0].Weight2, this.CollectionWeightStorageItem[0].Grade2);
            this.SetLabelText(this.label1Col3Row, this.CollectionWeightStorageItem[0].Weight3, this.CollectionWeightStorageItem[0].Grade3);
            this.SetLabelText(this.label1Col4Row, this.CollectionWeightStorageItem[0].Weight4, this.CollectionWeightStorageItem[0].Grade4);
            this.SetLabelText(this.label1Col5Row, this.CollectionWeightStorageItem[0].Weight5, this.CollectionWeightStorageItem[0].Grade5);
            this.SetLabelText(this.label1Col6Row, this.CollectionWeightStorageItem[0].Weight6, this.CollectionWeightStorageItem[0].Grade6);
            this.SetLabelText(this.label1Col7Row, this.CollectionWeightStorageItem[0].Weight7, this.CollectionWeightStorageItem[0].Grade7);
            this.SetLabelText(this.label1Col8Row, this.CollectionWeightStorageItem[0].Weight8, this.CollectionWeightStorageItem[0].Grade8);
            this.SetLabelText(this.label1Col9Row, this.CollectionWeightStorageItem[0].Weight9, this.CollectionWeightStorageItem[0].Grade9);
            this.SetLabelText(this.label1Col10Row, this.CollectionWeightStorageItem[0].Weight10, this.CollectionWeightStorageItem[0].Grade10);
            this.SetLabelText(this.label1Col5Avg, this.CollectionWeightStorageItem[0].GetWeightTenAverage(), DataStore.JudgmentStatus.Empty);
            this.SetLabelText(this.label1AllAvg, this.CollectionWeightStorageItem[0].GetWeightTotalAverage(), DataStore.JudgmentStatus.Empty);

            this.SetLabelText(this.label2Col1Row, this.CollectionWeightStorageItem[1].Weight1, this.CollectionWeightStorageItem[1].Grade1);
            this.SetLabelText(this.label2Col2Row, this.CollectionWeightStorageItem[1].Weight2, this.CollectionWeightStorageItem[1].Grade2);
            this.SetLabelText(this.label2Col3Row, this.CollectionWeightStorageItem[1].Weight3, this.CollectionWeightStorageItem[1].Grade3);
            this.SetLabelText(this.label2Col4Row, this.CollectionWeightStorageItem[1].Weight4, this.CollectionWeightStorageItem[1].Grade4);
            this.SetLabelText(this.label2Col5Row, this.CollectionWeightStorageItem[1].Weight5, this.CollectionWeightStorageItem[1].Grade5);
            this.SetLabelText(this.label2Col6Row, this.CollectionWeightStorageItem[1].Weight6, this.CollectionWeightStorageItem[1].Grade6);
            this.SetLabelText(this.label2Col7Row, this.CollectionWeightStorageItem[1].Weight7, this.CollectionWeightStorageItem[1].Grade7);
            this.SetLabelText(this.label2Col8Row, this.CollectionWeightStorageItem[1].Weight8, this.CollectionWeightStorageItem[1].Grade8);
            this.SetLabelText(this.label2Col9Row, this.CollectionWeightStorageItem[1].Weight9, this.CollectionWeightStorageItem[1].Grade9);
            this.SetLabelText(this.label2Col10Row, this.CollectionWeightStorageItem[1].Weight10, this.CollectionWeightStorageItem[1].Grade10);
            this.SetLabelText(this.label2Col5Avg, this.CollectionWeightStorageItem[1].GetWeightTenAverage(), DataStore.JudgmentStatus.Empty);
            this.SetLabelText(this.label2AllAvg, this.CollectionWeightStorageItem[1].GetWeightTotalAverage(), DataStore.JudgmentStatus.Empty);
        }

        public void UpdateEquipmentStatusDisplay(DataStore.EquipmentStatus status)
        {
        }
        public void UpdateCurrentProductDisplay(DataStore.EquipmentStatus status, Collection<WeightData> weightDatas)
        {
            // 초기화
        }
        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++)
                {
                    // 영점표시
                    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.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]);
        }
        public void UpdateStartWeightDisplay1(DataStore.EquipmentStatus status, WeightData weightData)
        {
            string value = "";

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

            this.CollectionWeightStorageItem[0].SetValue(weightData.Weight, weightData.JudgmentStatus);

            this.SetLabelText(this.label1Col1Row, this.CollectionWeightStorageItem[0].Weight1, this.CollectionWeightStorageItem[0].Grade1);
            this.SetLabelText(this.label1Col2Row, this.CollectionWeightStorageItem[0].Weight2, this.CollectionWeightStorageItem[0].Grade2);
            this.SetLabelText(this.label1Col3Row, this.CollectionWeightStorageItem[0].Weight3, this.CollectionWeightStorageItem[0].Grade3);
            this.SetLabelText(this.label1Col4Row, this.CollectionWeightStorageItem[0].Weight4, this.CollectionWeightStorageItem[0].Grade4);
            this.SetLabelText(this.label1Col5Row, this.CollectionWeightStorageItem[0].Weight5, this.CollectionWeightStorageItem[0].Grade5);
            this.SetLabelText(this.label1Col6Row, this.CollectionWeightStorageItem[0].Weight6, this.CollectionWeightStorageItem[0].Grade6);
            this.SetLabelText(this.label1Col7Row, this.CollectionWeightStorageItem[0].Weight7, this.CollectionWeightStorageItem[0].Grade7);
            this.SetLabelText(this.label1Col8Row, this.CollectionWeightStorageItem[0].Weight8, this.CollectionWeightStorageItem[0].Grade8);
            this.SetLabelText(this.label1Col9Row, this.CollectionWeightStorageItem[0].Weight9, this.CollectionWeightStorageItem[0].Grade9);
            this.SetLabelText(this.label1Col10Row, this.CollectionWeightStorageItem[0].Weight10, this.CollectionWeightStorageItem[0].Grade10);
            this.SetLabelText(this.label1Col5Avg, this.CollectionWeightStorageItem[0].GetWeightTenAverage(), DataStore.JudgmentStatus.Empty);
            this.SetLabelText(this.label1AllAvg, this.CollectionWeightStorageItem[0].GetWeightTotalAverage(), DataStore.JudgmentStatus.Empty);
        }
        public void UpdateStartWeightDisplay2(DataStore.EquipmentStatus status, WeightData weightData)
        {
            string value = "";

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

            this.CollectionWeightStorageItem[1].SetValue(weightData.Weight, weightData.JudgmentStatus);

            this.SetLabelText(this.label2Col1Row, this.CollectionWeightStorageItem[1].Weight1, this.CollectionWeightStorageItem[1].Grade1);
            this.SetLabelText(this.label2Col2Row, this.CollectionWeightStorageItem[1].Weight2, this.CollectionWeightStorageItem[1].Grade2);
            this.SetLabelText(this.label2Col3Row, this.CollectionWeightStorageItem[1].Weight3, this.CollectionWeightStorageItem[1].Grade3);
            this.SetLabelText(this.label2Col4Row, this.CollectionWeightStorageItem[1].Weight4, this.CollectionWeightStorageItem[1].Grade4);
            this.SetLabelText(this.label2Col5Row, this.CollectionWeightStorageItem[1].Weight5, this.CollectionWeightStorageItem[1].Grade5);
            this.SetLabelText(this.label2Col6Row, this.CollectionWeightStorageItem[1].Weight6, this.CollectionWeightStorageItem[1].Grade6);
            this.SetLabelText(this.label2Col7Row, this.CollectionWeightStorageItem[1].Weight7, this.CollectionWeightStorageItem[1].Grade7);
            this.SetLabelText(this.label2Col8Row, this.CollectionWeightStorageItem[1].Weight8, this.CollectionWeightStorageItem[1].Grade8);
            this.SetLabelText(this.label2Col9Row, this.CollectionWeightStorageItem[1].Weight9, this.CollectionWeightStorageItem[1].Grade9);
            this.SetLabelText(this.label2Col10Row, this.CollectionWeightStorageItem[1].Weight10, this.CollectionWeightStorageItem[1].Grade10);
            this.SetLabelText(this.label2Col5Avg, this.CollectionWeightStorageItem[1].GetWeightTenAverage(), DataStore.JudgmentStatus.Empty);
            this.SetLabelText(this.label2AllAvg, this.CollectionWeightStorageItem[1].GetWeightTotalAverage(), DataStore.JudgmentStatus.Empty);
        }
        #endregion
    }
}
