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

using ITC81DB_2_ImageDll;
using ITC81DB_2.Forms;

namespace ITC81DB_2.Controls
{
    public partial class ControlMainDataStatistics : UserControl
    {
        #region Field
        private FormMainDisplay m_ParentForm;
        #endregion

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

            this.ParentForm = parent;

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

        #region Property
        public FormMainDisplay 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 == Define.E_LanguageID.English)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engFormMainDisplayStatistics));
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Chinese)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnFormMainDisplayStatistics));
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.spnFormMainDisplayStatistics));
            }
            else
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korFormMainDisplayStatistics));
            }
        }
        private void DefaultSetting()
        {

        }

        private void SetProduct1(ProductItem item)
        {
            string underValue = "", passValue = "", overValue = "";

            switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
            {
                case Define.E_WeightInputMode.Weight:
                    underValue = Helper.StringToDecimalPlaces(item.UnderRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    passValue = Helper.StringToDecimalPlaces(item.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    overValue = Helper.StringToDecimalPlaces(item.OverRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    break;
                case Define.E_WeightInputMode.Deviation:
                    underValue = Helper.StringToDecimalPlaces(item.UnderRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    passValue = Helper.StringToDecimalPlaces(item.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    overValue = Helper.StringToDecimalPlaces(item.OverRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    overValue = string.Format("+{0}", overValue);
                    break;
                default:
                    break;
            }

            if (this.labelUnderRange1.Text != underValue)
                this.labelUnderRange1.Text = underValue;

            if (this.labelPassRange1.Text != passValue)
                this.labelPassRange1.Text = passValue;

            if (this.labelOverRange1.Text != overValue)
                this.labelOverRange1.Text = overValue;
        }
        private void SetProduct2(ProductItem item)
        {
            string underValue = "", passValue = "", overValue = "";

            switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
            {
                case Define.E_WeightInputMode.Weight:
                    underValue = Helper.StringToDecimalPlaces(item.UnderRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    passValue = Helper.StringToDecimalPlaces(item.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    overValue = Helper.StringToDecimalPlaces(item.OverRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    break;
                case Define.E_WeightInputMode.Deviation:
                    underValue = Helper.StringToDecimalPlaces(item.UnderRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    passValue = Helper.StringToDecimalPlaces(item.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    overValue = Helper.StringToDecimalPlaces(item.OverRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    overValue = string.Format("+{0}", overValue);
                    break;
                default:
                    break;
            }

            if (this.labelUnderRange2.Text != underValue)
                this.labelUnderRange2.Text = underValue;

            if (this.labelPassRange2.Text != passValue)
                this.labelPassRange2.Text = passValue;

            if (this.labelOverRange2.Text != overValue)
                this.labelOverRange2.Text = overValue;
        }
        private void SetCount1(WeightData data)
        {
            string value = "";

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

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

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

            value = data.TotalUnderOverCount.ToString();
            if (this.labelNgCount1.Text != value)
                this.labelNgCount1.Text = value;

            value = data.ETCExNGCount.ToString();
            if (this.labelExNgCount1.Text != value)
                this.labelExNgCount1.Text = value;

            value = data.TotalCount.ToString();
            if (this.labelTotalCount1.Text != value)
                this.labelTotalCount1.Text = value;
            #endregion

            #region SumWeight
            value = data.UnderSumWeightKG;
            if (this.labelUnderSumWeight1.Text != value)
                this.labelUnderSumWeight1.Text = value;

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

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

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

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

            value = Helper.DoubleToString(data.OverAverage, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelOverAverage1.Text != value)
                this.labelOverAverage1.Text = value;
            #endregion
        }
        private void SetCount2(WeightData data)
        {
            string value = "";

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

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

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

            value = data.TotalUnderOverCount.ToString();
            if (this.labelNgCount2.Text != value)
                this.labelNgCount2.Text = value;

            value = data.ETCExNGCount.ToString();
            if (this.labelExNgCount2.Text != value)
                this.labelExNgCount2.Text = value;

            value = data.TotalCount.ToString();
            if (this.labelTotalCount2.Text != value)
                this.labelTotalCount2.Text = value;
            #endregion

            #region SumWeight
            value = data.UnderSumWeightKG;
            if (this.labelUnderSumWeight2.Text != value)
                this.labelUnderSumWeight2.Text = value;

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

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

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

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

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

        private void UpdateDisplayUser(UserItem user)
        {
            switch (user.Group)
            {
                case Define.E_UserGroup.LogOut:
                    break;
                case Define.E_UserGroup.Level1:
                    break;
                case Define.E_UserGroup.Level2:
                    break;
                case Define.E_UserGroup.Level3:
                    break;
                case Define.E_UserGroup.Developer:
                    break;
                default:
                    break;
            }
        }

        public void UpdateCurrentProductDisplay(Define.E_EquipmentStatus status, ProductItem pItem1, ProductItem pItem2, WeightData wData1, WeightData wData2)
        {
            this.SetProduct1(pItem1);
            this.SetProduct2(pItem2);
            this.SetCount1(wData1);
            this.SetCount2(wData2);
        }
        public void UpdateStartWeightDisplay1(Define.E_EquipmentStatus status, WeightData data)
        {
            string value = "";

            if (data.JudgmentStatus == Define.E_JudgmentStatus.Empty)
                return;

            this.SetCount1(data);
        }
        public void UpdateStartWeightDisplay2(Define.E_EquipmentStatus status, WeightData data)
        {
            string value = "";

            if (data.JudgmentStatus == Define.E_JudgmentStatus.Empty)
                return;

            this.SetCount2(data);
        }

        public void Clear()
        {
            this.SetCount1(this.ParentForm.ParentForm.CurrentWeightData1);
            this.SetCount2(this.ParentForm.ParentForm.CurrentWeightData2);
        }
        public void DisplayRefresh(SystemStatus status)
        {
            this.UpdateDisplayUser(status.CurrentUser);

            this.SetProduct1(this.ParentForm.ParentForm.CurrentProductItem1);
            this.SetProduct2(this.ParentForm.ParentForm.CurrentProductItem2);
            this.SetCount1(this.ParentForm.ParentForm.CurrentWeightData1);
            this.SetCount2(this.ParentForm.ParentForm.CurrentWeightData2);
        }
        #endregion

        #region Event Handler
        #endregion
    }
}
