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_2H.DialogForms;
using ITC81DB_2H.Forms;
using ITC81DB_2H_ImageDll;
using ITC81DB_2H.Datastore;
using SmartX;

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

        private Color NormalColor;
        private Color FinishColor;

        SmartSplash CalSplash = new SmartSplash();

        // 중량 조정이 완료 되었는지 확인하는 Flag
        private bool IsCalibrationFinish;
        #endregion

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

            this.ParentForm = parent;

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

            this.labelTitleDeadLoadZero.TextHAlign = SmartLabel.TextHorAlign.Middle;
            this.labelTitleDeadLoadZero.TextVAlign = SmartLabel.TextVerAlign.Middle;
            this.buttonDeadLoadZero.Font = new Font("새굴림", 12, FontStyle.Bold);

            if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.English)
            {
                this.buttonCancel.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engCalibrationCancelDisable));
                this.buttonCancel.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engCalibrationCancelDown));
                this.buttonCancel.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engCalibrationCancelUp));
                this.buttonStart.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engCalibrationStartDisable));
                this.buttonStart.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engCalibrationStartDown));
                this.buttonStart.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engCalibrationStartUp));
                this.buttonBalance.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engCalibrationCounterweightDisable));
                this.buttonBalance.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engCalibrationCounterweightDown));
                this.buttonBalance.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engCalibrationCounterweightUp));

                this.labelTitleDecimalPoint.Text = "Decimal point";
                this.labelTitleBalanceWeight.Text = "Balance";
                this.labelTitleDeadLoadZero.Font = new Font("새굴림", 10, FontStyle.Bold);
                this.labelTitleDeadLoadZero.Text = "Dead Load Zero";
                this.buttonDeadLoadZero.Text = "Run";
                this.labelTitleHelp.Text = "Help";
                this.labelTitleWeight.Text = "Weight";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
            {
                this.buttonCancel.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnCalibrationCancelDisable));
                this.buttonCancel.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnCalibrationCancelDown));
                this.buttonCancel.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnCalibrationCancelUp));
                this.buttonStart.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnCalibrationStartDisable));
                this.buttonStart.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnCalibrationStartDown));
                this.buttonStart.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnCalibrationStartUp));

                this.labelTitleDecimalPoint.Text = "小数点";
                this.labelTitleBalanceWeight.Text = "砝码重量";
                this.labelTitleDeadLoadZero.Font = new Font("새굴림", 10, FontStyle.Bold);
                this.labelTitleDeadLoadZero.Text = "死荷重零";
                this.buttonDeadLoadZero.Text = "执行";
                this.labelTitleHelp.Text = "帮助";
                this.labelTitleWeight.Text = "重量";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
            {
                this.buttonCancel.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusCalibrationCancelDisable));
                this.buttonCancel.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusCalibrationCancelDown));
                this.buttonCancel.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusCalibrationCancelUp));
                this.buttonStart.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusCalibrationStartDisable));
                this.buttonStart.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusCalibrationStartDown));
                this.buttonStart.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusCalibrationStartUp));

                this.labelTitleDecimalPoint.Text = "Десят.точка";
                this.labelTitleBalanceWeight.Text = "Калиб.вес";
                this.labelTitleDeadLoadZero.TextHAlign = SmartLabel.TextHorAlign.Left;
                this.labelTitleDeadLoadZero.TextVAlign = SmartLabel.TextVerAlign.Top;
                this.labelTitleDeadLoadZero.Font = new Font("새굴림", 10, FontStyle.Bold);
                this.labelTitleDeadLoadZero.Text = "Полная нагрузка равна нулю";
                this.buttonDeadLoadZero.Font = new Font("새굴림", 11, FontStyle.Bold);
                this.buttonDeadLoadZero.Text = "исполнение";
                this.labelTitleHelp.Text = "Пом.";
                this.labelTitleWeight.Text = "Вес";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
            {
                this.buttonCancel.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerCalibrationCancelDisable));
                this.buttonCancel.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerCalibrationCancelDown));
                this.buttonCancel.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerCalibrationCancelUp));
                this.buttonStart.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerCalibrationStartDisable));
                this.buttonStart.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerCalibrationStartDown));
                this.buttonStart.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerCalibrationStartUp));

                this.labelTitleDecimalPoint.Text = "Dezimalpunkt";
                this.labelTitleBalanceWeight.Font = new Font("새굴림", 10, FontStyle.Bold);
                this.labelTitleBalanceWeight.Text = "Gewicht der Waage";
                this.labelTitleDeadLoadZero.Font = new Font("새굴림", 10, FontStyle.Bold);
                this.labelTitleDeadLoadZero.Text = "Totlast Null";
                this.buttonDeadLoadZero.Text = "Ausführung";
                this.labelTitleHelp.Text = "Hilfe";
                this.labelTitleWeight.Text = "Gewicht";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
            {

            }
            else
            {
                this.buttonCancel.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korCalibrationCancelDisable));
                this.buttonCancel.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korCalibrationCancelDown));
                this.buttonCancel.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korCalibrationCancelUp));
                this.buttonStart.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korCalibrationStartDisable));
                this.buttonStart.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korCalibrationStartDown));
                this.buttonStart.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korCalibrationStartUp));
                this.buttonBalance.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korCalibrationCounterweightDisable));
                this.buttonBalance.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korCalibrationCounterweightDown));
                this.buttonBalance.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korCalibrationCounterweightUp));

                this.labelTitleDecimalPoint.Text = "소수점";
                this.labelTitleBalanceWeight.Text = "분동중량";
                this.labelTitleDeadLoadZero.Font = new Font("새굴림", 13, FontStyle.Bold);
                this.labelTitleDeadLoadZero.Text = "데드로드영점";
                this.buttonDeadLoadZero.Text = "실행";
                this.labelTitleHelp.Text = "도움말";
                this.labelTitleWeight.Text = "중량";
            }
        }
        private void DefaultSetting()
        {
            this.NormalColor = Color.White;
            this.FinishColor = Color.Lime;
        }

        public void CalibrationButtonEnable(bool btStart, bool btBalance, bool btCcancel)
        {
            this.buttonStart.Enabled = btStart;
            this.buttonBalance.Enabled = btBalance;
            this.buttonCancel.Enabled = btCcancel;
        }
        private void CalibrationPictureBoxVisible(bool step1, bool step2, bool step3)
        {
            if (step1 == true)
            {
                this.pictureBoxStep1.Visible = true;
                this.pictureBoxStep2.Visible = false;
                this.pictureBoxStep3.Visible = false;
            }
            else if (step2 == true)
            {
                this.pictureBoxStep1.Visible = false;
                this.pictureBoxStep2.Visible = true;
                this.pictureBoxStep3.Visible = false;
            }
            else if(step3 == true)
            {
                this.pictureBoxStep1.Visible = false;
                this.pictureBoxStep2.Visible = false;
                this.pictureBoxStep3.Visible = true;
            }
            else
            {
                this.pictureBoxStep1.Visible = true;
                this.pictureBoxStep2.Visible = false;
                this.pictureBoxStep3.Visible = false;
            }
        }

        public void SplashStop()
        {
            this.CalSplash.Finish();
            this.Enabled = true;
        }

        private void UpdateControlsDisplay()
        {
            this.listBoxHelp.ClearAll();
            if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.English)
            {
                this.listBoxHelp.AddItem("Push the start button.");
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
            {
                this.listBoxHelp.AddItem("需要重量调节，请点击开始按钮");
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
            {
                this.listBoxHelp.AddItem("Нажмите кнопку [Старт],");
                this.listBoxHelp.AddItem("чтобы начать калибровку");
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
            {
                this.listBoxHelp.AddItem("Drücken Sie die Taste [Start],");
                this.listBoxHelp.AddItem("um die Kalibrierung zu starten");
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
            {

            }
            else
            {
                this.listBoxHelp.AddItem("중량조정을 하시려면");
                this.listBoxHelp.AddItem("시작 버튼을 누르세요");
            }

            this.CalibrationButtonEnable(true, false, false);
            this.CalibrationPictureBoxVisible(true, false, false);

            this.radioButtonDecimalPlaces1.Click -= new EventHandler(this.radioButtonDecimalPlaces_Click);
            this.radioButtonDecimalPlaces2.Click -= new EventHandler(this.radioButtonDecimalPlaces_Click);

            if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 1)
            {
                if (this.radioButtonDecimalPlaces1.Checked != true)
                    this.radioButtonDecimalPlaces1.Checked = true;
            }
            else
            {
                if (this.radioButtonDecimalPlaces2.Checked != true)
                    this.radioButtonDecimalPlaces2.Checked = true;
            }

            this.radioButtonDecimalPlaces1.Click += new EventHandler(this.radioButtonDecimalPlaces_Click);
            this.radioButtonDecimalPlaces2.Click += new EventHandler(this.radioButtonDecimalPlaces_Click);

            //if (this.labelWeightUnit.Text != this.ParentForm.ParentForm.SystemConfig1.Unit)
            //    this.labelWeightUnit.Text = this.ParentForm.ParentForm.SystemConfig1.Unit;
        }
        public void UpdateWeightDisplay(Define.E_EquipmentStatus status, WeightData data)
        {
            string searchText;
            bool exists;

            switch (data.Status)
            {
                case Define.E_WeightStatus.CalNomal:
                    this.labelWeight.Text = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    this.labelWeight.ForeColor = this.NormalColor;
                    if (this.buttonStart.Enabled == false)
                    {
                        this.CalibrationButtonEnable(true, false, false);

                        this.listBoxHelp.ClearAll();
                        if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.English)
                        {
                            this.listBoxHelp.AddItem("Push the start button.");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
                        {
                            this.listBoxHelp.AddItem("需要重量调节，请点击开始按钮");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
                        {
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
                        {
                            this.listBoxHelp.AddItem("Нажмите кнопку [Старт],");
                            this.listBoxHelp.AddItem("чтобы начать калибровку");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
                        {
                            this.listBoxHelp.AddItem("Drücken Sie die Taste [Start],");
                            this.listBoxHelp.AddItem("um die Kalibrierung zu starten");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
                        {

                        }
                        else
                        {
                            this.listBoxHelp.AddItem("중량조정을 하시려면");
                            this.listBoxHelp.AddItem("시작 버튼을 누르세요");
                        }
                    }
                    this.CalibrationPictureBoxVisible(true, false, false);
                    break;
                case Define.E_WeightStatus.CalWait:
                    // 분동중량 표시
                    this.IsCalibrationFinish = false;
                    this.labelWeight.Text = string.Format("-{0}", this.labelBalanceWeight.Text);
                    this.labelWeight.ForeColor = this.NormalColor;
                    break;
                case Define.E_WeightStatus.CalStandby:
                    //// 분동중량 표시
                    //this.IsCalibrationFinish = false;
                    //if(this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 1)
                    //    this.labelWeight.Text = "-500.0";
                    //else
                    //    this.labelWeight.Text = "-500.00";
                    //this.labelWeight.ForeColor = this.NormalColor;
                    this.listBoxHelp.ClearAll();
                    if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.English)
                    {
                        this.listBoxHelp.AddItem("Put the balance weight.");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
                    {
                        this.listBoxHelp.AddItem("请放上砝码");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
                    {
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
                    {
                        this.listBoxHelp.AddItem("Утаноите весы");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
                    {
                        this.listBoxHelp.AddItem("Gewicht auf den Förderer legen");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
                    {

                    }
                    else
                    {
                        this.listBoxHelp.AddItem("분동을 올려주세요");
                    }
                    this.CalibrationButtonEnable(false, true, true);
                    this.CalibrationPictureBoxVisible(false, true, false);
                    break;
                case Define.E_WeightStatus.CalBalans:
                    // 분동중량표시
                    this.labelWeight.Text = string.Format("-{0}", this.labelBalanceWeight.Text);
                    this.CalibrationButtonEnable(false, false, true);
                    this.CalibrationPictureBoxVisible(false, false, true);
                    break;
                case Define.E_WeightStatus.CalFinish:
                    this.listBoxHelp.ClearAll();
                    if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.English)
                    {
                        this.listBoxHelp.AddItem("Finish the calibration");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
                    {
                        this.listBoxHelp.AddItem("重量调节已完成");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
                    {
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
                    {
                        this.listBoxHelp.AddItem("Завершение калибровки");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
                    {
                        this.listBoxHelp.AddItem("Die Kalibrierung ist abgeschlossen.");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
                    {

                    }
                    else
                    {
                        this.listBoxHelp.AddItem("중량조정이 완료 되었습니다");
                    }
                    this.CalibrationButtonEnable(true, false, false);
                    this.CalibrationPictureBoxVisible(true, false, false);
                    this.labelWeight.Text = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    this.labelWeight.ForeColor = this.FinishColor;
                    if (this.IsCalibrationFinish == false)
                    {
                        this.IsCalibrationFinish = true;
                        this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.MainBoard, CommunicationAddress.ParameterRead3901, "");
                        this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingOperation.Calibration, "Calibration Success");
                    }
                    break;
                case Define.E_WeightStatus.CalError:
                    #region CalError
                    this.listBoxHelp.ClearAll();
                    if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.English)
                    {
                        this.listBoxHelp.AddItem("Calibration error");
                        this.listBoxHelp.AddItem("Please try again.");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
                    {
                        this.listBoxHelp.AddItem("校准误差");
                        this.listBoxHelp.AddItem("请重试");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
                    {
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
                    {
                        this.listBoxHelp.AddItem("Произошла ошибка");
                        this.listBoxHelp.AddItem("Попробуйте еще раз");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
                    {
                        this.listBoxHelp.AddItem("Fehler!");
                        this.listBoxHelp.AddItem("Bitte noch einmal versuchen");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
                    {

                    }
                    else
                    {
                        this.listBoxHelp.AddItem("중량조정 에러");
                        this.listBoxHelp.AddItem("다시 시도해 주세요.");
                    }
                    this.CalibrationButtonEnable(true, false, false);
                    this.CalibrationPictureBoxVisible(true, false, false);
                    this.labelWeight.Text = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    this.labelWeight.ForeColor = this.NormalColor;

                    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingOperation.Calibration, "Calibration Fail");
                    #endregion
                    break;
                case Define.E_WeightStatus.CalError_E1:
                    #region CalError_E1
                    if (this.buttonStart.Enabled == true)
                        this.CalibrationButtonEnable(false, false, false);

                    searchText = "E1";
                    exists = this.listBoxHelp.Items.Cast<string>().Any(item => item.Contains(searchText));
                    if (exists == true)
                        return;

                    this.listBoxHelp.ClearAll();
                    if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.English)
                    {
                        this.listBoxHelp.AddItem("Error E1 : Overload");
                        this.listBoxHelp.AddItem("Please check the load cell.");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
                    {
                        this.listBoxHelp.AddItem("校准误差 E1 : 超载");
                        this.listBoxHelp.AddItem("请检查称重传感器。");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
                    {
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
                    {
                        this.listBoxHelp.AddItem("Произошла ошибка E1 : Перегрузка");
                        this.listBoxHelp.AddItem("Пожалуйста, проверьте датчик нагрузки.");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
                    {
                        this.listBoxHelp.AddItem("Fehler! E1 : Überlast");
                        this.listBoxHelp.AddItem("Bitte überprüfen Sie die Wägezelle.");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
                    {

                    }
                    else
                    {
                        this.listBoxHelp.AddItem("에러_E1 : Overload");
                        this.listBoxHelp.AddItem("로드셀을 확인해 주십시오");
                    }
                    this.CalibrationButtonEnable(false, false, false);
                    this.CalibrationPictureBoxVisible(true, false, false);
                    this.labelWeight.Text = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    this.labelWeight.ForeColor = this.NormalColor;

                    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingOperation.Calibration, "Calibration Fail E1");
                    #endregion
                    break;
                case Define.E_WeightStatus.CalError_E2:
                    #region CalError_E2
                    if (this.buttonStart.Enabled == true)
                        this.CalibrationButtonEnable(false, false, false);

                    searchText = "E2";
                    exists = this.listBoxHelp.Items.Cast<string>().Any(item => item.Contains(searchText));
                    if (exists == true)
                        return;

                    this.listBoxHelp.ClearAll();
                    if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.English)
                    {
                        this.listBoxHelp.AddItem("Error E2 : Underload");
                        this.listBoxHelp.AddItem("Please check the load cell.");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
                    {
                        this.listBoxHelp.AddItem("校准误差 E2 : 欠载");
                        this.listBoxHelp.AddItem("请检查称重传感器。");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
                    {
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
                    {
                        this.listBoxHelp.AddItem("Произошла ошибка E2 : Недогрузка");
                        this.listBoxHelp.AddItem("Пожалуйста, проверьте датчик нагрузки.");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
                    {
                        this.listBoxHelp.AddItem("Fehler! E2 : Unterlast");
                        this.listBoxHelp.AddItem("Bitte überprüfen Sie die Wägezelle.");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
                    {

                    }
                    else
                    {
                        this.listBoxHelp.AddItem("에러_E2 : Underload");
                        this.listBoxHelp.AddItem("로드셀을 확인해 주십시오");
                    }
                    this.CalibrationButtonEnable(false, false, false);
                    this.CalibrationPictureBoxVisible(true, false, false);
                    this.labelWeight.Text = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    this.labelWeight.ForeColor = this.NormalColor;

                    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingOperation.Calibration, "Calibration Fail E2");
                    #endregion
                    break;
                case Define.E_WeightStatus.CalError_E3:
                    #region CalError_E3
                    if (this.buttonStart.Enabled == true)
                        this.CalibrationButtonEnable(false, false, false);

                    searchText = "E3";
                    exists = this.listBoxHelp.Items.Cast<string>().Any(item => item.Contains(searchText));
                    if (exists == true)
                        return;

                    this.listBoxHelp.ClearAll();
                    if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.English)
                    {
                        this.listBoxHelp.AddItem("Error E3 : Please wait a moment");
                        this.listBoxHelp.AddItem("until the error  is cleared and");
                        this.listBoxHelp.AddItem("the [Start] button is activated.");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
                    {
                        this.listBoxHelp.AddItem("校准误差 E3");
                        this.listBoxHelp.AddItem("请稍等片刻，直到错误清除且[开始]按钮激活。");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
                    {
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
                    {
                        this.listBoxHelp.AddItem("Произошла ошибка E3 : Пожалуйста, ");
                        this.listBoxHelp.AddItem("подождите немного, пока ошибка не будет");
                        this.listBoxHelp.AddItem("устранена и кнопка [Старт] не станет активной.");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
                    {
                        this.listBoxHelp.AddItem("Fehler! E3 : Bitte warten Sie einen Moment,");
                        this.listBoxHelp.AddItem("bis der Fehler behoben ist und die Schaltfläche");
                        this.listBoxHelp.AddItem("[Start] aktiviert wird.");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
                    {

                    }
                    else
                    {
                        this.listBoxHelp.AddItem("중량조정 에러 E3");
                        this.listBoxHelp.AddItem("에러가 해제되고 [Start]버튼이 활성화 될때까지");
                        this.listBoxHelp.AddItem("잠시 기다려 주십시오");
                    }
                    this.CalibrationButtonEnable(false, false, false);
                    this.CalibrationPictureBoxVisible(true, false, false);
                    this.labelWeight.Text = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    this.labelWeight.ForeColor = this.NormalColor;

                    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingOperation.Calibration, "Calibration Fail E3");
                    #endregion
                    break;
                case Define.E_WeightStatus.CalError_E5:
                    #region CalError_E5
                    if (this.buttonStart.Enabled == true)
                        this.CalibrationButtonEnable(false, false, false);

                    searchText = "E5";
                    exists = this.listBoxHelp.Items.Cast<string>().Any(item => item.Contains(searchText));
                    if (exists == true)
                        return;

                    this.listBoxHelp.ClearAll();
                    if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.English)
                    {
                        this.listBoxHelp.AddItem("Error E5 : Please wait a moment");
                        this.listBoxHelp.AddItem("until the error  is cleared and");
                        this.listBoxHelp.AddItem("the [Start] button is activated.");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
                    {
                        this.listBoxHelp.AddItem("校准误差 E5");
                        this.listBoxHelp.AddItem("请稍等片刻，直到错误清除且[开始]按钮激活。");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
                    {
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
                    {
                        this.listBoxHelp.AddItem("Произошла ошибка E5 : Пожалуйста, ");
                        this.listBoxHelp.AddItem("подождите немного, пока ошибка не будет");
                        this.listBoxHelp.AddItem("устранена и кнопка [Старт] не станет активной.");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
                    {
                        this.listBoxHelp.AddItem("Fehler! E5 : Bitte warten Sie einen Moment,");
                        this.listBoxHelp.AddItem("bis der Fehler behoben ist und die Schaltfläche");
                        this.listBoxHelp.AddItem("[Start] aktiviert wird.");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
                    {

                    }
                    else
                    {
                        this.listBoxHelp.AddItem("중량조정 에러 E5");
                        this.listBoxHelp.AddItem("에러가 해제되고 [Start]버튼이 활성화 될때까지");
                        this.listBoxHelp.AddItem("잠시 기다려 주십시오");
                    }
                    this.CalibrationButtonEnable(false, false, false);
                    this.CalibrationPictureBoxVisible(true, false, false);
                    this.labelWeight.Text = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    this.labelWeight.ForeColor = this.NormalColor;

                    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingOperation.Calibration, "Calibration Fail E5");
                    #endregion
                    break;
                default:
                    break;
            }
        }
        public void UpdateCalibrationItemDisplay(Define.E_EquipmentStatus status, CalibrationItem item)
        {
            string value = "";

            value = Helper.StringToDecimalPlaces(item.BalanceWeight, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            if (this.labelBalanceWeight.Text != value)
                this.labelBalanceWeight.Text = value;
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = Define.E_DisplayStore.SystemCalibration;
            this.ParentForm.ParentForm.SetDisplayMode(Define.E_DisplayMode.Calibration);
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);

            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.MainBoard, CommunicationAddress.BalanceWeight, "");
            this.UpdateControlsDisplay();

            this.SplashStop();

            this.Refresh();
        }
        #endregion

        #region Event Handler
        private void labelBalanceWeight_Click(object sender, EventArgs e)
        {
            string before = "", after = "";
            string value = "";

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 0.0 || myKeyPad.doubleValue > 750.0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language);
                    myMsg.Location = new Point(167, 207);
                    myMsg.ShowDialog();
                }
                else
                {
                    after = this.labelBalanceWeight.Text = myKeyPad.StringValue;

                    value = Helper.StringZeroFillDigits7(this.labelBalanceWeight.Text.Replace(".", ""));
                    this.ParentForm.ParentForm.CurrentCalibrationItem.BalanceWeight = value;
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.BalanceWeight, value);

                    if (before != after)
                        this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.CalBalanceWeight, "", before, after);
                }
            }
        }

        private void buttonDeadLoadZero_Click(object sender, EventArgs e)
        {
            // 영점 옵셋 조정을 실행하시겠습니까?
            DialogFormYesNo msg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig1.Language, 30);
            if (msg.ShowDialog() == DialogResult.Yes)
            {
                this.CalSplash.CenterPosition = true;
                this.CalSplash.AnimationInterval = 200;
                this.CalSplash.LoadingImagePathname = "SmartLoading4";
                this.CalSplash.Start();

                this.Enabled = false;
                this.timerSplashFinish.Enabled = true;

                this.CalibrationButtonEnable(false, false, false);
                this.ParentForm.ParentForm.TransferData(CommunicationCommand.Zero, CommunicationID.SubBoard1); 
            }
        }
        private void buttonStart_Click(object sender, EventArgs e)
        {
            this.listBoxHelp.ClearAll();
            if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.English)
            {
                this.listBoxHelp.AddItem("Calibration start");
                this.listBoxHelp.AddItem("Wait a minute.");
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
            {
                this.listBoxHelp.AddItem("校准开始按钮");
                this.listBoxHelp.AddItem("请等待");
            }      
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
            {
                this.listBoxHelp.AddItem("Начало калибровки");
                this.listBoxHelp.AddItem("Подождите");
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
            {
                this.listBoxHelp.AddItem("Kalibrierungsstart");
                this.listBoxHelp.AddItem("bitte warten");
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
            {

            }
            else
            {
                this.listBoxHelp.AddItem("중량조정을 시작합니다");
                this.listBoxHelp.AddItem("잠시만 기다리세요");
            }

            this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationStart, CommunicationID.SubBoard1);

            this.CalibrationButtonEnable(false, false, false);
        }
        private void buttonBalance_Click(object sender, EventArgs e)
        {
            this.listBoxHelp.ClearAll();
            if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.English)
            {
                this.listBoxHelp.AddItem("Caculating...");
                this.listBoxHelp.AddItem("Wait a minute.");
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
            {
                this.listBoxHelp.AddItem("测量砝码中");
                this.listBoxHelp.AddItem("请等待");
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
            {

            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
            {
                this.listBoxHelp.AddItem("Имерение…");
                this.listBoxHelp.AddItem("Ожидайте");
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
            {
                this.listBoxHelp.AddItem("Rechnen...");
                this.listBoxHelp.AddItem("bitte warten.");
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
            {

            }
            else
            {
                this.listBoxHelp.AddItem("분동 계산중 입니다");
                this.listBoxHelp.AddItem("잠시만 기다리세요");
            }

            this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationBalance, CommunicationID.SubBoard1);
            this.CalibrationButtonEnable(false, false, true);
        }
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.listBoxHelp.ClearAll();
            if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.English)
            {
                this.listBoxHelp.AddItem("Cancel the calibration");
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
            {
                this.listBoxHelp.AddItem("取消重量调节");
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
            {
                this.listBoxHelp.AddItem("Отменить калибровку");
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
            {
                this.listBoxHelp.AddItem("Brechen Sie die Kalibrierung ab");
            }
            else
            {
                this.listBoxHelp.AddItem("중량조정을 취소합니다");
            }

            this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationCancel, CommunicationID.SubBoard1);
            this.CalibrationButtonEnable(true, false, false);
        }

        private void radioButtonDecimalPlaces_Click(object sender, EventArgs e)
        {
            SmartX.SmartRadioButton radioButton = sender as SmartX.SmartRadioButton;

            if ((radioButton.Text == "0.0" && this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 1)
                || (radioButton.Text == "0.00" && this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 2))
                return;

            string value = "";
            string before = "", after = "";
            int length = (int.Parse(this.ParentForm.ParentForm.CurrentCalibrationItem.BalanceWeight)).ToString().Length;
            string balanceWeight = (int.Parse(this.ParentForm.ParentForm.CurrentCalibrationItem.BalanceWeight)).ToString();

            //this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.MainBoard, CommunicationAddress.ParameterRead3901, "");
            this.ParentForm.ParentForm.OpenSmartFileIO();

            before = this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces.ToString();

            if (this.ParentForm.ParentForm.SystemConfig2.IsUsingRandomMode == true)
            {
                int currentDecimalPlaces = this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces;
                bool radioButtonCompare = false;

                if (this.radioButtonDecimalPlaces1.Checked == true)
                {
                    if (currentDecimalPlaces != 1)
                        radioButtonCompare = true;
                }
                else if (this.radioButtonDecimalPlaces2.Checked == true)
                {
                    if (currentDecimalPlaces != 2)
                        radioButtonCompare = true;
                }

                if (radioButtonCompare == true)
                {
                    DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig1.Language, 24);
                    if (myDlg.ShowDialog() == DialogResult.Yes)
                    {
                        if (this.radioButtonDecimalPlaces1.Checked == true)
                        {
                            this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces = 1;

                            balanceWeight = balanceWeight.Remove(length - 1, 1);
                        }
                        else if (this.radioButtonDecimalPlaces2.Checked == true)
                        {
                            this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces = 2;

                            balanceWeight = balanceWeight.Insert(length, "0");
                        }

                        value = Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces.ToString());
                        this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.DecimalPlaces, value);
                        value = Helper.StringZeroFillDigits7(balanceWeight);
                        this.ParentForm.ParentForm.CurrentCalibrationItem.BalanceWeight = value;
                        this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.BalanceWeight, value);
                        this.ParentForm.ParentForm.SaveSystemConfigurationFile1(this.ParentForm.ParentForm.SystemConfig1);
                        this.UpdateCalibrationItemDisplay(this.ParentForm.ParentForm.CurrentSystemStatus.Equipment, this.ParentForm.ParentForm.CurrentCalibrationItem);
                    }
                }
            }
            else
            {
                if (this.radioButtonDecimalPlaces1.Checked == true)
                {
                    this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces = 1;

                    balanceWeight = balanceWeight.Remove(length - 1, 1);
                }
                else if (this.radioButtonDecimalPlaces2.Checked == true)
                {
                    this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces = 2;

                    balanceWeight = balanceWeight.Insert(length, "0");
                }

                value = Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces.ToString());
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.DecimalPlaces, value);
                value = Helper.StringZeroFillDigits7(balanceWeight);
                this.ParentForm.ParentForm.CurrentCalibrationItem.BalanceWeight = value;
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.BalanceWeight, value);
                
                this.ParentForm.CenterSystemAutoZero.DetermineRange();

                this.ParentForm.ParentForm.SaveSystemConfigurationFile1(this.ParentForm.ParentForm.SystemConfig1);
                this.UpdateCalibrationItemDisplay(this.ParentForm.ParentForm.CurrentSystemStatus.Equipment, this.ParentForm.ParentForm.CurrentCalibrationItem);
            }

            this.ParentForm.ParentForm.CloseSmartFileIO();
            this.ParentForm.ParentForm.ReLoadProductFile();

            after = this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces.ToString();

            if (before != after)
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.CalDecimalPoint, "", before, after);
        }

        private void timerSplashFinish_Tick(object sender, EventArgs e)
        {
            this.timerSplashFinish.Enabled = false;
            this.SplashStop();
        }
        #endregion
    }
}
