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 INT51DB.DialogForms;
using INT51DB.Forms;
using INT51DB_ImageDll;

namespace INT51DB.Controls
{
    public partial class ControlCenterBasicDataStatistics : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;

        private int SelectedProductNo;
        private WeightData SelectedWeightData;
        private ProductItem SelectedProductItem;

        private Collection<SmartLabel> CollectionNormalDistributionLabel;
        private Collection<SmartProgressBar> CollectionNormalDistributionProgressBar;
        #endregion

        #region Constructor
        public ControlCenterBasicDataStatistics(FormMenu parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

            this.InitializeDesign();
            this.DefaultSetting();
        }
        #endregion

        #region Property
        public FormMenu ParentForm
        {
            get { return this.m_ParentForm; }
            private set { this.m_ParentForm = value; }
        }
        #endregion

        #region Method
        public void InitializeDesign()
        {
            Class1 images = new Class1();

            if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
            }
            else
            {
            }
        }
        private void DefaultSetting()
        {
            this.SelectedProductNo = 1;
            this.SelectedWeightData = new WeightData();
            this.SelectedProductItem = new ProductItem();

            this.CollectionNormalDistributionLabel = new Collection<SmartLabel>();
            this.CollectionNormalDistributionLabel.Clear();
            this.CollectionNormalDistributionLabel.Add(this.labelNormalDistributionRange1);
            this.CollectionNormalDistributionLabel.Add(this.labelNormalDistributionRange2);
            this.CollectionNormalDistributionLabel.Add(this.labelNormalDistributionRange3);
            this.CollectionNormalDistributionLabel.Add(this.labelNormalDistributionRange4);
            this.CollectionNormalDistributionLabel.Add(this.labelNormalDistributionRange5);
            this.CollectionNormalDistributionLabel.Add(this.labelNormalDistributionRange6);
            this.CollectionNormalDistributionLabel.Add(this.labelNormalDistributionRange7);
            this.CollectionNormalDistributionLabel.Add(this.labelNormalDistributionRange8);
            this.CollectionNormalDistributionLabel.Add(this.labelNormalDistributionRange9);

            this.CollectionNormalDistributionProgressBar = new Collection<SmartProgressBar>();
            this.CollectionNormalDistributionProgressBar.Clear();
            this.CollectionNormalDistributionProgressBar.Add(this.smartProgressBar1);
            this.CollectionNormalDistributionProgressBar.Add(this.smartProgressBar2);
            this.CollectionNormalDistributionProgressBar.Add(this.smartProgressBar3);
            this.CollectionNormalDistributionProgressBar.Add(this.smartProgressBar4);
            this.CollectionNormalDistributionProgressBar.Add(this.smartProgressBar5);
            this.CollectionNormalDistributionProgressBar.Add(this.smartProgressBar6);
            this.CollectionNormalDistributionProgressBar.Add(this.smartProgressBar7);
            this.CollectionNormalDistributionProgressBar.Add(this.smartProgressBar8);
            this.CollectionNormalDistributionProgressBar.Add(this.smartProgressBar9);
        }

        private void UpdateDisplay(WeightData data, ProductItem item)
        {
            string value = "";

            #region Range
            value = Helper.StringToDecimalPlaces(item.UnderRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelUnderRange.Text != value)
                this.labelUnderRange.Text = value;

            value = Helper.StringToDecimalPlaces(item.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelPassRange.Text != value)
                this.labelPassRange.Text = value;

            value = Helper.StringToDecimalPlaces(item.OverRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelOverRange.Text != value)
                this.labelOverRange.Text = value;
            #endregion

            #region Count
            value = data.UnderCount.ToString();
            if (this.labelUnderCount.Text != value)
                this.labelUnderCount.Text = value;

            value = data.PassCount.ToString();
            if (this.labelPassCount.Text != value)
                this.labelPassCount.Text = value;

            value = data.OverCount.ToString();
            if (this.labelOverCount.Text != value)
                this.labelOverCount.Text = value;
            #endregion

            #region Average
            value = Helper.DoubleToString(data.UnderAverage, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelUnderAverage.Text != value)
                this.labelUnderAverage.Text = value;

            value = Helper.DoubleToString(data.PassAverage, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelPassAverage.Text != value)
                this.labelPassAverage.Text = value;

            value = Helper.DoubleToString(data.OverAverage, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelOverAverage.Text != value)
                this.labelOverAverage.Text = value;
            #endregion

            #region Sum Weight
            value = data.UnderSumWeightKG;
            if (this.labelUnderSumWeight.Text != value)
                this.labelUnderSumWeight.Text = value;

            value = data.PassSumWeightKG;
            if (this.labelPassSumWeight.Text != value)
                this.labelPassSumWeight.Text = value;

            value = data.OverSumWeightKG;
            if (this.labelOverSumWeight.Text != value)
                this.labelOverSumWeight.Text = value;
            #endregion

            #region Ratio
            value = data.UnderRatio;
            if (this.labelUnderRatio.Text != value)
                this.labelUnderRatio.Text = value;

            value = data.PassRatio;
            if (this.labelPassRatio.Text != value)
                this.labelPassRatio.Text = value;

            value = data.OverRatio;
            if (this.labelOverRatio.Text != value)
                this.labelOverRatio.Text = value;
            #endregion

            #region NormalDistribution
            for (int i = 0; i < item.CollectionNormalDistributionViewRange.Count; i++)
            {
                value = Helper.StringToDecimalPlaces(item.CollectionNormalDistributionViewRange[i].ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                if (this.CollectionNormalDistributionLabel[i].Text != value)
                    this.CollectionNormalDistributionLabel[i].Text = value;
            }

            for (int i = 0; i < this.CollectionNormalDistributionProgressBar.Count; i++)
            {
                if (this.CollectionNormalDistributionProgressBar[i].Maximum != data.TotalCount)
                    this.CollectionNormalDistributionProgressBar[i].Maximum = data.TotalCount;

                if(this.CollectionNormalDistributionProgressBar[i].Value != data.CollectionNormalDistributionCount[i])
                    this.CollectionNormalDistributionProgressBar[i].Value = data.CollectionNormalDistributionCount[i];
            }
            #endregion

            value = data.TotalNGCount.ToString();
            if (this.labelNGCount.Text != value)
                this.labelNGCount.Text = value;

            value = data.TotalCount.ToString();
            if (this.labelTotalCount.Text != value)
                this.labelTotalCount.Text = value;

            if (data.StartTime.Year == 1111 || data.StopTime.Year == 1111)
                data.StartTime = data.StopTime = DateTime.Now;

            value = data.StartTime.ToString("yyyy-MM-dd HH:mm:ss");
            if (this.labelStartTime.Text != value)
                this.labelStartTime.Text = value;

            value = data.StopTime.ToString("yyyy-MM-dd HH:mm:ss");
            if (this.labelStopTime.Text != value)
                this.labelStopTime.Text = value;
        }

        public void DisplayRefresh(DataStore.UserGroup group)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = DataStore.DisplayStore.BasicDataStatistics;
            this.ParentForm.ParentForm.SetDisplayMode(DataStore.DisplayMode.Menu);
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);

            this.SelectedProductNo = this.ParentForm.ParentForm.SystemConfig.ProductNumber;

            if (this.buttonItemNo.Text != this.SelectedProductNo.ToString())
                this.buttonItemNo.Text = this.SelectedProductNo.ToString();

            this.ParentForm.ParentForm.LoadCounterFile(ref this.SelectedWeightData, this.SelectedProductNo - 1);
            this.ParentForm.ParentForm.LoadProductFile(ref this.SelectedProductItem, this.SelectedProductNo - 1);
            this.UpdateDisplay(this.SelectedWeightData, this.SelectedProductItem);
        }
        #endregion

        #region Event Handler
        private void buttonItemNo_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.buttonItemNo.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 1 || myKeyPad.doubleValue > 1000)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.buttonItemNo.Text = myKeyPad.StringValue;
                    this.SelectedProductNo = myKeyPad.IntValue;

                    this.ParentForm.ParentForm.LoadCounterFile(ref this.SelectedWeightData, this.SelectedProductNo - 1);
                    this.ParentForm.ParentForm.LoadProductFile(ref this.SelectedProductItem, this.SelectedProductNo - 1);
                    this.UpdateDisplay(this.SelectedWeightData, this.SelectedProductItem);
                }
            }
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig.Language, 8);
            if (myDlg.ShowDialog() == DialogResult.Yes)
            {
                this.ParentForm.ParentForm.ChildFormMainDisplay.Clear();

                this.ParentForm.ParentForm.LoadCounterFile(ref this.SelectedWeightData, this.SelectedProductNo - 1);
                this.ParentForm.ParentForm.LoadProductFile(ref this.SelectedProductItem, this.SelectedProductNo - 1);
                this.UpdateDisplay(this.SelectedWeightData, this.SelectedProductItem);
            }
        }
        #endregion
    }
}
