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

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

        private Collection<PictureBox> CollectionPictureBoxZero;
        private Collection<PictureBox> CollectionPictureBoxBypass;
        private Collection<SmartLabel> CollectionLabelTenAvg;
        private Collection<SmartLabel> CollectionLabelAllAvg;
        private Collection<SmartListBox> CollectionListBox;
        private Collection<WeightStorageItem> CollectionWeightStorageItem;
        #endregion

        #region Constructor
        public ControlMainDisplayTable16(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.CollectionWeightStorageItem = new Collection<WeightStorageItem>();
            this.CollectionPictureBoxZero = new Collection<PictureBox>();
            this.CollectionPictureBoxBypass = new Collection<PictureBox>();
            this.CollectionLabelTenAvg = new Collection<SmartLabel>();
            this.CollectionLabelAllAvg = new Collection<SmartLabel>();
            this.CollectionListBox = new Collection<SmartListBox>();

            for (int i = 1; i <= this.EquipmentColumns; i++)
            {
                this.CollectionPictureBoxZero.Add(FindByNameUtil.PictureBox("pictureBox" + i, this));
                this.CollectionPictureBoxBypass.Add(FindByNameUtil.PictureBox("pictureBoxBypass" + i, this));
                this.CollectionLabelTenAvg.Add(FindByNameUtil.SmartLabel("labelTenAvg" + i, this));
                this.CollectionLabelAllAvg.Add(FindByNameUtil.SmartLabel("labelAllAvg" + i, this));
                this.CollectionListBox.Add(FindByNameUtil.SmartListBox("listBox" + i, this));
                this.CollectionWeightStorageItem.Add(new WeightStorageItem());
            }
        }
        private void InitializeControl()
        {
            string weight = "";
            int[] iColPos = new int[2];
            iColPos[0] = 10;
            iColPos[1] = 53;

            for (int i = 0; i < this.EquipmentColumns; i++)
            {
                this.CollectionPictureBoxZero[i].Visible = false;
                this.CollectionPictureBoxBypass[i].Visible = false;
                this.CollectionLabelTenAvg[i].Text = Helper.DoubleToString(0.0, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                this.CollectionLabelAllAvg[i].Text = Helper.DoubleToString(0.0, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

                this.CollectionListBox[i].ColumnAlign = SmartListBox.COLUMNALIGNS.RIGHT;
                this.CollectionListBox[i].ColumnOffsets = iColPos;
                this.CollectionListBox[i].ColumnDelimiter = ';';
                this.CollectionListBox[i].Enabled = false;
                for (int j = 0; j < 10; j++)
                {
                    weight = Helper.DoubleToString(0.0, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    this.CollectionListBox[i].AddItem(string.Format(";{0}", weight));
                }
            }
        }

        public void Clear()
        {
            string weight = "";

            for (int i = 0; i < this.EquipmentColumns; i++)
            {
                this.CollectionWeightStorageItem[i].AllClear();
                this.CollectionListBox[i].ClearAll();
                for (int j = 0; j < 10; j++)
                {
                    weight = Helper.DoubleToString(0.0, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    this.CollectionListBox[i].AddItem(string.Format(";{0}", weight));
                }

                Helper.SetTableLabelValue(this.CollectionLabelTenAvg[i], this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig,
                   this.CollectionWeightStorageItem[i].WeightTenAverage());
                Helper.SetTableLabelValue(this.CollectionLabelAllAvg[i], this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig,
                    this.CollectionWeightStorageItem[i].WeightTotalAverage());
            }
        }

        public void UpdateEquipmentStatusDisplay(DataStore.EquipmentStatus status)
        {
        }
        public void UpdateCurrentProductDisplay(DataStore.EquipmentStatus status, Collection<WeightData> weightDatas)
        {
            // 초기화
        }
        public void UpdateBypassDisplay(Collection<WeightData> weightData)
        {
            for (int i = 0; i < this.CollectionPictureBoxBypass.Count; 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.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)
        {
            string weight = "", grd = "";

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

                this.CollectionWeightStorageItem[i].SetValue(weightDatas[i]);
                this.CollectionWeightStorageItem[i].SetQueueData();

                if (weightDatas[i].JudgmentStatus == DataStore.JudgmentStatus.Empty)
                {
                    //if (weightDatas[i].IsSamplingData == true)
                    //    grd = "ES";
                    //else
                    //    grd = "E";
                }
                else
                {
                    //grd = "●";

                    if (weightDatas[i].JudgmentStatus == DataStore.JudgmentStatus.Under)
                        grd = "●";//grd = "U";
                    else if (weightDatas[i].JudgmentStatus == DataStore.JudgmentStatus.UnderLimit)
                        grd = "●";//grd = "UL";
                    else if (weightDatas[i].JudgmentStatus == DataStore.JudgmentStatus.Over)
                        grd = "●";//grd = "O";
                    else if (weightDatas[i].JudgmentStatus == DataStore.JudgmentStatus.OverLimit)
                        grd = "●";//grd = "OL";
                    else
                        grd = "";

                    if (weightDatas[i].IsSamplingData == true)
                        grd = "●"; //grd = "S";
                }

                weight = Helper.DoubleToString(weightDatas[i].Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                this.CollectionListBox[i].AddItem(string.Format("{0};{1}", grd, weight));
                if (this.CollectionListBox[i].Items.Count > 10)
                    this.CollectionListBox[i].RemoveItem(10);

                Helper.SetTableLabelValue(this.CollectionLabelTenAvg[i], this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig,
                    this.CollectionWeightStorageItem[i].WeightTenAverage());
                Helper.SetTableLabelValue(this.CollectionLabelAllAvg[i], this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig,
                    this.CollectionWeightStorageItem[i].WeightTotalAverage());
            }
        }
        #endregion

        private void smartButton1_Click(object sender, EventArgs e)
        {
            //this.ParentForm.MainDisplayTable16_1.BringToFront();
        }
    }
}
