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;
using ITC81DB.DialogForms;

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

        #region Constructor
        public ControlMainAverageTracking(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.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engMainAverageTrackingScreen));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainAverageTrackingScreen));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainAverageTrackingScreen));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainAverageTrackingScreen));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Spanish)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engMainAverageTrackingScreen));
            }
            else
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korMainAverageTrackingScreen));
            }
        }
        private void DefaultSetting()
        {
           
        }

        public void Clear()
        {
            this.ParentForm.ParentForm.CurrentAverageTrackingItem.DataClear();
            this.SetCount(this.ParentForm.ParentForm.CurrentWeightData);
            this.UpdateParameter(this.ParentForm.ParentForm.CurrentAverageTrackingItem, this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig1);        
        }
        private void SetCount(WeightData data)
        {
            string value = "";

            value = data.UnderCount.ToString();
            if (this.labelAverageTrackingUnderCount.Text != value)
                this.labelAverageTrackingUnderCount.Text = value;

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

            value = data.OverCount.ToString();
            if (this.labelAverageTrackingOverCount.Text != value)
                this.labelAverageTrackingOverCount.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;
        }

        // 기준치 갱신
        public void UpdateDisplayAverageTracking(WeightData data)
        {
            string value = "";
            int overRange = 0, underRange = 0, passRange = 0, referenceRange = 0;

            if (this.ParentForm.ParentForm.SystemConfig2.AverageTrackingCount == 0)
                return;

            this.ParentForm.ParentForm.CurrentAverageTrackingItem.SetValue(int.Parse(data.WeightString), this.ParentForm.ParentForm.SystemConfig2.AverageTrackingCount);

            // 리미트 설정과 비교 코드 추가
            passRange = this.ParentForm.ParentForm.CurrentAverageTrackingItem.GetAverageWeight;
            overRange = passRange + this.ParentForm.ParentForm.CurrentProductItem.OverRangeDeviation;
            underRange = passRange + this.ParentForm.ParentForm.CurrentProductItem.UnderRangeDeviation;

            if (this.ParentForm.ParentForm.CurrentAverageTrackingItem.GetQueueCount != this.ParentForm.ParentForm.SystemConfig2.AverageTrackingCount)
            {
                this.ParentForm.ParentForm.ChildFormMainDisplay.MainAverageTracking.UpdateParameter(this.ParentForm.ParentForm.CurrentAverageTrackingItem, this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig1);
                return;
            }

            // 기준치 리미트 범위 체크
            if (overRange > int.Parse(this.ParentForm.ParentForm.CurrentAverageTrackingItem.LimitOver))
            {
                overRange = int.Parse(this.ParentForm.ParentForm.CurrentAverageTrackingItem.LimitOver);
                referenceRange = int.Parse(this.ParentForm.ParentForm.CurrentAverageTrackingItem.LimitOver)
                    - this.ParentForm.ParentForm.CurrentProductItem.OverRangeDeviation;
                underRange = referenceRange + this.ParentForm.ParentForm.CurrentProductItem.UnderRangeDeviation;
            }
            else if (underRange < int.Parse(this.ParentForm.ParentForm.CurrentAverageTrackingItem.LimitUnder))
            {
                underRange = int.Parse(this.ParentForm.ParentForm.CurrentAverageTrackingItem.LimitUnder);
                referenceRange = int.Parse(this.ParentForm.ParentForm.CurrentAverageTrackingItem.LimitUnder)
                    + Math.Abs(this.ParentForm.ParentForm.CurrentProductItem.UnderRangeDeviation);
                overRange = referenceRange + this.ParentForm.ParentForm.CurrentProductItem.OverRangeDeviation;
            }
            else
            {
                referenceRange = this.ParentForm.ParentForm.CurrentAverageTrackingItem.GetAverageWeight;
            }

            // 갱신 기준치 전송
            if (this.ParentForm.ParentForm.CurrentProductItem.PassRange != referenceRange.ToString())
            {
                this.ParentForm.ParentForm.CurrentProductItem.OverRange = overRange.ToString();
                this.ParentForm.ParentForm.CurrentProductItem.PassRange = referenceRange.ToString();
                this.ParentForm.ParentForm.CurrentProductItem.UnderRange = underRange.ToString();

                this.ParentForm.RescaleControl(this.ParentForm.ParentForm.CurrentProductItem);
                this.ParentForm.ParentForm.SaveProductFile(this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);
                this.ParentForm.ParentForm.TransferCurrentProductItem(this.ParentForm.ParentForm.CurrentProductItem);
            }
            this.ParentForm.ParentForm.ChildFormMainDisplay.MainAverageTracking.UpdateParameter(this.ParentForm.ParentForm.CurrentAverageTrackingItem, this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig1);
        }

        public void UpdateCurrentProductDisplay(DataStore.EquipmentStatus status, ProductItem pItem, WeightData wData)
        {
            this.SetCount(wData);
            this.UpdateParameter(this.ParentForm.ParentForm.CurrentAverageTrackingItem, this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig1);
        }
        public void UpdateStartWeightDisplay(DataStore.EquipmentStatus status, WeightData wData)
        {
            this.SetCount(wData);
        }
        public void UpdateParameter(AverageTrackingItem aItem, ProductItem pItem, SystemConfigurationItem1 sItem)
        {
            string sValue = "";

            sValue = Helper.StringToDecimalPlaces(pItem.OverRange, sItem.DecimalPlaces);
            if (this.labelAverageTrackingOver.Text != sValue)
                this.labelAverageTrackingOver.Text = sValue;

            sValue = Helper.StringToDecimalPlaces(pItem.PassRange, sItem.DecimalPlaces);
            if (this.labelAverageTrackingPass.Text != sValue)
                this.labelAverageTrackingPass.Text = sValue;

            sValue = Helper.StringToDecimalPlaces(pItem.UnderRange, sItem.DecimalPlaces);
            if (this.labelAverageTrackingUnder.Text != sValue)
                this.labelAverageTrackingUnder.Text = sValue;

            sValue = Helper.StringToDecimalPlaces(aItem.LimitOver, sItem.DecimalPlaces);
            if (this.labelAverageTrackingOverLimit.Text != sValue)
                this.labelAverageTrackingOverLimit.Text = sValue;

            sValue = Helper.StringToDecimalPlaces(aItem.LimitUnder, sItem.DecimalPlaces);
            if (this.labelAverageTrackingUnderLimit.Text != sValue)
                this.labelAverageTrackingUnderLimit.Text = sValue;

            sValue = this.ParentForm.ParentForm.SystemConfig2.AverageTrackingCount.ToString();
            if (this.buttonAverageTrackingCount.Text != sValue)
                this.buttonAverageTrackingCount.Text = sValue;

            sValue = Helper.StringToDecimalPlaces(aItem.GetAverageWeight.ToString(), sItem.DecimalPlaces);
            if (this.labelCurrentAverage.Text != sValue)
                this.labelCurrentAverage.Text = sValue;

            sValue = aItem.GetQueueCount.ToString();
            if (this.labelCurrentCount.Text != sValue)
                this.labelCurrentCount.Text = sValue;
        }

        public void DisplayRefresh(SystemStatus status)
        {
            string sValue = "";

            this.SetCount(this.ParentForm.ParentForm.CurrentWeightData);
            this.UpdateParameter(this.ParentForm.ParentForm.CurrentAverageTrackingItem, this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig1);

            sValue = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.PassRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            if (this.labelAverageTrackingPassLimit.Text != sValue)
                this.labelAverageTrackingPassLimit.Text = sValue;
        }
        #endregion

        #region Event Handler
        private void labelAverageTrackingOverLimit_Click(object sender, EventArgs e)
        {
            string value = "";
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelAverageTrackingOverLimit.Text, 5, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces, false, this.ParentForm.ParentForm.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem.OverRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces))
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelAverageTrackingOverLimit.Text = Helper.StringToDecimalPlaces(myKeyPad.StringValue, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    this.ParentForm.ParentForm.CurrentAverageTrackingItem.LimitOver = myKeyPad.StringValue.Replace(".", "");

                    this.UpdateParameter(this.ParentForm.ParentForm.CurrentAverageTrackingItem, this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig1);
                    this.ParentForm.ParentForm.SaveAverageTrackingFile(this.ParentForm.ParentForm.CurrentAverageTrackingItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);
                }
            }
        }
        private void labelAverageTrackingUnderLimit_Click(object sender, EventArgs e)
        {
            string value = "";
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelAverageTrackingUnderLimit.Text, 5, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces, false, this.ParentForm.ParentForm.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue > Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem.UnderRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces))
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelAverageTrackingUnderLimit.Text = Helper.StringToDecimalPlaces(myKeyPad.StringValue, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    this.ParentForm.ParentForm.CurrentAverageTrackingItem.LimitUnder = myKeyPad.StringValue.Replace(".", "");

                    this.UpdateParameter(this.ParentForm.ParentForm.CurrentAverageTrackingItem, this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig1);
                    this.ParentForm.ParentForm.SaveAverageTrackingFile(this.ParentForm.ParentForm.CurrentAverageTrackingItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);
                }
            }
        }

        private void buttonAverageTrackingCount_Click(object sender, EventArgs e)
        {
            string value = "";
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.buttonAverageTrackingCount.Text, 2, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

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

                    this.ParentForm.ParentForm.CurrentAverageTrackingItem.DataClear();
                    this.ParentForm.ParentForm.SystemConfig2.AverageTrackingCount = myKeyPad.IntValue;
                    this.ParentForm.ParentForm.SaveSystemConfigurationFile2(this.ParentForm.ParentForm.SystemConfig2);
                }
            }
        }
        #endregion        
    }
}
