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

        #region Constructor
        public ControlMainDisplayDataStatistics(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 == DataStore.LanguageID.English)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engFormMainDisplayStatistics));

                this.buttonOver1.DownImage = this.buttonOver2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engLightOverDown));
                this.buttonOver1.UpImage = this.buttonOver2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engLightOverUp));
                this.buttonPass1.DownImage = this.buttonPass2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engLightPassDown));
                this.buttonPass1.UpImage = this.buttonPass2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engLightPassUp));
                this.buttonUnder1.DownImage = this.buttonUnder2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engLightUnderDown));
                this.buttonUnder1.UpImage = this.buttonUnder2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engLightUnderUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnFormMainDisplayStatistics));

                this.buttonOver1.DownImage = this.buttonOver2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnLightOverDown));
                this.buttonOver1.UpImage = this.buttonOver2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnLightOverUp));
                this.buttonPass1.DownImage = this.buttonPass2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnLightPassDown));
                this.buttonPass1.UpImage = this.buttonPass2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnLightPassUp));
                this.buttonUnder1.DownImage = this.buttonUnder2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnLightUnderDown));
                this.buttonUnder1.UpImage = this.buttonUnder2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnLightUnderUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Russian)
            {
            }
            else
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korFormMainDisplayStatistics));

                this.buttonOver1.DownImage = this.buttonOver2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korLightOverDown));
                this.buttonOver1.UpImage = this.buttonOver2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korLightOverUp));
                this.buttonPass1.DownImage = this.buttonPass2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korLightPassDown));
                this.buttonPass1.UpImage = this.buttonPass2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korLightPassUp));
                this.buttonUnder1.DownImage = this.buttonUnder2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korLightUnderDown));
                this.buttonUnder1.UpImage = this.buttonUnder2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korLightUnderUp));
            }
        }
        private void DefaultSetting()
        {

        }

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

            switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
            {
                case DataStore.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 DataStore.WeightInputMode.Deviation:
                    underValue = Helper.StringToDecimalPlaces(item.UnderRangDeviation.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;

            // NET Icon 표시
            this.UpdateDisplayIconNET1(item);
        }
        private void SetProduct2(ProductItem item)
        {
            string underValue = "", passValue = "", overValue = "";

            switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
            {
                case DataStore.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 DataStore.WeightInputMode.Deviation:
                    underValue = Helper.StringToDecimalPlaces(item.UnderRangDeviation.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;

            // NET Icon 표시
            this.UpdateDisplayIconNET2(item);
        }
        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.ExNGCount.ToString();
            if (this.labelExNgCount1.Text != value)
                this.labelExNgCount1.Text = value;

            value = data.TotalCount1.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.ExNGCount.ToString();
            if (this.labelExNgCount2.Text != value)
                this.labelExNgCount2.Text = value;

            value = data.TotalCount1.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 UpdateDisplayJudgmentIcon1(DataStore.JudgmentStatus status)
        {
            if (status == DataStore.JudgmentStatus.Under)
            {
                this.buttonUnder1.ButtonDown();
                this.buttonPass1.ButtonUp();
                this.buttonOver1.ButtonUp();
            }
            else if (status == DataStore.JudgmentStatus.Pass)
            {
                this.buttonUnder1.ButtonUp();
                this.buttonPass1.ButtonDown();
                this.buttonOver1.ButtonUp();
            }
            else if (status == DataStore.JudgmentStatus.Over)
            {
                this.buttonUnder1.ButtonUp();
                this.buttonPass1.ButtonUp();
                this.buttonOver1.ButtonDown();
            }
            else
            {
                this.buttonUnder1.ButtonUp();
                this.buttonPass1.ButtonUp();
                this.buttonOver1.ButtonUp();
            }
        }
        private void UpdateDisplayJudgmentIcon2(DataStore.JudgmentStatus status)
        {
            if (status == DataStore.JudgmentStatus.Under)
            {
                this.buttonUnder2.ButtonDown();
                this.buttonPass2.ButtonUp();
                this.buttonOver2.ButtonUp();
            }
            else if (status == DataStore.JudgmentStatus.Pass)
            {
                this.buttonUnder2.ButtonUp();
                this.buttonPass2.ButtonDown();
                this.buttonOver2.ButtonUp();
            }
            else if (status == DataStore.JudgmentStatus.Over)
            {
                this.buttonUnder2.ButtonUp();
                this.buttonPass2.ButtonUp();
                this.buttonOver2.ButtonDown();
            }
            else
            {
                this.buttonUnder2.ButtonUp();
                this.buttonPass2.ButtonUp();
                this.buttonOver2.ButtonUp();
            }
        }
        private void UpdateDisplayIconNET1(ProductItem item)
        {
            double dValue = 0.0;

            dValue = Helper.StringToWeight(item.TareRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (dValue != 0.0)
                this.labelIconNET1.Visible = true;
            else
                this.labelIconNET1.Visible = false;
        }
        private void UpdateDisplayIconNET2(ProductItem item)
        {
            double dValue = 0.0;

            dValue = Helper.StringToWeight(item.TareRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (dValue != 0.0)
                this.labelIconNET2.Visible = true;
            else
                this.labelIconNET2.Visible = false;
        }
        private void UpdateDisplayStopWeight1(DataStore.EquipmentStatus status, WeightData data)
        {
            string value = "";

            // 영점 아이콘표시
            if (data.Status == DataStore.WeightStatus.WeightZero)
                this.pictureBoxIconZero1.Visible = true;
            else
                this.pictureBoxIconZero1.Visible = false;

            if (status == DataStore.EquipmentStatus.Stop)
            {
                // 중량
                value = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                if (this.labelWeight1.Text != value)
                    this.labelWeight1.Text = value;
            }
        }
        private void UpdateDisplayStopWeight2(DataStore.EquipmentStatus status, WeightData data)
        {
            string value = "";

            // 영점 아이콘표시
            if (data.Status == DataStore.WeightStatus.WeightZero)
                this.pictureBoxIconZero2.Visible = true;
            else
                this.pictureBoxIconZero2.Visible = false;

            if (status == DataStore.EquipmentStatus.Stop)
            {
                // 중량
                value = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                if (this.labelWeight2.Text != value)
                    this.labelWeight2.Text = value;
            }
        }
        private void UpdateDisplayUser(UserItem user)
        {
            switch (user.Group)
            {
                case DataStore.UserGroup.LogOff:
                    break;
                case DataStore.UserGroup.Level1Operator:
                    break;
                case DataStore.UserGroup.Level2Engineer:
                    break;
                case DataStore.UserGroup.Level3Manager:
                    break;
                case DataStore.UserGroup.Level4Developer:
                    break;
                default:
                    break;
            }
        }

        public void UpdateEquipmentStatusDisplay(DataStore.EquipmentStatus status)
        {
            if (status == DataStore.EquipmentStatus.Start)
            {
                this.labelWeight1.Text = Helper.StringToDecimalPlaces("0", this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                this.labelWeight2.Text = Helper.StringToDecimalPlaces("0", this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            }
            else
            {
                this.pictureBoxIconGap1.Visible = false;
                this.pictureBoxIconGap2.Visible = false;
                this.pictureBoxIconMetal1.Visible = false;
                this.pictureBoxIconMetal2.Visible = false;

                this.buttonUnder1.ButtonUp();
                this.buttonUnder2.ButtonUp();
                this.buttonPass1.ButtonUp();
                this.buttonPass2.ButtonUp();
                this.buttonOver1.ButtonUp();
                this.buttonOver2.ButtonUp();
            }
        }
        public void UpdateCurrentProductDisplay(DataStore.EquipmentStatus status, ProductItem pItem1, ProductItem pItem2, WeightData wData1, WeightData wData2)
        {
            this.SetProduct1(pItem1);
            this.SetProduct2(pItem2);
            this.SetCount1(wData1);
            this.SetCount2(wData2);
        }
        public void UpdateStopWeightDisplay(DataStore.EquipmentStatus status, WeightData data1, WeightData data2)
        {
            this.UpdateDisplayStopWeight1(status, data1);
            this.UpdateDisplayStopWeight2(status, data2);
        }
        public void UpdateStartWeightDisplay1(DataStore.EquipmentStatus status, WeightData data)
        {
            string value = "";

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

            // 중량
            value = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelWeight1.Text != value)
                this.labelWeight1.Text = value;

            // GAP 아이콘 표시
            if (data.JudgmentStatus == DataStore.JudgmentStatus.Double)
            {
                if (this.pictureBoxIconGap1.Visible != true)
                    this.pictureBoxIconGap1.Visible = true;
            }
            else
                this.pictureBoxIconGap1.Visible = false;

            // Metal 아이콘 표시
            if (data.JudgmentStatus == DataStore.JudgmentStatus.Metal)
            {
                if (this.pictureBoxIconMetal1.Visible != true)
                    this.pictureBoxIconMetal1.Visible = true;
            }
            else
                this.pictureBoxIconMetal1.Visible = false;

            this.UpdateDisplayJudgmentIcon1(data.JudgmentStatus);
            this.SetCount1(data);
        }
        public void UpdateStartWeightDisplay2(DataStore.EquipmentStatus status, WeightData data)
        {
            string value = "";

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

            // 중량
            value = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelWeight2.Text != value)
                this.labelWeight2.Text = value;

            // GAP 아이콘 표시
            if (data.JudgmentStatus == DataStore.JudgmentStatus.Double)
            {
                if (this.pictureBoxIconGap2.Visible != true)
                    this.pictureBoxIconGap2.Visible = true;
            }
            else
                this.pictureBoxIconGap2.Visible = false;

            // Metal 아이콘 표시
            if (data.JudgmentStatus == DataStore.JudgmentStatus.Metal)
            {
                if (this.pictureBoxIconMetal2.Visible != true)
                    this.pictureBoxIconMetal2.Visible = true;
            }
            else
                this.pictureBoxIconMetal2.Visible = false;

            this.UpdateDisplayJudgmentIcon2(data.JudgmentStatus);
            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
    }
}
