using System;
using System.Linq;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

using ITC81DB.Forms;
using ITC81DB_ImageDll;
using ITC81DB.DialogForms;

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

        private int DifferenceValue;

        public Collection<ProductItem> CollectionRandomItem;
        private Collection<int> CollectionPassGap;
        private ProductItem ProductItemRandom1;
        private ProductItem ProductItemRandom2;
        private ProductItem ProductItemRandom3;
        private ProductItem ProductItemRandom4;
        private ProductItem ProductItemRandom5;

        private Collection<SmartX.SmartLabel> CollectionRange;
        private Collection<SmartX.SmartLabel> CollectionUnderRange;
        private Collection<SmartX.SmartLabel> CollectionPassRange;
        private Collection<SmartX.SmartLabel> CollectionOverRange;
        private Collection<SmartX.SmartButton> CollectionUsing;
        #endregion

        #region Constructor
        public ControlBasicRandomMode(FormMenu parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

            this.InitializeForm();

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

        #region Property
        public FormMenu 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.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engBackgroundRandomMode));

                for (int i = 0; i < this.CollectionUsing.Count; i++)
                {
                    this.CollectionUsing[i].DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engON_Black));
                    this.CollectionUsing[i].UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engOFF_Black));
                }

                this.labelHelp1.Text = "- The weight setting value of each number should be at least 50g difference(kg : 1kg)";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackgroundRandomMode));

                for (int i = 0; i < this.CollectionUsing.Count; i++)
                {
                    this.CollectionUsing[i].DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnON_Black));
                    this.CollectionUsing[i].UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnOFF_Black));
                }

                this.labelHelp1.Text = "- 每个数字的重量设定值应相差至少50g(kg : 1kg)";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.rusBackgroundRandomMode));

                for (int i = 0; i < this.CollectionUsing.Count; i++)
                {
                    this.CollectionUsing[i].DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusON_Black));
                    this.CollectionUsing[i].UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusOFF_Black));
                }

                this.labelHelp1.Text = "";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerBackgroundRandomMode));

                for (int i = 0; i < this.CollectionUsing.Count; i++)
                {
                    this.CollectionUsing[i].DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerON_Black));
                    this.CollectionUsing[i].UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerOFF_Black));
                }

                this.labelHelp1.Text = "- Der Gewichtseinstellungswert jeder Zahl sollte mindestens 50 g Unterschied betragen(kg : 1kg)";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Spanish)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engBackgroundRandomMode));

                for (int i = 0; i < this.CollectionUsing.Count; i++)
                {
                    this.CollectionUsing[i].DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engON_Black));
                    this.CollectionUsing[i].UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engOFF_Black));
                }

                this.labelHelp1.Text = "- El valor de ajuste de peso de cada número debe ser de al menos 50 g (kg: 1kg)";
            }
            else
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korBackgroundRandomMode));

                for (int i = 0; i < this.CollectionUsing.Count; i++)
                {
                    this.CollectionUsing[i].DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engON_Black));
                    this.CollectionUsing[i].UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engOFF_Black));
                }

                this.labelHelp1.Text = "- 각 품번의 중량 설정값은 최소 50g 이상 차이나야 합니다.(단위 kg 시 1kg)";
            }
        }
        private void DefaultSetting()
        {
            this.ProductItemRandom1 = new ProductItem();
            this.ProductItemRandom2 = new ProductItem();
            this.ProductItemRandom3 = new ProductItem();
            this.ProductItemRandom4 = new ProductItem();
            this.ProductItemRandom5 = new ProductItem();

            this.CollectionPassGap = new Collection<int>();
            this.CollectionRandomItem = new Collection<ProductItem>();
            for (int i = 0; i < 5; i++)
            {
                this.CollectionRandomItem.Add(new ProductItem());
                this.CollectionPassGap.Add(0);
            }

            this.ParentForm.ParentForm.LoadProductFile(ref this.ProductItemRandom1, 995);
            this.ParentForm.ParentForm.LoadProductFile(ref this.ProductItemRandom2, 996);
            this.ParentForm.ParentForm.LoadProductFile(ref this.ProductItemRandom3, 997);
            this.ParentForm.ParentForm.LoadProductFile(ref this.ProductItemRandom4, 998);
            this.ParentForm.ParentForm.LoadProductFile(ref this.ProductItemRandom5, 999);

            this.CollectionRandomItem[0] = this.ProductItemRandom1;
            this.CollectionRandomItem[1] = this.ProductItemRandom2;
            this.CollectionRandomItem[2] = this.ProductItemRandom3;
            this.CollectionRandomItem[3] = this.ProductItemRandom4;
            this.CollectionRandomItem[4] = this.ProductItemRandom5;

            if (this.ParentForm.ParentForm.SystemConfig1.Unit == "g")
            {
                if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 1)
                    this.DifferenceValue = 500;
                else if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 2)
                    this.DifferenceValue = 5000;
                else
                    this.DifferenceValue = 50;
            }
            else
            {
                if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 1)
                    this.DifferenceValue = 10;
                else if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 2)
                    this.DifferenceValue = 100;
                else
                    this.DifferenceValue = 1;
            }
        }
        private void InitializeForm()
        {
            this.CollectionRange = new Collection<SmartX.SmartLabel>();
            this.CollectionRange.Clear();
            this.CollectionRange.Add(this.labelRange1);
            this.CollectionRange.Add(this.labelRange2);
            this.CollectionRange.Add(this.labelRange3);
            this.CollectionRange.Add(this.labelRange4);

            this.CollectionUnderRange = new Collection<SmartX.SmartLabel>();
            this.CollectionUnderRange.Clear();
            this.CollectionUnderRange.Add(this.labelGroup1Under);
            this.CollectionUnderRange.Add(this.labelGroup2Under);
            this.CollectionUnderRange.Add(this.labelGroup3Under);
            this.CollectionUnderRange.Add(this.labelGroup4Under);
            this.CollectionUnderRange.Add(this.labelGroup5Under);

            this.CollectionPassRange = new Collection<SmartX.SmartLabel>();
            this.CollectionPassRange.Clear();
            this.CollectionPassRange.Add(this.labelGroup1Pass);
            this.CollectionPassRange.Add(this.labelGroup2Pass);
            this.CollectionPassRange.Add(this.labelGroup3Pass);
            this.CollectionPassRange.Add(this.labelGroup4Pass);
            this.CollectionPassRange.Add(this.labelGroup5Pass);

            this.CollectionOverRange = new Collection<SmartX.SmartLabel>();
            this.CollectionOverRange.Clear();
            this.CollectionOverRange.Add(this.labelGroup1Over);
            this.CollectionOverRange.Add(this.labelGroup2Over);
            this.CollectionOverRange.Add(this.labelGroup3Over);
            this.CollectionOverRange.Add(this.labelGroup4Over);
            this.CollectionOverRange.Add(this.labelGroup5Over);

            this.CollectionUsing = new Collection<SmartX.SmartButton>();
            this.CollectionUsing.Clear();
            this.CollectionUsing.Add(this.buttonUsing1);
            this.CollectionUsing.Add(this.buttonUsing2);
            this.CollectionUsing.Add(this.buttonUsing3);
            this.CollectionUsing.Add(this.buttonUsing4);
            this.CollectionUsing.Add(this.buttonUsing5);
        }

        private void CalculatePassGap(int index)
        {
            for (int i = 0; i < this.CollectionPassGap.Count; i++)
            {
                this.CollectionPassGap[i] = this.CollectionRandomItem[i].PassRangeInt -
                    this.CollectionRandomItem[index].PassRangeInt;
            }
        }
        private void DecideRandomModeRange()
        {
            double tempDouble;
            for (int i = 0; i < this.CollectionRange.Count; i++)
            {
                tempDouble = (this.CollectionRandomItem[i + 1].UnderRangeInt - this.CollectionRandomItem[i].OverRangeInt) / 2
                    + this.CollectionRandomItem[i].OverRangeInt;
                tempDouble = Math.Round(tempDouble, 0);
                this.CollectionRange[i].Text = 
                    Helper.StringToDecimalPlaces(tempDouble.ToString().Trim(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            }
        }
        private int FindLabelName(SmartX.SmartLabel label, Collection<SmartX.SmartLabel> collectionLable)
        {
            int returnValue = 0;
            for (int i = 0; i < 5; i++)
            {
                if (label.Name == collectionLable[i].Name)
                {
                    returnValue = i;
                    break;
                }
            }
            return returnValue;
        }
        private void DisplayControls(Collection<ProductItem> pItem)
        {
            int iValue = 0;
            string sValue = "";

            if (this.CollectionRandomItem == null && pItem == null)
                return;

            for (int i = 0; i < this.CollectionUnderRange.Count; i++)
            {
                sValue = Helper.StringToDecimalPlaces(pItem[i].UnderRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                this.CollectionUnderRange[i].Text = sValue;
            }

            for (int i = 0; i < this.CollectionPassRange.Count; i++)
            {
                sValue = Helper.StringToDecimalPlaces(pItem[i].PassRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                this.CollectionPassRange[i].Text = sValue;
            }

            for (int i = 0; i < this.CollectionOverRange.Count; i++)
            {
                sValue = Helper.StringToDecimalPlaces(pItem[i].OverRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                this.CollectionOverRange[i].Text = sValue;
            }

            this.DecideRandomModeRange();
        }

        public void DisplayRefresh()
        {
            this.ParentForm.ParentForm.LoadProductFile(ref this.ProductItemRandom1, 995);
            this.ParentForm.ParentForm.LoadProductFile(ref this.ProductItemRandom2, 996);
            this.ParentForm.ParentForm.LoadProductFile(ref this.ProductItemRandom3, 997);
            this.ParentForm.ParentForm.LoadProductFile(ref this.ProductItemRandom4, 998);
            this.ParentForm.ParentForm.LoadProductFile(ref this.ProductItemRandom5, 999);

            this.CollectionRandomItem[0] = this.ProductItemRandom1;
            this.CollectionRandomItem[1] = this.ProductItemRandom2;
            this.CollectionRandomItem[2] = this.ProductItemRandom3;
            this.CollectionRandomItem[3] = this.ProductItemRandom4;
            this.CollectionRandomItem[4] = this.ProductItemRandom5;

            #region 사용여부
            for (int i = 0; i < this.CollectionUsing.Count; i++)
                this.CollectionUsing[i].Click -= new EventHandler(this.buttonUsing_Click);

            if (this.ParentForm.ParentForm.SystemConfig2.IsGroup1UsingRandomMode == true)
                this.CollectionUsing[0].ButtonDown();
            else
                this.CollectionUsing[0].ButtonUp();// 버튼 ON

            if (this.ParentForm.ParentForm.SystemConfig2.IsGroup2UsingRandomMode == true)
                this.CollectionUsing[1].ButtonDown();
            else
                this.CollectionUsing[1].ButtonUp();// 버튼 ON

            if (this.ParentForm.ParentForm.SystemConfig2.IsGroup3UsingRandomMode == true)
                this.CollectionUsing[2].ButtonDown();
            else
                this.CollectionUsing[2].ButtonUp();// 버튼 ON

            if (this.ParentForm.ParentForm.SystemConfig2.IsGroup4UsingRandomMode == true)
                this.CollectionUsing[3].ButtonDown();
            else
                this.CollectionUsing[3].ButtonUp();// 버튼 ON

            if (this.ParentForm.ParentForm.SystemConfig2.IsGroup5UsingRandomMode == true)
                this.CollectionUsing[4].ButtonDown();
            else
                this.CollectionUsing[4].ButtonUp();// 버튼 ON
  
            for (int i = 0; i < this.CollectionUsing.Count; i++)
                this.CollectionUsing[i].Click += new EventHandler(this.buttonUsing_Click);
            #endregion

            this.buttonSave.Visible = false;

            if (this.ParentForm.ParentForm.SystemConfig1.Unit == "g")
            {
                if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 1)
                    this.DifferenceValue = 500;
                else if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 2)
                    this.DifferenceValue = 5000;
                else
                    this.DifferenceValue = 50;
            }
            else
            {
                if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 1)
                    this.DifferenceValue = 10;
                else if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 2)
                    this.DifferenceValue = 100;
                else
                    this.DifferenceValue = 1;
            }

            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = DataStore.DisplayStore.BasicRandomMode;
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);
            this.DisplayControls(this.CollectionRandomItem);
        }
        #endregion

        #region Event Handler
        private void labelGroupUnder_Click(object sender, EventArgs e)
        {
            SmartX.SmartLabel label = sender as SmartX.SmartLabel;
            int index = 0;

            if (label == null)
                return;

            index = this.FindLabelName(label, this.CollectionUnderRange);

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(label.Text, 5, 
                this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces, false, this.ParentForm.ParentForm.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue > Helper.StringToWeight(this.CollectionRandomItem[index].PassRange.ToString(), 
                    this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces))
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language);
                    myMsg.ShowDialog();
                    return;
                }
                else
                {
                    if (index != 0)
                    {
                        double previousOverWeight = Helper.StringToWeight((this.CollectionRandomItem[index - 1].OverRangeInt + this.DifferenceValue).ToString(),
                            this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);

                        if (index == 4)
                        {
                            if (previousOverWeight > myKeyPad.doubleValue)
                            {
                                // 996번 범위 : ~XXX.X, ~번 범위 : XXX.X ~ XXX.X, 1000번 범위 : XXX.X ~
                                DialogFormMessage myMsg = new DialogFormMessage(28, index + 996,
                                    Helper.StringToDecimalPlaces((this.CollectionRandomItem[index - 1].OverRangeInt + this.DifferenceValue).ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces), "",
                                    this.ParentForm.ParentForm.SystemConfig1.Language);
                                myMsg.ShowDialog();
                                return;
                            }
                        }
                        else
                        {
                            if (previousOverWeight > myKeyPad.doubleValue)
                            {
                                // 996번 범위 : ~XXX.X, ~번 범위 : XXX.X ~ XXX.X, 1000번 범위 : XXX.X ~
                                DialogFormMessage myMsg = new DialogFormMessage(28, index + 996,
                                    Helper.StringToDecimalPlaces((this.CollectionRandomItem[index - 1].OverRangeInt + this.DifferenceValue).ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces),
                                    Helper.StringToDecimalPlaces((this.CollectionRandomItem[index + 1].UnderRangeInt - this.DifferenceValue).ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces),
                                    this.ParentForm.ParentForm.SystemConfig1.Language);
                                myMsg.ShowDialog();
                                return;
                            }
                        }
                    }

                    this.ParentForm.ParentForm.ClearAlarm(0);

                    label.Text = myKeyPad.StringValue;
                    this.CollectionRandomItem[index].UnderRange = myKeyPad.StringValue.Replace(".", "");
                    this.buttonSave.Visible = true;
                    this.DisplayControls(this.CollectionRandomItem);
                }
            }
        }

        private void labelGroupPass_Click(object sender, EventArgs e)
        {
            SmartX.SmartLabel label = sender as SmartX.SmartLabel;
            int index = 0;

            if (label == null)
                return;

            index = this.FindLabelName(label, this.CollectionPassRange);

            int digit = 0, passGap = 0, temp = 0, range = 0;
            string value = "";
            int oldUnderRangeDeviation;
            int oldOverRangeDeviation;
            int oldPassRange = this.CollectionRandomItem[index].PassRangeInt;
            this.CalculatePassGap(index);

             DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(label.Text, 5, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces, false, this.ParentForm.ParentForm.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() != DialogResult.OK)
                return;

            this.ParentForm.ParentForm.ClearAlarm(0);

            if (int.Parse(myKeyPad.StringValue.Replace(".", "")) < this.CollectionRandomItem[index].UnderRangeInt
                || int.Parse(myKeyPad.StringValue.Replace(".", "")) > this.CollectionRandomItem[index].OverRangeInt)
            {
                // 입력범위를 확인하세요
                DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language);
                myMsg.ShowDialog();
            }
            else
            {
                #region 사용안함
                //if (index == 0)
                //{
                //    #region 996번 선택 시
                //    if (int.Parse(myKeyPad.StringValue.Replace(".", "")) > this.CollectionRandomItem[1].UnderRangeInt - this.DifferenceValue)
                //    {
                //        // 996번 범위 : ~XXX.X, ~번 범위 : XXX.X ~ XXX.X, 1000번 범위 : XXX.X ~
                //        DialogFormMessage myMsg = new DialogFormMessage(28, index + 996, this.ParentForm.ParentForm.SystemConfig1.Language);
                //        myMsg.ShowDialog();
                //        return;
                //    }
                //    #endregion
                //}
                //else if (index == 4)
                //{
                //    #region 1000번 선택 시
                //    if (int.Parse(myKeyPad.StringValue.Replace(".", "")) < this.CollectionRandomItem[3].OverRangeInt + this.DifferenceValue)
                //    {
                //        // 996번 범위 : ~XXX.X, ~번 범위 : XXX.X ~ XXX.X, 1000번 범위 : XXX.X ~
                //        DialogFormMessage myMsg = new DialogFormMessage(28, index + 996, this.ParentForm.ParentForm.SystemConfig1.Language);
                //        myMsg.ShowDialog();
                //        return;
                //    }
                //    #endregion
                //}
                //else
                //{
                //    if (int.Parse(myKeyPad.StringValue.Replace(".", "")) > this.CollectionRandomItem[index + 1].UnderRangeInt - this.DifferenceValue)
                //    {
                //        // 996번 범위 : ~XXX.X, ~번 범위 : XXX.X ~ XXX.X, 1000번 범위 : XXX.X ~
                //        DialogFormMessage myMsg = new DialogFormMessage(28, index + 996, this.ParentForm.ParentForm.SystemConfig1.Language);
                //        myMsg.ShowDialog();
                //        return;
                //    }
                //    else if (int.Parse(myKeyPad.StringValue.Replace(".", "")) < this.CollectionRandomItem[index - 1].OverRangeInt + this.DifferenceValue)
                //    {
                //        // 996번 범위 : ~XXX.X, ~번 범위 : XXX.X ~ XXX.X, 1000번 범위 : XXX.X ~
                //        DialogFormMessage myMsg = new DialogFormMessage(28, index + 996, this.ParentForm.ParentForm.SystemConfig1.Language);
                //        myMsg.ShowDialog();
                //        return;
                //    }                    
                //} 
                #endregion
                label.Text = myKeyPad.StringValue;
                this.CollectionRandomItem[index].PassRange = myKeyPad.StringValue.Replace(".", "");
                this.buttonSave.Visible = true;
                this.DisplayControls(this.CollectionRandomItem);
            }

            #region 사용안함
            //if (this.checkBoxSync.Checked == true)
            //{
            //    #region 싱크 체크 시
            //    passGap = int.Parse(myKeyPad.StringValue.Replace(".", "")) - oldPassRange;

            //    for (int i = index; i < 5; i++)
            //    {
            //        oldUnderRangeDeviation = this.CollectionRandomItem[i].UnderRangDeviation;
            //        oldOverRangeDeviation = this.CollectionRandomItem[i].OverRangeDeviation;

            //        #region PassRange
            //        temp = oldPassRange + this.CollectionPassGap[i] + passGap;
            //        if (temp < 0)
            //            value = "0";
            //        else
            //            value = temp.ToString();

            //        this.CollectionRandomItem[i].PassRange = value;
            //        #endregion
            //        #region UnderRange
            //        temp = this.CollectionRandomItem[i].PassRangeInt + oldUnderRangeDeviation;
            //        if (temp < 0)
            //            value = "0";
            //        else
            //            value = temp.ToString();

            //        this.CollectionRandomItem[i].UnderRange = value;
            //        #endregion
            //        #region OverRange
            //        if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 2)
            //            range = 1000000;
            //        else if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 1)
            //            range = 100000;
            //        else
            //            range = 10000;
            //        temp = this.CollectionRandomItem[i].PassRangeInt + oldOverRangeDeviation;
            //        if (temp >= range)
            //            value = myKeyPad.StringValue.Replace(".", "");
            //        else if (temp < 0)
            //            value = "0";
            //        else
            //            value = temp.ToString();

            //        this.CollectionRandomItem[i].OverRange = value;
            //        #endregion
            //        this.buttonSave.Visible = true;
            //    }
            //    #endregion
            //}
            //else
            //{
            //    #region 싱크 미체크 시
            //    if (index == 0)
            //    {
            //        #region 996번 선택 시
            //        if (int.Parse(myKeyPad.StringValue.Replace(".", "")) > this.CollectionRandomItem[1].UnderRangeInt - this.DifferenceValue)
            //        {
            //            // ~의 하한값보다 50g 이하값을 입력하세요.
            //            DialogFormMessage myMsg = new DialogFormMessage(1, index + 997, this.ParentForm.ParentForm.SystemConfig1.Language);
            //            myMsg.ShowDialog();
            //        }
            //        else
            //        {
            //            oldUnderRangeDeviation = this.CollectionRandomItem[index].UnderRangDeviation;
            //            oldOverRangeDeviation = this.CollectionRandomItem[index].OverRangeDeviation;

            //            this.CollectionRandomItem[index].PassRange = myKeyPad.StringValue.Replace(".", "");

            //            #region UnderRange
            //            temp = this.CollectionRandomItem[index].PassRangeInt + oldUnderRangeDeviation;
            //            if (temp < 0)
            //                value = "0";
            //            else
            //                value = temp.ToString();

            //            this.CollectionRandomItem[index].UnderRange = value;
            //            #endregion

            //            #region OverRange
            //            if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 2)
            //                range = 1000000;
            //            else if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 1)
            //                range = 100000;
            //            else
            //                range = 10000;
            //            temp = this.CollectionRandomItem[index].PassRangeInt + oldOverRangeDeviation;
            //            if (temp >= range)
            //                value = myKeyPad.StringValue.Replace(".", "");
            //            else if (temp > this.CollectionRandomItem[index + 1].UnderRangeInt - this.DifferenceValue)
            //                value = (this.CollectionRandomItem[index + 1].UnderRangeInt - this.DifferenceValue).ToString();
            //            else
            //                value = temp.ToString();

            //            this.CollectionRandomItem[index].OverRange = value;
            //            #endregion
            //            this.buttonSave.Visible = true;
            //        }
            //        #endregion
            //    }
            //    else if (index == 4)
            //    {
            //        #region 1000번 선택 시
            //        if (int.Parse(myKeyPad.StringValue.Replace(".", "")) < this.CollectionRandomItem[3].OverRangeInt + this.DifferenceValue)
            //        {
            //            // ~의 상한값보다 50g 이상값을 입력하세요.
            //            DialogFormMessage myMsg = new DialogFormMessage(2, index + 995, this.ParentForm.ParentForm.SystemConfig1.Language);
            //            myMsg.ShowDialog();
            //        }
            //        else
            //        {
            //            oldUnderRangeDeviation = this.CollectionRandomItem[index].UnderRangDeviation;
            //            oldOverRangeDeviation = this.CollectionRandomItem[index].OverRangeDeviation;

            //            this.CollectionRandomItem[index].PassRange = myKeyPad.StringValue.Replace(".", "");

            //            #region UnderRange
            //            temp = this.CollectionRandomItem[index].PassRangeInt + oldUnderRangeDeviation;
            //            if (temp < 0)
            //                value = "0";
            //            else if (temp < this.CollectionRandomItem[index - 1].OverRangeInt + this.DifferenceValue)
            //                value = (this.CollectionRandomItem[index - 1].OverRangeInt + this.DifferenceValue).ToString();
            //            else
            //                value = temp.ToString();

            //            this.CollectionRandomItem[index].UnderRange = value;
            //            #endregion

            //            #region OverRange
            //            if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 2)
            //                range = 1000000;
            //            else if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 1)
            //                range = 100000;
            //            else
            //                range = 10000;
            //            temp = this.CollectionRandomItem[index].PassRangeInt + oldOverRangeDeviation;
            //            if (temp >= range)
            //                value = myKeyPad.StringValue.Replace(".", "");
            //            else
            //                value = temp.ToString();

            //            this.CollectionRandomItem[index].OverRange = value;
            //            #endregion
            //            this.buttonSave.Visible = true;
            //        }
            //        #endregion
            //    }
            //    else
            //    {
            //        oldUnderRangeDeviation = this.CollectionRandomItem[index].UnderRangDeviation;
            //        oldOverRangeDeviation = this.CollectionRandomItem[index].OverRangeDeviation;

            //        if (int.Parse(myKeyPad.StringValue.Replace(".", "")) > this.CollectionRandomItem[index + 1].UnderRangeInt - this.DifferenceValue)
            //        {
            //            // ~번의 하한값보다 50g 이하값을 입력하세요.
            //            DialogFormMessage myMsg = new DialogFormMessage(1, index + 997, this.ParentForm.ParentForm.SystemConfig1.Language);
            //            myMsg.ShowDialog();
            //        }
            //        else if (int.Parse(myKeyPad.StringValue.Replace(".", "")) < this.CollectionRandomItem[index - 1].OverRangeInt + this.DifferenceValue)
            //        {
            //            // ~의 상한값보다 50g 이상값을 입력하세요.
            //            DialogFormMessage myMsg = new DialogFormMessage(2, index + 995, this.ParentForm.ParentForm.SystemConfig1.Language);
            //            myMsg.ShowDialog();
            //        }
            //        else
            //        {

            //            this.CollectionRandomItem[index].PassRange = myKeyPad.StringValue.Replace(".", "");

            //            #region UnderRange
            //            temp = this.CollectionRandomItem[index].PassRangeInt + oldUnderRangeDeviation;
            //            if (temp < 0)
            //                value = "0";
            //            else if (temp < this.CollectionRandomItem[index - 1].OverRangeInt + this.DifferenceValue)
            //                value = (this.CollectionRandomItem[index - 1].OverRangeInt + this.DifferenceValue).ToString();
            //            else
            //                value = temp.ToString();

            //            this.CollectionRandomItem[index].UnderRange = value;
            //            #endregion

            //            #region OverRange
            //            if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 2)
            //                range = 1000000;
            //            else if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 1)
            //                range = 100000;
            //            else
            //                range = 10000;
            //            temp = this.CollectionRandomItem[index].PassRangeInt + oldOverRangeDeviation;
            //            if (temp >= range)
            //                value = myKeyPad.StringValue.Replace(".", "");
            //            else if (temp > this.CollectionRandomItem[index + 1].UnderRangeInt - this.DifferenceValue)
            //                value = (this.CollectionRandomItem[index + 1].UnderRangeInt - this.DifferenceValue).ToString();
            //            else
            //                value = temp.ToString();

            //            this.CollectionRandomItem[index].OverRange = value;
            //            #endregion
            //            this.buttonSave.Visible = true;
            //        }
            //    }
            //    #endregion
            //} 
            #endregion
        }

        private void labelGroupOver_Click(object sender, EventArgs e)
        {
            SmartX.SmartLabel label = sender as SmartX.SmartLabel;
            int index = 0;

            if (label == null)
                return;

            index = this.FindLabelName(label, this.CollectionOverRange);

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(label.Text, 5, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < Helper.StringToWeight(this.CollectionRandomItem[index].PassRange.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces))
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language);
                    myMsg.ShowDialog();
                    return;
                }
                else
                {
                    if (index != 4)
                    {
                        if (index == 0)
                        {
                            if (Helper.StringToWeight((this.CollectionRandomItem[index + 1].UnderRangeInt - this.DifferenceValue).ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces) < myKeyPad.doubleValue)
                            {
                                // 996번 범위 : ~XXX.X, ~번 범위 : XXX.X ~ XXX.X, 1000번 범위 : XXX.X ~
                                DialogFormMessage myMsg = new DialogFormMessage(28, index + 996,
                                    "", Helper.StringToDecimalPlaces((this.CollectionRandomItem[index + 1].UnderRangeInt - this.DifferenceValue).ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces),
                                    this.ParentForm.ParentForm.SystemConfig1.Language);
                                myMsg.ShowDialog();
                                return;
                            }
                        }
                        else
                        {
                            if (Helper.StringToWeight((this.CollectionRandomItem[index + 1].UnderRangeInt - this.DifferenceValue).ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces) < myKeyPad.doubleValue)
                            {
                                // 996번 범위 : ~XXX.X, ~번 범위 : XXX.X ~ XXX.X, 1000번 범위 : XXX.X ~
                                DialogFormMessage myMsg = new DialogFormMessage(28, index + 996,
                                    Helper.StringToDecimalPlaces((this.CollectionRandomItem[index - 1].OverRangeInt + this.DifferenceValue).ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces),
                                    Helper.StringToDecimalPlaces((this.CollectionRandomItem[index + 1].UnderRangeInt - this.DifferenceValue).ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces),
                                    this.ParentForm.ParentForm.SystemConfig1.Language);
                                myMsg.ShowDialog();
                                return;
                            }
                        }
                    }

                    this.ParentForm.ParentForm.ClearAlarm(0);

                    label.Text = myKeyPad.StringValue;
                    this.CollectionRandomItem[index].OverRange = myKeyPad.StringValue.Replace(".", "");
                    this.buttonSave.Visible = true;
                    this.DisplayControls(this.CollectionRandomItem);
                }
            }
        }

        private void buttonUsing_Click(object sender, EventArgs e)
        {
            SmartX.SmartButton button = sender as SmartX.SmartButton;
            SmartX.SmartButton.BUTSTATUS buttonStatus;
            int buttonNo = 0;

            if (button == null)
                return;

            switch (button.Name)
            {
                case "buttonUsing1":
                    buttonNo = 1;
                    break;
                case "buttonUsing2":
                    buttonNo = 2;
                    break;
                case "buttonUsing3":
                    buttonNo = 3;
                    break;
                case "buttonUsing4":
                    buttonNo = 4;
                    break;
                case "buttonUsing5":
                    buttonNo = 5;
                    break;
                default:
                    break;
            }

            if (button.ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN) // 버튼 ON
            {
                for (int i = 0; i < buttonNo; i++)
                    this.CollectionUsing[i].ButtonDown();
            }
            else // 버튼 OFF
            {
                for (int i = 4; i >= buttonNo - 1; i--)
                    this.CollectionUsing[i].ButtonUp();
            }

            this.buttonSave.Visible = true;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            #region 사용여부
            if (this.CollectionUsing[0].ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN) // 버튼 ON
                this.ParentForm.ParentForm.SystemConfig2.IsGroup1UsingRandomMode = true;
            else
                this.ParentForm.ParentForm.SystemConfig2.IsGroup1UsingRandomMode = false;

            if (this.CollectionUsing[1].ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN) // 버튼 ON
                this.ParentForm.ParentForm.SystemConfig2.IsGroup2UsingRandomMode = true;
            else
                this.ParentForm.ParentForm.SystemConfig2.IsGroup2UsingRandomMode = false;

            if (this.CollectionUsing[2].ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN) // 버튼 ON
                this.ParentForm.ParentForm.SystemConfig2.IsGroup3UsingRandomMode = true;
            else
                this.ParentForm.ParentForm.SystemConfig2.IsGroup3UsingRandomMode = false;

            if (this.CollectionUsing[3].ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN) // 버튼 ON
                this.ParentForm.ParentForm.SystemConfig2.IsGroup4UsingRandomMode = true;
            else
                this.ParentForm.ParentForm.SystemConfig2.IsGroup4UsingRandomMode = false;

            if (this.CollectionUsing[4].ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN) // 버튼 ON
                this.ParentForm.ParentForm.SystemConfig2.IsGroup5UsingRandomMode = true;
            else
                this.ParentForm.ParentForm.SystemConfig2.IsGroup5UsingRandomMode = false;

            int index = 995;

            if (this.ParentForm.ParentForm.SystemConfig2.IsUsingRandomMode == true)
            {
                if (this.ParentForm.CenterBasicProduct.listBoxProductList.Items.Contains(this.ParentForm.ParentForm.CollectionProductItem[995].Name) == true)
                {
                    for (int i = 0; i < 5; i++)
                        this.ParentForm.CenterBasicProduct.listBoxProductList.Items.RemoveAt(index);
                }

                this.ParentForm.ParentForm.SystemConfig1.ProductNumber = 996;
            }
            else
            {
                if(this.ParentForm.CenterBasicProduct.listBoxProductList.Items.Contains(this.ParentForm.ParentForm.CollectionProductItem[995].Name) == false)
                    for (int i = 0; i < 5; i++)
                        this.ParentForm.CenterBasicProduct.listBoxProductList.Items.Insert(index + i, "Product " + (index + 1 + i).ToString());

                this.ParentForm.ParentForm.SystemConfig1.ProductNumber = 1;
            }
            #endregion

            this.buttonSave.Visible = false;
            this.ParentForm.ParentForm.SaveSystemConfigurationFile2(this.ParentForm.ParentForm.SystemConfig2);
            for (int i = 0; i < 5; i++)
                this.ParentForm.ParentForm.SaveProductFile(this.CollectionRandomItem[i], 995 + i);
            this.ParentForm.ParentForm.ReLoadProductFile();
            this.ParentForm.ParentForm.TransferRandomMode();
            this.DisplayControls(this.CollectionRandomItem);
        }
        #endregion
    }
}
