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_0H.DialogForms;
using ITC81DB_0H.Forms;
using SmartX;
using System.Collections.ObjectModel;
using ITC81DB_0H_ImageDll;

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

        private int MovePoint;
        //private int SelectedProductNo;
        private string RefrenceADC;
        //private ProductItem SelectedProductItem;
        //private JudgmentSetItem SelectedJudgmentSetItem;
        private JudgmentSetItem CaptureJudgmentSetItem;
        private Collection<int> CollectionGraphDataTemp;
        #endregion

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

            this.ParentForm = parent;

            this.InitializeDesign();
            this.DefaultSetting();

            this.CollectionGraphDataTemp = new Collection<int>();
            this.CollectionGraphDataTemp.Clear();
            for (int i = 0; i < 300; i++)
                this.CollectionGraphDataTemp.Add(0);
        }
        #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.buttonWeightReference.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engJudgmentStandardDisable));
                this.buttonWeightReference.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engJudgmentStandardDown));
                this.buttonWeightReference.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engJudgmentStandardUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
            {
                this.buttonWeightReference.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnFormJudgmentStandardDisable));
                this.buttonWeightReference.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnFormJudgmentStandardDown));
                this.buttonWeightReference.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnFormJudgmentStandardUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
            {
                this.buttonWeightReference.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusJudgmentStandardDisable));
                this.buttonWeightReference.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusJudgmentStandardDown));
                this.buttonWeightReference.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusJudgmentStandardUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
                this.buttonWeightReference.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerJudgmentStandardDisable));
                this.buttonWeightReference.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerJudgmentStandardDown));
                this.buttonWeightReference.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerJudgmentStandardUp));
            }
            else
            {
                this.buttonWeightReference.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korJudgmentStandardDisable));
                this.buttonWeightReference.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korJudgmentStandardDown));
                this.buttonWeightReference.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korJudgmentStandardUp));
            }

            this.ChangeScreen();
        }
        private void DefaultSetting()
        {
            this.RefrenceADC = "0";
            this.CaptureJudgmentSetItem = new JudgmentSetItem();

            this.ControlInitializeAsEquipmentType();
            this.ControlInitialize();
        }

        private void ControlInitializeAsEquipmentType()
        {

        }
        private void ControlInitialize()
        {
            this.buttonProductNo.Text = this.ParentForm.ParentForm.SystemConfig1.ProductNumber.ToString();
            this.buttonCopy.Text = "00";
            this.labelSensingTime.Text = "";
            this.buttonWeightReference.Text = Helper.StringToDecimalPlaces("0", this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            this.labelWeightJudgment.Text = Helper.StringToDecimalPlaces("0", this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);

            this.labelDoubleDelayTime.Text = " -";
            this.labelJudgmentDelayTime.Text = " -";
        }
       
        public void ChangeScreen()
        {
            Class1 images = new Class1();

            this.buttonUp.Visible = false;
            this.buttonDown.Visible = false;

            // 0 : 컨베어, 4 : 업다운, 6 : 정지계량
            if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.English)
            {
                switch (int.Parse(this.ParentForm.ParentForm.CurrentSystemParameter1.EquipmentType))
                {
                    case 0:
                        this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engBackgroundJudgment));
                        break;
                    case 4:
                        this.buttonUp.Visible = true;
                        this.buttonDown.Visible = true;
                        this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engBackgroundJudgment_Updown));
                        break;
                    case 6:
                        this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engBackgroundJudgment_StopWeighing));
                        break;
                    default:
                        this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engBackgroundJudgment));
                        break;
                }
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
            {
                switch (int.Parse(this.ParentForm.ParentForm.CurrentSystemParameter1.EquipmentType))
                {
                    case 0:
                        this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackgroundJudgment));
                        break;
                    case 4:
                        this.buttonUp.Visible = true;
                        this.buttonDown.Visible = true;
                        this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackgroundJudgment_Updown));
                        break;
                    case 6:
                        this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackgroundJudgment_StopWeighing));
                        break;
                    default:
                        this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackgroundJudgment));
                        break;
                }
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
            {
                switch (int.Parse(this.ParentForm.ParentForm.CurrentSystemParameter1.EquipmentType))
                {
                    case 0:
                        this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.rusBackgroundJudgment));
                        break;
                    case 4:
                        this.buttonUp.Visible = true;
                        this.buttonDown.Visible = true;
                        this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.rusBackgroundJudgment_Updown));
                        break;
                    case 6:
                        this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.rusBackgroundJudgment_StopWeighing));
                        break;
                    default:
                        this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.rusBackgroundJudgment));
                        break;
                }
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
                switch (int.Parse(this.ParentForm.ParentForm.CurrentSystemParameter1.EquipmentType))
                {
                    case 0:
                        this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerBackgroundJudgment));
                        break;
                    case 4:
                        this.buttonUp.Visible = true;
                        this.buttonDown.Visible = true;
                        this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerBackgroundJudgment_Updown));
                        break;
                    case 6:
                        this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerBackgroundJudgment_StopWeighing));
                        break;
                    default:
                        this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerBackgroundJudgment));
                        break;
                }
            }
            else
            {
                switch (int.Parse(this.ParentForm.ParentForm.CurrentSystemParameter1.EquipmentType))
                {
                    case 0:
                        this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korBackgroundJudgment));
                        break;
                    case 4:
                        this.buttonUp.Visible = true;
                        this.buttonDown.Visible = true;
                        this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korBackgroundJudgment_Updown));
                        break;
                    case 6:
                        this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korBackgroundJudgment_StopWeighing));
                        break;
                    default:
                        this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korBackgroundJudgment));
                        break;
                }
            }

            //this.LabelLocation(int.Parse(this.ParentForm.ParentForm.CurrentSystemParameter1.EquipmentType));
        }
        private void IsAutoMode(bool value)
        {
            if (value == true)
            {
                this.labelJudgmentDelayTime.ForeColor = Color.Gray;
                this.labelDoubleDelayTime.ForeColor = Color.Gray;
                this.labelFeedSpeed.ForeColor = Color.Gray;
                this.labelJudgmentDelayTime.Enabled = false;
                this.labelDoubleDelayTime.Enabled = false;
                this.labelFeedSpeed.Enabled = false;
            }
            else
            {
                this.labelJudgmentDelayTime.ForeColor = Color.White;
                this.labelDoubleDelayTime.ForeColor = Color.White;
                this.labelFeedSpeed.ForeColor = Color.White;
                this.labelJudgmentDelayTime.Enabled = true;
                this.labelDoubleDelayTime.Enabled = true;
                this.labelFeedSpeed.Enabled = true;
            }
        }

        // 모드별 배경화면 및 Label Visible/위치 변경
        private void LabelLocation(int equipmentType)
        {
            switch (equipmentType)
            {
                case 4:
                     break;
                default:
                    break;
            }
        }

        public void CaculateJudgmentData(int time1)
        {
            if (this.buttonUsingAutoMode.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
            {
                double time2; // 시간2 : (컨베이어 길이 - 제품길이) / 속도
                double speed; // 이송속도 : 제품길이 / 시간1

                // 이중지연 = 시간1 = 센서 검출 시간
                this.ParentForm.ParentForm.CurrentJudgmentSetItem.DoubleDelayTime = time1;
                this.labelDoubleDelayTime.Text = this.ParentForm.ParentForm.CurrentJudgmentSetItem.DoubleDelayTime.ToString();

                // 이송속도 = 제품길이 / 시간1
                speed = Convert.ToDouble(this.ParentForm.ParentForm.CurrentJudgmentSetItem.ProductLength) / Convert.ToDouble(time1);
                speed = Math.Round(speed + 0.5, 2);
                this.labelFeedSpeed.Text = speed.ToString();

                time2 = Convert.ToDouble(this.ParentForm.ParentForm.SystemConfig2.ConveyorLength -
                    this.ParentForm.ParentForm.CurrentJudgmentSetItem.ProductLength) / speed;
                time2 = Math.Floor(time2);

                // 판정지연 = 시간1 + 시간2(=(컨베어 길이-제품길이) / 속도)
                this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime = time1 + Convert.ToInt32(time2);
                this.labelJudgmentDelayTime.Text = this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime.ToString();

                this.ParentForm.ParentForm.TransferProductParameter(this.ParentForm.ParentForm.SystemConfig1.ProductNumber);

                this.buttonUsingAutoMode.ButtonUp();
                this.IsAutoMode(false);
            }

            this.labelSensingTime.Text = time1.ToString();
        }

        private void UpdateSystemParameterDisplay(JudgmentSetItem item)
        {
            string value = "";

            value = item.DescendDelayTime.ToString();
            if (this.labelDescendDelayTime.Text != value)
                this.labelDescendDelayTime.Text = value;

            value = item.AscendDelayTime.ToString();
            if (this.labelAscendDelayTime.Text != value)
                this.labelAscendDelayTime.Text = value;

            value = item.JudgmentDelayTime.ToString();
            if (this.labelJudgmentDelayTime.Text != value)
                this.labelJudgmentDelayTime.Text = value;

            value = item.DoubleDelayTime.ToString();
            if (this.labelDoubleDelayTime.Text != value)
                this.labelDoubleDelayTime.Text = value;

            value = item.JudgmentCount.ToString();
            if (this.labelJudgmentCount.Text != value)
                this.labelJudgmentCount.Text = value;

            value = item.FeedSpeed1.ToString();
            if (this.labelFeedSpeed.Text != value)
                this.labelFeedSpeed.Text = value;

            value = Helper.DoubleToString(item.DynamicCorrection, 6);
            if (this.labelDynamicCorrection.Text != value)
                this.labelDynamicCorrection.Text = value;

        }
        public void UpdateEquipmentStatusDisplay(DataStore.EquipmentStatus status)
        {
            if (status == DataStore.EquipmentStatus.Start)
            {
                this.buttonProductNo.Enabled = false;
                this.buttonWeightReference.Enabled = false;
                this.buttonCopy.Enabled = false;
                this.buttonPaste.Enabled = false;
                this.buttonPasteAll.Enabled = false;
            }
            else
            {
                this.buttonProductNo.Enabled = true;
                this.buttonWeightReference.Enabled = true;
                this.buttonCopy.Enabled = true;
                this.buttonPaste.Enabled = true;
                this.buttonPasteAll.Enabled = true;
            }
        }
        public void UpdateDataDisplay(DataStore.EquipmentStatus status, WeightData weightData)
        {
            string sValue = "";

            sValue = Helper.DoubleToString(weightData.Weight, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            if (this.labelWeightJudgment.Text != sValue)
                this.labelWeightJudgment.Text = sValue;
        }
        public void UpdateCurrentProductDisplay(DataStore.EquipmentStatus status, ProductItem pItem)
        {
            string value = "";

            value = pItem.Number.ToString();
            if (this.buttonProductNo.Text != value)
                this.buttonProductNo.Text = value;

            this.UpdateSystemParameterDisplay(this.ParentForm.ParentForm.CurrentJudgmentSetItem);
        }
        public void UpdateStopWeightDisplay(DataStore.EquipmentStatus status, WeightData data)
        {
            string value = "";

            value = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            if (this.labelWeightValue.Text != value)
                this.labelWeightValue.Text = value;
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = DataStore.DisplayStore.SystemJudgmentManual;
            this.ParentForm.ParentForm.SetDisplayMode(DataStore.DisplayMode.SystemSetting);
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);

            this.CaptureJudgmentSetItem = new JudgmentSetItem();

            this.ControlInitialize();
            if (this.ParentForm != null)
                this.ParentForm.UpdateEquipmentStatusDisplay(this.ParentForm.ParentForm.CurrentSystemStatus.Equipment);
            this.UpdateSystemParameterDisplay(this.ParentForm.ParentForm.CurrentJudgmentSetItem);

            if (int.Parse(this.ParentForm.ParentForm.CurrentSystemParameter1.EquipmentType) != 0)
                this.buttonUsingAutoMode.Enabled = false;
            else
            {
                this.buttonUsingAutoMode.Enabled = true;
                this.buttonUsingAutoMode.ButtonUp();
            }
            this.IsAutoMode(false);

            // Read Calibration Constant
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.MainBoard, CommunicationAddress.ReadConstant, "");
        }
        #endregion

        #region Event Handler
        private void buttonProductNo_Click(object sender, EventArgs e)
        {
            string message = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.buttonProductNo.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

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

                    this.buttonProductNo.Text = "**";
                    this.ParentForm.ParentForm.TransferProductParameter(myKeyPad.IntValue);
                }
            }
        }

        private void labelJudgmentDelayTime_Click(object sender, EventArgs e)
        {
            string value = "", message = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelJudgmentDelayTime.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

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

                    value = Helper.StringZeroFillDigits4(myKeyPad.StringValue);

                    this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime = myKeyPad.IntValue;
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.JudgmentDelayTime, value);

                    if (this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime < this.ParentForm.ParentForm.CurrentJudgmentSetItem.DoubleDelayTime)
                    {
                        this.labelDoubleDelayTime.Text = this.labelJudgmentDelayTime.Text;

                        value = Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime.ToString());
                        this.ParentForm.ParentForm.CurrentJudgmentSetItem.DoubleDelayTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime;
                        this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.DoubleDelayTime, value);
                    }

                    this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);
                }
            }
        }
        private void labelDoubleDelayTime_Click(object sender, EventArgs e)
        {
            string value = "", message = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelDoubleDelayTime.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 0 || myKeyPad.doubleValue > 9999)
                {
                    // 입력범위를 확인하세요
                    message = "0~9999";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language, message);
                    myMsg.ShowDialog();
                }
                else
                {
                    if (myKeyPad.IntValue > this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime)
                    {
                        this.labelDoubleDelayTime.Text = this.labelJudgmentDelayTime.Text;
                        value = Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime.ToString());
                        this.ParentForm.ParentForm.CurrentJudgmentSetItem.DoubleDelayTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime;
                    }
                    else
                    {
                        this.labelDoubleDelayTime.Text = myKeyPad.StringValue;
                        value = Helper.StringZeroFillDigits4(myKeyPad.StringValue);
                        this.ParentForm.ParentForm.CurrentJudgmentSetItem.DoubleDelayTime = myKeyPad.IntValue;
                    }
                    
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.DoubleDelayTime, value);
                    this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);
                }
            }
        }
        private void labelJudgmentCount_Click(object sender, EventArgs e)
        {
            string value = "", message = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelJudgmentCount.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 1 || myKeyPad.doubleValue > 20)
                {
                    // 입력범위를 확인하세요
                    message = "1~20";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language, message);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelJudgmentCount.Text = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentCount = myKeyPad.IntValue;

                    value = Helper.StringZeroFillDigits4(myKeyPad.StringValue);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.JudgmentCount, value);

                    this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);
                }
            }
        }
        private void labelFeedSpeed_Click(object sender, EventArgs e)
        {
            string value = "", message = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelFeedSpeed.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 1 || myKeyPad.doubleValue > 120)
                {
                    // 입력범위를 확인하세요
                    message = "1~120";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language, message);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelFeedSpeed.Text = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.CurrentJudgmentSetItem.FeedSpeed1 = myKeyPad.IntValue;

                    value = Helper.StringZeroFillDigits4(myKeyPad.StringValue);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.FeedSpeed, value);

                    this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);
                }
            }
        }

        private void labelDescendDelayTime_Click(object sender, EventArgs e)
        {
            string value = "", message = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelDescendDelayTime.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

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

                    value = Helper.StringZeroFillDigits4(myKeyPad.StringValue);
                    this.ParentForm.ParentForm.CurrentJudgmentSetItem.DescendDelayTime = myKeyPad.IntValue;
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.DescendDelayTime, value);

                    this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);
                }
            }
        }
        private void labelAscendDelayTime_Click(object sender, EventArgs e)
        {
            string value = "", message = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelAscendDelayTime.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

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

                    value = Helper.StringZeroFillDigits4(myKeyPad.StringValue);
                    this.ParentForm.ParentForm.CurrentJudgmentSetItem.AscendDelayTime = myKeyPad.IntValue;
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.AscendDelayTime, value);

                    this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);
                }
            }
        }

        private void labelDynamicCorrection_Click(object sender, EventArgs e)
        {
            string value = "", message = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelDynamicCorrection.Text, 7, 6, false, this.ParentForm.ParentForm.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 0.500000 || myKeyPad.doubleValue > 2.000000)
                {
                    // 입력범위를 확인하세요
                    message = "0.500000~2.000000";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language, message);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelDynamicCorrection.Text = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.CurrentJudgmentSetItem.DynamicCorrection = myKeyPad.doubleValue;

                    value = string.Format("{0:f6}", this.labelDynamicCorrection.Text);
                    value = value.Remove(1, 1);
                    value = Helper.StringZeroFillDigits7(value);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.DynamicCorrection, value);

                    this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);
                }
            }
        }

        private void buttonUsingAutoMode_Click(object sender, EventArgs e)
        {
            if (this.buttonUsingAutoMode.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
            {
                this.IsAutoMode(true);
            }
            else
            {
                this.IsAutoMode(false);
            }
        }
        private void buttonToLeftOrRight_Click(object sender, EventArgs e)
        {
            //int iValue = 0, temp = 0, inputValue = 0, graphInitValue = 0, unit = 50;
            //int removeData = 0;

            //SmartX.SmartButton button = sender as SmartX.SmartButton;

            //if (button == null)
            //    return;

            //if (button == this.buttonToLeft)
            //{
            //    this.MovePoint -= 1;
            //    removeData = (unit * 2) / (this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime / 150);
            //    this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime += unit;
            //}
            //else
            //{
            //    this.MovePoint += 1;
            //    removeData = (unit * 2) / (this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime / 150);
            //    this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime -= unit;
            //}

            //this.labelJudgmentDelayTime.Text = this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime.ToString();
            //this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);
            //this.ParentForm.ParentForm.TransferProductParameter(this.ParentForm.ParentForm.SystemConfig1.ProductNumber);

            //this.smartDraw.PutDataAllClear();

            //iValue = int.Parse(this.RefrenceADC);
            //temp = 120 - iValue;

            //if (this.MovePoint < 0)
            //{
            //    graphInitValue = removeData * this.MovePoint * -1;

            //    for (int i = graphInitValue; i < this.CollectionGraphDataTemp.Count; i++)
            //    {
            //        inputValue = this.CollectionGraphDataTemp[i] + temp;

            //        if (inputValue > 240)
            //            inputValue = 240;
            //        else if (inputValue < 0)
            //            inputValue = 1;

            //        this.smartDraw.PutData(inputValue);
            //    }

            //    for (int i = 0; i < graphInitValue; i++)
            //        this.smartDraw.PutData(1);
            //}
            //else
            //{
            //    graphInitValue = removeData * this.MovePoint;

            //    for (int i = 0; i < graphInitValue; i++)
            //        this.smartDraw.PutData(1);

            //    for (int i = 0; i < this.CollectionGraphDataTemp.Count - graphInitValue; i++)
            //    {
            //        inputValue = this.CollectionGraphDataTemp[i] + temp;

            //        if (inputValue > 240)
            //            inputValue = 240;
            //        else if (inputValue < 0)
            //            inputValue = 1;

            //        this.smartDraw.PutData(inputValue);
            //    }
            //}
        }

        private void buttonUp_Click(object sender, EventArgs e)
        {
            this.ParentForm.ParentForm.TransferData(CommunicationCommand.MotorUp, CommunicationID.MainBoard);
        }
        private void buttonDown_Click(object sender, EventArgs e)
        {
            this.ParentForm.ParentForm.TransferData(CommunicationCommand.MotorDown, CommunicationID.MainBoard);
        }

        private void buttonCopy_Click(object sender, EventArgs e)
        {
            this.buttonCopy.Text = this.buttonProductNo.Text;

            this.CaptureJudgmentSetItem.Filter = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Filter;
            this.CaptureJudgmentSetItem.JudgmentDelayTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime;
            this.CaptureJudgmentSetItem.DoubleDelayTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.DoubleDelayTime;
            this.CaptureJudgmentSetItem.JudgmentCount = this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentCount;
            this.CaptureJudgmentSetItem.FeedSpeed1 = this.ParentForm.ParentForm.CurrentJudgmentSetItem.FeedSpeed1;
            this.CaptureJudgmentSetItem.DynamicCorrection = this.ParentForm.ParentForm.CurrentJudgmentSetItem.DynamicCorrection;
            this.CaptureJudgmentSetItem.MovingAverage = this.ParentForm.ParentForm.CurrentJudgmentSetItem.MovingAverage;

            this.CaptureJudgmentSetItem.Sorter1Mode = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1Mode;
            this.CaptureJudgmentSetItem.Sorter1DelayTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1DelayTime;
            this.CaptureJudgmentSetItem.Sorter1RunTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1RunTime;
            this.CaptureJudgmentSetItem.Sorter2Mode = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2Mode;
            this.CaptureJudgmentSetItem.Sorter2DelayTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2DelayTime;
            this.CaptureJudgmentSetItem.Sorter2RunTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2RunTime;

            this.CaptureJudgmentSetItem.DescendDelayTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.DescendDelayTime;
            this.CaptureJudgmentSetItem.AscendDelayTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.AscendDelayTime;
            this.CaptureJudgmentSetItem.ProductLength = this.ParentForm.ParentForm.CurrentJudgmentSetItem.ProductLength;
        }
        private void buttonPaste_Click(object sender, EventArgs e)
        {
            if (this.buttonCopy.Text == "00")
                return;

            this.ParentForm.ParentForm.CurrentJudgmentSetItem.Filter = this.CaptureJudgmentSetItem.Filter;
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime = this.CaptureJudgmentSetItem.JudgmentDelayTime;
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.DoubleDelayTime = this.CaptureJudgmentSetItem.DoubleDelayTime;
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentCount = this.CaptureJudgmentSetItem.JudgmentCount;
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.FeedSpeed1 = this.CaptureJudgmentSetItem.FeedSpeed1;
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.DynamicCorrection = this.CaptureJudgmentSetItem.DynamicCorrection;
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.MovingAverage = this.CaptureJudgmentSetItem.MovingAverage;

            this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1Mode = this.CaptureJudgmentSetItem.Sorter1Mode;
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1DelayTime = this.CaptureJudgmentSetItem.Sorter1DelayTime;
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1RunTime = this.CaptureJudgmentSetItem.Sorter1RunTime;
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2Mode = this.CaptureJudgmentSetItem.Sorter2Mode;
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2DelayTime = this.CaptureJudgmentSetItem.Sorter2DelayTime;
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2RunTime = this.CaptureJudgmentSetItem.Sorter2RunTime;

            this.ParentForm.ParentForm.CurrentJudgmentSetItem.DescendDelayTime = this.CaptureJudgmentSetItem.DescendDelayTime;
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.AscendDelayTime = this.CaptureJudgmentSetItem.AscendDelayTime;
            this.ParentForm.ParentForm.CurrentJudgmentSetItem.ProductLength = this.CaptureJudgmentSetItem.ProductLength;

            this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);

            this.buttonProductNo.Text = "**";
            this.ParentForm.ParentForm.TransferProductParameter(this.ParentForm.ParentForm.SystemConfig1.ProductNumber);
        }
        private void buttonPasteAll_Click(object sender, EventArgs e)
        {
            string fullFilePath = "";
            StructJudgmentSetItem structItem;

            if (this.buttonCopy.Text == "00")
                return;

            SmartSplash splash = new SmartSplash();
            splash.CenterPosition = true;
            splash.AnimationInterval = 200;
            splash.LoadingImagePathname = "SmartLoading4";
            splash.Start();

            this.ParentForm.Enabled = false;

            fullFilePath = this.ParentForm.ParentForm.PathSDCardSystemFile2Folder + "JudgmentSetItem.int";
            this.ParentForm.ParentForm.smartFileIO.FilePathName = fullFilePath;

            this.ParentForm.ParentForm.smartFileIO.Open(this.ParentForm.ParentForm.BufferSmartUart);

            for (int i = 0; i < this.ParentForm.ParentForm.ProductCount; i++)
            {
                structItem.Filter = this.CaptureJudgmentSetItem.Filter;
                structItem.JudgmentDelayTime = this.CaptureJudgmentSetItem.JudgmentDelayTime;
                structItem.DoubleDelayTime = this.CaptureJudgmentSetItem.DoubleDelayTime;
                structItem.JudgmentCount = this.CaptureJudgmentSetItem.JudgmentCount;
                structItem.FeedSpeed1 = this.CaptureJudgmentSetItem.FeedSpeed1;
                structItem.FeedSpeed2 = this.CaptureJudgmentSetItem.FeedSpeed2;
                structItem.FeedSpeed3 = this.CaptureJudgmentSetItem.FeedSpeed3;
                structItem.DynamicCorrection = this.CaptureJudgmentSetItem.DynamicCorrection;

                structItem.Sorter1Mode = this.CaptureJudgmentSetItem.Sorter1Mode;
                structItem.Sorter1DelayTime = this.CaptureJudgmentSetItem.Sorter1DelayTime;
                structItem.Sorter1RunTime = this.CaptureJudgmentSetItem.Sorter1RunTime;
                structItem.Sorter2Mode = this.CaptureJudgmentSetItem.Sorter2Mode;
                structItem.Sorter2DelayTime = this.CaptureJudgmentSetItem.Sorter2DelayTime;
                structItem.Sorter2RunTime = this.CaptureJudgmentSetItem.Sorter2RunTime;

                structItem.ProductLength = this.CaptureJudgmentSetItem.ProductLength;
                structItem.AutoJudgment1 = this.CaptureJudgmentSetItem.AutoJudgment1;
                structItem.AutoJudgment2 = this.CaptureJudgmentSetItem.AutoJudgment2;
                structItem.AutoJudgment3 = this.CaptureJudgmentSetItem.AutoJudgment3;

                structItem.DescendDelayTime = this.CaptureJudgmentSetItem.DescendDelayTime;
                structItem.AscendDelayTime = this.CaptureJudgmentSetItem.AscendDelayTime;
                structItem.MovingAverage = this.CaptureJudgmentSetItem.MovingAverage;
                structItem.DummyInt1 = 0;
                structItem.DummyInt2 = 0;
                structItem.DummyString1 = "0";
                structItem.DummyString2 = "0";
                structItem.DummyString3 = "0";
                structItem.DummyString4 = "0";
                structItem.DummyString5 = "0";

                this.ParentForm.ParentForm.smartFileIO.WriteStructure_Begin(structItem, i);
            }
            this.ParentForm.ParentForm.smartFileIO.WriteStructure_End();
            this.ParentForm.ParentForm.smartFileIO.Close();

            this.ParentForm.ParentForm.ReLoadJudgmentSetFile();

            this.buttonProductNo.Text = "**";
            this.ParentForm.ParentForm.TransferProductParameter(this.ParentForm.ParentForm.SystemConfig1.ProductNumber);

            this.ParentForm.Enabled = true;

            splash.Finish();
        }

        private void buttonWeightReference_Click(object sender, EventArgs e)
        {
            string value = "";

            value = this.labelWeightValue.Text;
            if (this.buttonWeightReference.Text != value)
                this.buttonWeightReference.Text = value;

            //this.RefrenceADC = this.labelADCValue.Text;
        }
        #endregion       
    }
}