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.Forms;
using ITC81DB_ImageDll;

namespace ITC81DB.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.SystemConfig1.Language == DataStore.LanguageID.English)
            {
                this.labelTitleWeight.Font = new Font("새굴림", 15, FontStyle.Bold);
                this.labelTitleWeight.Text = "Weight value";
                this.labelTitleCount.Text = "Count";
                this.labelTitleSumWeight.Text = "Sum(kg)";
                this.labelTitleAverage.Text = "Average";
                this.labelTitleNG.Text = "Faulty";
                this.labelTitleExNG.Text = "Other Faulty";
                this.labelTitleTotalCount.Text = "Total";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
            {
                this.labelTitleWeight.Font = new Font("새굴림", 15, FontStyle.Bold);
                this.labelTitleWeight.Text = "设定值";
                this.labelTitleCount.Text = "当前";
                this.labelTitleSumWeight.Text = "总和(kg)";
                this.labelTitleAverage.Text = "平均";
                this.labelTitleNG.Text = "剔除";
                this.labelTitleExNG.Text = "其他不良";
                this.labelTitleTotalCount.Text = "总共计数";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
            {
                this.labelTitleWeight.Font = new Font("새굴림", 9, FontStyle.Bold);
                this.labelTitleWeight.Text = "Настройка значений";
                this.labelTitleCount.Text = "Итог";
                this.labelTitleSumWeight.Text = "Сумма(kg)";
                this.labelTitleAverage.Text = "Средний";
                this.labelTitleNG.Text = "Дефект";
                this.labelTitleExNG.Text = "Другие дефект";
                this.labelTitleTotalCount.Text = "Итого";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
                this.labelTitleWeight.Font = new Font("새굴림", 11, FontStyle.Bold);
                this.labelTitleWeight.Text = "Einstellungswert";
                this.labelTitleCount.Text = "Zählen";
                this.labelTitleSumWeight.Text = "Summe(kg)";
                this.labelTitleAverage.Text = "Durchschnitt";
                this.labelTitleNG.Text = "Schlecht";
                this.labelTitleExNG.Text = "Andere Schlecht";
                this.labelTitleTotalCount.Text = "Insgesamt";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Spanish)
            {
                this.labelTitleWeight.Font = new Font("새굴림", 15, FontStyle.Bold);
                this.labelTitleWeight.Text = "Setear Valor";
                this.labelTitleCount.Text = "Conteo";
                this.labelTitleSumWeight.Text = "Suma(kg)";
                this.labelTitleAverage.Text = "Promedio";
                this.labelTitleNG.Text = "Rechazo";
                this.labelTitleExNG.Text = "Otro rechazo";
                this.labelTitleTotalCount.Text = "Conteo Total";
            }
            else
            {
                this.labelTitleWeight.Font = new Font("새굴림", 15, FontStyle.Bold);
                this.labelTitleWeight.Text = "설정값";
                this.labelTitleCount.Text = "수량";
                this.labelTitleSumWeight.Text = "누적(kg)";
                this.labelTitleAverage.Text = "평균";
                this.labelTitleNG.Text = "불량";
                this.labelTitleExNG.Text = "기타불량";
                this.labelTitleTotalCount.Text = "전체수량";
            }

            this.LabelUnit();
        }
        private void DefaultSetting()
        {
        }

        public void LabelUnit()
        {
            string[] data = this.labelTitleSumWeight.Text.Split('(');
            if (this.ParentForm.ParentForm.SystemConfig1.Unit == "oz")
                this.labelTitleSumWeight.Text = data[0] + "(oz)";
            else
                this.labelTitleSumWeight.Text = data[0] + "(kg)";
        }
        private void SetProduct(ProductItem item)
        {
            string underValue = "", passValue = "", overValue = "";

            switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
            {
                case DataStore.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 DataStore.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;

            value = data.ExNGCount.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
            if (this.ParentForm.ParentForm.SystemConfig1.Unit != "oz")
            {
                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;
            }
            else
            {
                value = data.UnderSumWeight.ToString();
                if (this.labelUnderSumWeight.Text != value)
                    this.labelUnderSumWeight.Text = value;

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

                value = data.OverSumWeight.ToString();
                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 DataStore.UserGroup.LogOut:
                    break;
                case DataStore.UserGroup.Level1Operator:
                    break;
                case DataStore.UserGroup.Level2Engineer:
                    break;
                case DataStore.UserGroup.Level3Manager:
                    break;
                case DataStore.UserGroup.Level4Developer:
                    break;
                case DataStore.UserGroup.NotLogin:
                    break;
                default:
                    break;
            }
        }

        public void UpdateEquipmentStatusDisplay(DataStore.EquipmentStatus status)
        {

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

        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);
        }
        #endregion
    }
}
