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

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

        private int m_Count;
        private int m_SumWeight;
        private int m_MinWeight;
        private int m_MaxWeight;
        #endregion

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

            this.ParentForm = parent;

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

        #region Property
        public FormMainDisplay ParentForm
        {
            get { return this.m_ParentForm; }
            set { this.m_ParentForm = value; }
        }

        public int Count
        {
            get { return this.m_Count; }
            set { this.m_Count = value; }
        }
        public int SumWeight
        {
            get { return this.m_SumWeight; }
            private set { this.m_SumWeight = value; }
        }
        public int MaxWeight
        {
            get { return this.m_MaxWeight; }
            set { this.m_MaxWeight = value; }
        }
        public int MinWeight
        {
            get { return this.m_MinWeight; }
            set { this.m_MinWeight = 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.engMainSpotCheckScreen));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainSpotCheckScreen));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainSpotCheckScreen));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainSpotCheckScreen));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Spanish)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engMainSpotCheckScreen));
            }
            else
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korMainSpotCheckScreen));
            }
        }
        private void DefaultSetting()
        {
            this.Clear();
            this.UpdateDisplay();
        }

        public void Clear()
        {
            this.Count = 0;
            this.MaxWeight = 0;
            this.MinWeight = 99999;
            this.SumWeight = 0;

            this.listBoxWeightList.ClearAll();
        }
        private void SetValue(WeightData wData)
        {
            string value = "";
            string weightString = "";
            int weightInt = int.Parse(wData.WeightString);

            this.Count++;
            this.SumWeight += weightInt;

            if (weightInt < this.MinWeight)
                this.MinWeight = weightInt;
            if (weightInt > this.MaxWeight)
                this.MaxWeight = weightInt;

            if (this.listBoxWeightList.Items.Count >= 100)
                this.listBoxWeightList.RemoveItem(0);

            weightString = Helper.StringToDecimalPlaces(wData.WeightString, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            value = string.Format("{0,15}{1,20}", this.Count, weightString);

            this.listBoxWeightList.AddItem(value);
            this.listBoxWeightList.ScrollDown();
        }
        public string UpdateAverageWeight()
        {
            int value = 0;

            if (this.Count == 0)
                return value.ToString();

            try
            {
                value = this.SumWeight / this.Count;
                return value.ToString();
            }
            catch
            {
                return value.ToString();
            }
        }

        public void UpdateDisplay()
        {
            if (this.Count == 0)
            {
                this.labelSpotCheckMax.Enabled = false;
                this.labelSpotCheckMin.Enabled = false;
                this.labelSpotCheckAverage.Enabled = false;
            }
            else
            {
                this.labelSpotCheckMax.Enabled = true;
                this.labelSpotCheckMin.Enabled = true;
                this.labelSpotCheckAverage.Enabled = true;
            }

            this.labelTotalCount.Text = this.Count.ToString();
            this.labelSpotCheckMax.Text = Helper.StringToDecimalPlaces(this.MaxWeight.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            this.labelSpotCheckMin.Text = Helper.StringToDecimalPlaces(this.MinWeight.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            this.labelSpotCheckAverage.Text = Helper.StringToDecimalPlaces(this.UpdateAverageWeight(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
        }
        public void UpdateCurrentProductDisplay(DataStore.EquipmentStatus status, ProductItem pItem, WeightData wData)
        {
        }
        public void UpdateStartWeightDisplay(DataStore.EquipmentStatus status, WeightData wData)
        {
            if (this.buttonSpotCheckStart.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
            {
                this.SetValue(wData);
                this.UpdateDisplay();
            }
        }
        public bool CurrentStatusButtonSpotCheckRun()
        {
            bool bValue = false;

            if (this.buttonSpotCheckStart.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                bValue = true;
            else
                bValue = false;

            return bValue;
        }
        public void ChangeEnableButtonSpotCheckRun(bool bValue)
        {
            if (bValue == true)
                this.buttonSpotCheckStart.Enabled = true;
            else
                this.buttonSpotCheckStart.Enabled = false;
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.UpdateDisplay();
        }
        #endregion

        #region Event Handler
        private void buttonSpotCheckStart_Click(object sender, EventArgs e)
        {
            if (this.buttonSpotCheckStart.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
            {
                if (this.ParentForm.MainSubMenu.CurrentStatusButtonBypassRun() == false)
                {
                    this.Clear();
                    this.UpdateDisplay();
                    this.ParentForm.ParentForm.ChildFormMainDisplay.MainWeightBigScreen.ChangeToSpotCheckImage();
                    this.ParentForm.ParentForm.ChildFormMainDisplay.MainWeightSmallScreen.ChangeToSpotCheckImage();
                    this.ParentForm.ParentForm.ChildFormMainDisplay.MainWeightBigScreen.UpdateSpotOrBypassImageVisibleStatus(true);
                    this.ParentForm.ParentForm.ChildFormMainDisplay.MainWeightSmallScreen.UpdateSpotOrBypassImageVisibleStatus(true);
                    this.ParentForm.ParentForm.TransferData(CommunicationCommand.ByNGON, CommunicationID.MainBoard);
                    this.ParentForm.MainSubMenu.ChangeEnableButtonBypassRun(false);
                }
                else
                {
                    this.buttonSpotCheckStart.ButtonUp();
                }
            }
            else
            {
                this.ParentForm.ParentForm.ChildFormMainDisplay.MainWeightBigScreen.UpdateSpotOrBypassImageVisibleStatus(false);
                this.ParentForm.ParentForm.ChildFormMainDisplay.MainWeightSmallScreen.UpdateSpotOrBypassImageVisibleStatus(false);
                this.ParentForm.ParentForm.TransferData(CommunicationCommand.ByNGOFF, CommunicationID.MainBoard);
                this.ParentForm.MainSubMenu.ChangeEnableButtonBypassRun(true);
            }
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            this.Clear();
            this.UpdateDisplay();
        }

        private void buttonListUp_Click(object sender, EventArgs e)
        {
            if (this.listBoxWeightList.ViewStartItemIndex / 5 > 0)
                this.listBoxWeightList.ScrollUp(100);
            else
                this.listBoxWeightList.ScrollUp();

            this.listBoxWeightList.SelectItemIndex = -1;
        }
        private void buttonListDown_Click(object sender, EventArgs e)
        {
            if (this.listBoxWeightList.ViewStartItemIndex / 5 > 0)
                this.listBoxWeightList.ScrollDown(100);
            else
                this.listBoxWeightList.ScrollDown();

            this.listBoxWeightList.SelectItemIndex = -1;
        }
        private void buttonListRapidDown_Click(object sender, EventArgs e)
        {
            this.listBoxWeightList.ScrollDown(this.listBoxWeightList.ViewRemainCount / 5 * 100);

            for (int i = 0; i < this.listBoxWeightList.ViewRemainCount % 5; i++)
                this.listBoxWeightList.ScrollDown();

            this.listBoxWeightList.SelectItemIndex = -1;
        }

        private void labelSpotCheckMin_Click(object sender, EventArgs e)
        {
            DialogFormYesNo myDlg;

            myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig1.Language, 27); // 해당 값을 기준값으로 설정하시겠습니까?

            if (myDlg.ShowDialog() == DialogResult.Yes)
            {
                if (this.ParentForm.ParentForm.SystemConfig1.ProductNumber >= 996
              && this.ParentForm.ParentForm.SystemConfig1.ProductNumber <= 1000)
                {
                    DialogFormMessage myMsg = new DialogFormMessage(27, this.ParentForm.ParentForm.SystemConfig1.Language);
                    myMsg.ShowDialog();
                    return;
                }

                string value = "", underRange = "", overRange = "", viewValue = "";
                int digit = 0, temp = 0;
                int oldUnderRangeDeviation = this.ParentForm.ParentForm.CurrentProductItem.UnderRangeDeviation;
                int oldOverRangeDeviation = this.ParentForm.ParentForm.CurrentProductItem.OverRangeDeviation;

                if (this.ParentForm.ParentForm.CurrentWeightData.Weight < 1)
                    viewValue = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.PassRange.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                else
                    viewValue = Helper.DoubleToString(this.ParentForm.ParentForm.CurrentWeightData.Weight, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);

                this.ParentForm.ParentForm.CurrentProductItem.PassRange = this.labelSpotCheckMin.Text.Replace(".", "");

                #region UnderRange
                temp = this.ParentForm.ParentForm.CurrentProductItem.PassRangeInt + oldUnderRangeDeviation;
                if (temp < 0)
                    value = "0";
                else
                    value = temp.ToString();

                this.ParentForm.ParentForm.CurrentProductItem.UnderRange = value;
                #endregion

                #region OverRange
                // V6.2.0
                //if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 2)
                //    range = 1000000;
                //else if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 1)
                //    range = 100000;
                //else
                //    range = 10000;
                temp = this.ParentForm.ParentForm.CurrentProductItem.PassRangeInt + oldOverRangeDeviation;
                if (temp > 99999)
                    value = "99999";
                else
                    value = temp.ToString();

                this.ParentForm.ParentForm.CurrentProductItem.OverRange = value;
                #endregion

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

                switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
                {
                    case DataStore.WeightInputMode.Weight:
                        #region WeightMode
                        underRange = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.UnderRange.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                        overRange = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.OverRange.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                        #endregion
                        break;
                    case DataStore.WeightInputMode.Deviation:
                        #region DeviationMode
                        if (this.ParentForm.ParentForm.CurrentProductItem.UnderRangeDeviation == 0)
                            underRange = string.Format("-{0}", Helper.DoubleToString(0, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces));
                        else
                            underRange = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.UnderRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                        overRange = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.OverRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                        overRange = string.Format("+{0}", overRange);
                        #endregion
                        break;
                    default:
                        break;
                }

                if (this.ParentForm.ParentForm.ChildFormMainDisplay.MainFeedback.IsUsingFeedback() == true)
                    this.ParentForm.ParentForm.UpdateFeedbackData(this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.CurrentSystemParameter2);
            }
        }
        private void labelSpotCheckMax_Click(object sender, EventArgs e)
        {
            DialogFormYesNo myDlg;

            myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig1.Language, 27); // 해당 값을 기준값으로 설정하시겠습니까?

            if (myDlg.ShowDialog() == DialogResult.Yes)
            {
                if (this.ParentForm.ParentForm.SystemConfig1.ProductNumber >= 996
              && this.ParentForm.ParentForm.SystemConfig1.ProductNumber <= 1000)
                {
                    DialogFormMessage myMsg = new DialogFormMessage(27, this.ParentForm.ParentForm.SystemConfig1.Language);
                    myMsg.ShowDialog();
                    return;
                }

                string value = "", underRange = "", overRange = "", viewValue = "";
                int digit = 0, temp = 0;
                int oldUnderRangeDeviation = this.ParentForm.ParentForm.CurrentProductItem.UnderRangeDeviation;
                int oldOverRangeDeviation = this.ParentForm.ParentForm.CurrentProductItem.OverRangeDeviation;

                if (this.ParentForm.ParentForm.CurrentWeightData.Weight < 1)
                    viewValue = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.PassRange.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                else
                    viewValue = Helper.DoubleToString(this.ParentForm.ParentForm.CurrentWeightData.Weight, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);

                this.ParentForm.ParentForm.CurrentProductItem.PassRange = this.labelSpotCheckMax.Text.Replace(".", "");

                #region UnderRange
                temp = this.ParentForm.ParentForm.CurrentProductItem.PassRangeInt + oldUnderRangeDeviation;
                if (temp < 0)
                    value = "0";
                else
                    value = temp.ToString();

                this.ParentForm.ParentForm.CurrentProductItem.UnderRange = value;
                #endregion

                #region OverRange
                // V6.2.0
                //if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 2)
                //    range = 1000000;
                //else if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 1)
                //    range = 100000;
                //else
                //    range = 10000;
                temp = this.ParentForm.ParentForm.CurrentProductItem.PassRangeInt + oldOverRangeDeviation;
                if (temp > 99999)
                    value = "99999";
                else
                    value = temp.ToString();

                this.ParentForm.ParentForm.CurrentProductItem.OverRange = value;
                #endregion

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

                switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
                {
                    case DataStore.WeightInputMode.Weight:
                        #region WeightMode
                        underRange = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.UnderRange.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                        overRange = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.OverRange.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                        #endregion
                        break;
                    case DataStore.WeightInputMode.Deviation:
                        #region DeviationMode
                        if (this.ParentForm.ParentForm.CurrentProductItem.UnderRangeDeviation == 0)
                            underRange = string.Format("-{0}", Helper.DoubleToString(0, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces));
                        else
                            underRange = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.UnderRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                        overRange = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.OverRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                        overRange = string.Format("+{0}", overRange);
                        #endregion
                        break;
                    default:
                        break;
                }

                if (this.ParentForm.ParentForm.ChildFormMainDisplay.MainFeedback.IsUsingFeedback() == true)
                    this.ParentForm.ParentForm.UpdateFeedbackData(this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.CurrentSystemParameter2);
            }
        }
        private void labelSpotCheckAverage_Click(object sender, EventArgs e)
        {
            DialogFormYesNo myDlg;

            myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig1.Language, 27); // 해당 값을 기준값으로 설정하시겠습니까?

            if (myDlg.ShowDialog() == DialogResult.Yes)
            {
                if (this.ParentForm.ParentForm.SystemConfig1.ProductNumber >= 996
              && this.ParentForm.ParentForm.SystemConfig1.ProductNumber <= 1000)
                {
                    DialogFormMessage myMsg = new DialogFormMessage(27, this.ParentForm.ParentForm.SystemConfig1.Language);
                    myMsg.ShowDialog();
                    return;
                }

                string value = "", underRange = "", overRange = "", viewValue = "";
                int digit = 0, temp = 0;
                int oldUnderRangeDeviation = this.ParentForm.ParentForm.CurrentProductItem.UnderRangeDeviation;
                int oldOverRangeDeviation = this.ParentForm.ParentForm.CurrentProductItem.OverRangeDeviation;

                if (this.ParentForm.ParentForm.CurrentWeightData.Weight < 1)
                    viewValue = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.PassRange.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                else
                    viewValue = Helper.DoubleToString(this.ParentForm.ParentForm.CurrentWeightData.Weight, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);

                this.ParentForm.ParentForm.CurrentProductItem.PassRange = this.labelSpotCheckAverage.Text.Replace(".", "");

                #region UnderRange
                temp = this.ParentForm.ParentForm.CurrentProductItem.PassRangeInt + oldUnderRangeDeviation;
                if (temp < 0)
                    value = "0";
                else
                    value = temp.ToString();

                this.ParentForm.ParentForm.CurrentProductItem.UnderRange = value;
                #endregion

                #region OverRange
                // V6.2.0
                //if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 2)
                //    range = 1000000;
                //else if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 1)
                //    range = 100000;
                //else
                //    range = 10000;
                temp = this.ParentForm.ParentForm.CurrentProductItem.PassRangeInt + oldOverRangeDeviation;
                if (temp > 99999)
                    value = "99999";
                else
                    value = temp.ToString();

                this.ParentForm.ParentForm.CurrentProductItem.OverRange = value;
                #endregion

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

                switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
                {
                    case DataStore.WeightInputMode.Weight:
                        #region WeightMode
                        underRange = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.UnderRange.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                        overRange = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.OverRange.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                        #endregion
                        break;
                    case DataStore.WeightInputMode.Deviation:
                        #region DeviationMode
                        if (this.ParentForm.ParentForm.CurrentProductItem.UnderRangeDeviation == 0)
                            underRange = string.Format("-{0}", Helper.DoubleToString(0, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces));
                        else
                            underRange = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.UnderRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                        overRange = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.OverRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                        overRange = string.Format("+{0}", overRange);
                        #endregion
                        break;
                    default:
                        break;
                }

                if (this.ParentForm.ParentForm.ChildFormMainDisplay.MainFeedback.IsUsingFeedback() == true)
                    this.ParentForm.ParentForm.UpdateFeedbackData(this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.CurrentSystemParameter2);
            }
        }
        #endregion
    }
}