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_2H.Forms;
using ITC81DB_2H_ImageDll;
using ITC81DB_2H.Datastore;
using ITC81DB_2H.Part11_UserManager;

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

        #region Constructor
        public ControlMainDisplayStopDataStatistics(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.SystemConfig1.Language == Define.E_LanguageID.English)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engMainStatScreen));

                this.labelTitleNgCount.Text = "Faulty";
                this.labelTitleExNgCount.Text = "Other faulty";
                this.labelTitleTotalCount.Text = "Total";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainStatScreen));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainStatScreen));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainStatScreen));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
            {

            }
            else
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korMainStatScreen));

                this.labelTitleNgCount.Text = "불량";
                this.labelTitleExNgCount.Text = "기타불량";
                this.labelTitleTotalCount.Text = "총계";
            }
        }
        private void DefaultSetting()
        {
        }

        private void SetProduct(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.SystemConfig1.DecimalPlaces);
                    passValue = Helper.StringToDecimalPlaces(item.PassRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    overValue = Helper.StringToDecimalPlaces(item.OverRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    break;
                case Define.E_WeightInputMode.Deviation:
                    underValue = Helper.StringToDecimalPlaces(item.UnderRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    passValue = Helper.StringToDecimalPlaces(item.PassRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    overValue = Helper.StringToDecimalPlaces(item.OverRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    overValue = string.Format("+{0}", overValue);
                    break;
                default:
                    break;
            }

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

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

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

            #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;

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

            if (this.ParentForm.IsCurrentValueDouble == false)
                value = data.ExNGCount.ToString();
            else
                value = data.DoubleEntryCount.ToString();
            if (this.labelExNgCount.Text != value)
                this.labelExNgCount.Text = value;

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

            #region SumWeight
            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 Average
            value = Helper.DoubleToString(data.UnderAverage, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            if (this.labelUnderAverage.Text != value)
                this.labelUnderAverage.Text = value;

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

            value = Helper.DoubleToString(data.OverAverage, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            if (this.labelOverAverage.Text != value)
                this.labelOverAverage.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.Admin:
                    break;
                default:
                    break;
            }
        }

        public void UpdateEquipmentStatusDisplay(Define.E_EquipmentStatus status)
        {

        }
        public void UpdateCurrentProductDisplay(Define.E_EquipmentStatus status, ProductItem pItem, WeightData wData)
        {
            this.SetProduct(pItem);
            this.SetCount(wData);
        }
        public void UpdateStartWeightDisplay(Define.E_EquipmentStatus status, WeightData data)
        {
            this.SetCount(data);
        }

        private void DisplayExNG(bool bValue)
        {
            if (bValue == false)
            {
                if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.English)
                    this.labelTitleExNgCount.Text = "Other faulty";
                else
                    this.labelTitleExNgCount.Text = "기타불량";

                this.labelExNgCount.Text = this.ParentForm.ParentForm.CurrentWeightData.ExNGCount.ToString();
            }
            else
            {
                if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.English)
                    this.labelTitleExNgCount.Text = "Double";
                else
                    this.labelTitleExNgCount.Text = "이중진입";

                this.labelExNgCount.Text = this.ParentForm.ParentForm.CurrentWeightData.DoubleEntryCount.ToString();
            }
        }
        public void Clear()
        {
            this.SetCount(this.ParentForm.ParentForm.CurrentWeightData);
        }
        public void DisplayRefresh(SystemStatus status)
        {
            this.UpdateDisplayUser(status.CurrentUser);
            this.SetProduct(this.ParentForm.ParentForm.CurrentProductItem);
            this.SetCount(this.ParentForm.ParentForm.CurrentWeightData);
            this.DisplayExNG(this.ParentForm.IsCurrentValueDouble);
        }
        #endregion

        #region Event Handler
        private void labelTitleExNGCount_Click(object sender, EventArgs e)
        {
            // Automatic Logout Reset
            if (this.ParentForm.ParentForm.SystemConfig3.IsPart11 == true)
                this.ParentForm.Part11AutomaticLogoutReset();

            if (this.ParentForm.IsCurrentValueDouble == false)
                this.ParentForm.IsCurrentValueDouble = true;
            else
                this.ParentForm.IsCurrentValueDouble = false;

            this.DisplayExNG(this.ParentForm.IsCurrentValueDouble);       
        }
        #endregion
    }
}
