using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using SmartX;
using ITC81DB_2.DialogForms;
using ITC81DB_2.Forms;
using ITC81DB_2_ImageDll;

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

        private Color NormalColor;
        private Color FinishColor;

        // 중량 조정이 완료 되었는지 확인하는 Flag
        private bool FlagCalibrationFinish1;
        private bool FlagCalibrationFinish2;
        #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();

            if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.English)
            {
                this.labelTitleWeight1.Text = this.labelTitleWeight2.Text = "Weight";
                this.labelTitleMaxWeight.Font = new Font("새굴림", 9, FontStyle.Bold);
                this.labelTitleMaxWeight.Text = "Max weight";
                this.labelTitleBalanceWeight.Font = new Font("새굴림", 9, FontStyle.Bold);
                this.labelTitleBalanceWeight.Text = "Balance weight";
                this.labelTitleBalanceWeight.Font = new Font("새굴림", 9, FontStyle.Bold);
                this.labelTitleDecimalPoint.Text = "Decimal point";
                this.labelTitleDigit.Text = "Digit";
                this.labelTitleUnit.Text = "Unit";
                this.labelTitleHelp.Text = "Help";

                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.buttonBalance.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engCalibrationBalanceDisable));
                this.buttonBalance.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engCalibrationBalanceDown));
                this.buttonBalance.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engCalibrationBalanceUp));
                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.buttonZero.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engCalibrationZeroDisable));
                this.buttonZero.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engCalibrationZeroDown));
                this.buttonZero.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engCalibrationZeroUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Chinese)
            {
                this.labelTitleWeight1.Text = this.labelTitleWeight2.Text = "重量";
                this.labelTitleMaxWeight.Font = new Font("새굴림", 13, FontStyle.Bold);
                this.labelTitleMaxWeight.Text = "最大重量";
                this.labelTitleBalanceWeight.Font = new Font("새굴림", 13, FontStyle.Bold);
                this.labelTitleBalanceWeight.Text = "砝码重量";
                this.labelTitleDecimalPoint.Font = new Font("새굴림", 13, FontStyle.Bold);
                this.labelTitleDecimalPoint.Text = "小数点";
                this.labelTitleDigit.Text = "数字";
                this.labelTitleUnit.Text = "单位";
                this.labelTitleHelp.Text = "帮助";

                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.buttonBalance.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnCalibrationBalanceDisable));
                this.buttonBalance.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnCalibrationBalanceDown));
                this.buttonBalance.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnCalibrationBalanceUp));
                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.buttonZero.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnCalibrationZeroDisable));
                this.buttonZero.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnCalibrationZeroDown));
                this.buttonZero.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnCalibrationZeroUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
            {
                this.labelTitleWeight1.Text = this.labelTitleWeight2.Text = "Peso";
                this.labelTitleMaxWeight.Font = new Font("새굴림", 9, FontStyle.Bold);
                this.labelTitleMaxWeight.Text = "Peso máximo";
                this.labelTitleBalanceWeight.Font = new Font("새굴림", 9, FontStyle.Bold);
                this.labelTitleBalanceWeight.Text = "Contrapeso";
                this.labelTitleDecimalPoint.Font = new Font("새굴림", 9, FontStyle.Bold);
                this.labelTitleDecimalPoint.Text = "Punto decimal";
                this.labelTitleDigit.Text = "Dígito";
                this.labelTitleUnit.Text = "Unidad";
                this.labelTitleHelp.Text = "Ayudar";

                this.buttonCancel.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnCalibrationCancelDisable));
                this.buttonCancel.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnCalibrationCancelDown));
                this.buttonCancel.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnCalibrationCancelUp));
                this.buttonBalance.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnCalibrationBalanceDisable));
                this.buttonBalance.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnCalibrationBalanceDown));
                this.buttonBalance.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnCalibrationBalanceUp));
                this.buttonStart.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnCalibrationStartDisable));
                this.buttonStart.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnCalibrationStartDown));
                this.buttonStart.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnCalibrationStartUp));
                this.buttonZero.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnCalibrationZeroDisable));
                this.buttonZero.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnCalibrationZeroDown));
                this.buttonZero.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnCalibrationZeroUp));
            }
            else
            {
                this.labelTitleWeight1.Text = this.labelTitleWeight2.Text = "중량";
                this.labelTitleMaxWeight.Font = new Font("새굴림", 13, FontStyle.Bold);
                this.labelTitleMaxWeight.Text = "최대중량";
                this.labelTitleBalanceWeight.Font = new Font("새굴림", 13, FontStyle.Bold);
                this.labelTitleBalanceWeight.Text = "분동중량";
                this.labelTitleDecimalPoint.Font = new Font("새굴림", 13, FontStyle.Bold);
                this.labelTitleDecimalPoint.Text = "소수점";
                this.labelTitleDigit.Text = "한눈의값";
                this.labelTitleUnit.Text = "단위";
                this.labelTitleHelp.Text = "도움말";

                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.buttonBalance.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korCalibrationBalanceDisable));
                this.buttonBalance.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korCalibrationBalanceDown));
                this.buttonBalance.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korCalibrationBalanceUp));
                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.buttonZero.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korCalibrationZeroDisable));
                this.buttonZero.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korCalibrationZeroDown));
                this.buttonZero.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korCalibrationZeroUp));
            }

            this.CalibrationButtonEnable(true, false, false);
        }
        private void DefaultSetting()
        {
            this.NormalColor = Color.White;
            this.FinishColor = Color.Lime;

            this.FlagCalibrationFinish1 = false;
            this.FlagCalibrationFinish2 = false;

            this.buttonLane1.ButtonDown();
            this.buttonLane2.ButtonUp();
        }

        private void CalibrationButtonEnable(bool btStart, bool btBalance, bool btCcancel)
        {
            if (this.buttonStart.Enabled != btStart)
                this.buttonStart.Enabled = btStart;
            if (this.buttonBalance.Enabled != btBalance)
                this.buttonBalance.Enabled = btBalance;
            if (this.buttonCancel.Enabled != btCcancel)
                this.buttonCancel.Enabled = btCcancel;
        }

        private void UpdateUnitDisplay()
        {
            this.labelMaxWeightUnit.Text = this.ParentForm.ParentForm.SystemConfig.Unit;
            this.labelBalanceWeightUnit.Text = this.ParentForm.ParentForm.SystemConfig.Unit;
            this.labelWeightUnit1.Text = this.ParentForm.ParentForm.SystemConfig.Unit;
            this.labelWeightUnit2.Text = this.ParentForm.ParentForm.SystemConfig.Unit;
        }
        private void UpdateLabelWeight1(WeightData data)
        {
            switch (data.Status)
            {
                case Define.E_WeightStatus.CalNomal:
                    this.labelWeight1.Text = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    this.labelWeight1.ForeColor = this.NormalColor;
                    this.labelADC1.Text = data.ADCValue;
                    break;
                case Define.E_WeightStatus.CalStandby:
                    if (this.buttonLane1.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                    {
                        // 분동중량 표시
                        this.labelWeight1.Text = string.Format("-{0}", this.labelBalanceWeight.Text);
                        this.labelWeight1.ForeColor = this.NormalColor;
                        this.labelADC1.Text = data.ADCValue;
                        this.listBoxHelp.Items.Clear();
                        if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.English)
                        {
                            this.listBoxHelp.Items.Add("Put the balance weight.");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Chinese)
                        {
                            this.listBoxHelp.Items.Add("请放上砝码");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
                        {
                            this.listBoxHelp.Items.Add("Ponga el peso de la balanza.");
                        }
                        else
                        {
                            this.listBoxHelp.Items.Add("분동을 올려주세요");
                        }
                        this.CalibrationButtonEnable(false, true, true);
                    }
                    break;
                case Define.E_WeightStatus.CalBalans:
                    if (this.buttonLane1.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                    {
                        // 분동중량표시
                        this.labelWeight1.Text = string.Format("-{0}", this.labelBalanceWeight.Text);
                        this.labelWeight1.ForeColor = this.NormalColor;
                        this.labelADC1.Text = data.ADCValue;
                        this.CalibrationButtonEnable(false, false, true);
                    }
                    break;
                case Define.E_WeightStatus.CalFinish:
                    if (this.buttonLane1.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                    {
                        this.listBoxHelp.Items.Clear();
                        if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.English)
                        {
                            this.listBoxHelp.Items.Add("Finish the calibration");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Chinese)
                        {
                            this.listBoxHelp.Items.Add("重量调节已完成");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Czech)
                        {
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
                        {
                            this.listBoxHelp.Items.Add("Calibración terminada.");
                        }
                        else
                        {
                            this.listBoxHelp.Items.Add("중량조정이 완료 되었습니다");
                        }

                        this.CalibrationButtonEnable(true, false, false);
                        this.buttonLane1.Enabled = true;
                        this.buttonLane2.Enabled = true;
                    }

                    this.labelWeight1.Text = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    this.labelWeight1.ForeColor = this.FinishColor;
                    this.labelADC1.Text = data.ADCValue;
                    if (this.FlagCalibrationFinish1 == false)
                    {
                        this.FlagCalibrationFinish1 = true;
                        this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.MainBoard, CommunicationAddress._3601_ConstantRead, "");
                    }
                    break;
                case Define.E_WeightStatus.CalError:
                    this.listBoxHelp.Items.Clear();
                    if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.English)
                    {
                        this.listBoxHelp.Items.Add("Calibration error");
                        this.listBoxHelp.Items.Add("Please try again.");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Chinese)
                    {
                        this.listBoxHelp.Items.Add("错误发生，重试");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Czech)
                    {
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
                    {
                        this.listBoxHelp.Items.Add("Error de calibración.");
                        this.listBoxHelp.Items.Add("Inténtalo de nuevo.");
                    }
                    else
                    {
                        this.listBoxHelp.Items.Add("중량조정 에러");
                        this.listBoxHelp.Items.Add("다시 시도해 주세요.");
                    }
                    this.CalibrationButtonEnable(true, false, false);
                    this.labelWeight1.Text = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    this.labelWeight1.ForeColor = this.NormalColor;
                    this.labelADC1.Text = data.ADCValue;
                    break;
                default:
                    break;
            }
        }
        private void UpdateLabelWeight2(WeightData data)
        {
            switch (data.Status)
            {
                case Define.E_WeightStatus.CalNomal:
                    this.labelWeight2.Text = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    this.labelWeight2.ForeColor = this.NormalColor;
                    this.labelADC2.Text = data.ADCValue;
                    break;
                case Define.E_WeightStatus.CalStandby:
                    if (this.buttonLane2.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                    {
                        // 분동중량 표시
                        this.labelWeight2.Text = string.Format("-{0}", this.labelBalanceWeight.Text);
                        this.labelWeight2.ForeColor = this.NormalColor;
                        this.labelADC2.Text = data.ADCValue;
                        this.listBoxHelp.Items.Clear();
                        if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.English)
                        {
                            this.listBoxHelp.Items.Add("Put the balance weight.");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Chinese)
                        {
                            this.listBoxHelp.Items.Add("请放上砝码");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Czech)
                        {
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
                        {
                            this.listBoxHelp.Items.Add("Ponga el peso de la balanza.");
                        }
                        else
                        {
                            this.listBoxHelp.Items.Add("분동을 올려주세요");
                        }
                        this.CalibrationButtonEnable(false, true, true);
                    }
                    break;
                case Define.E_WeightStatus.CalBalans:
                    if (this.buttonLane2.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                    {
                        // 분동중량표시
                        this.labelWeight2.Text = string.Format("-{0}", this.labelBalanceWeight.Text);
                        this.labelWeight2.ForeColor = this.NormalColor;
                        this.labelADC2.Text = data.ADCValue;
                        this.CalibrationButtonEnable(false, false, true);
                    }
                    break;
                case Define.E_WeightStatus.CalFinish:
                    if (this.buttonLane2.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                    {
                        this.listBoxHelp.Items.Clear();
                        if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.English)
                        {
                            this.listBoxHelp.Items.Add("Finish the calibration");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Chinese)
                        {
                            this.listBoxHelp.Items.Add("重量调节已完成");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Czech)
                        {
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
                        {
                            this.listBoxHelp.Items.Add("Calibración terminada.");
                        }
                        else
                        {
                            this.listBoxHelp.Items.Add("중량조정이 완료 되었습니다");
                        }

                        this.CalibrationButtonEnable(true, false, false);
                        this.buttonLane1.Enabled = true;
                        this.buttonLane2.Enabled = true;
                    }

                    this.labelWeight2.Text = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    this.labelWeight2.ForeColor = this.FinishColor;
                    this.labelADC2.Text = data.ADCValue;
                    if (this.FlagCalibrationFinish2 == false)
                    {
                        this.FlagCalibrationFinish2 = true;
                        this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.MainBoard, CommunicationAddress._3601_ConstantRead, "");
                    }
                    break;
                case Define.E_WeightStatus.CalError:
                    this.listBoxHelp.Items.Clear();
                    if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.English)
                    {
                        this.listBoxHelp.Items.Add("Calibration error");
                        this.listBoxHelp.Items.Add("Please try again.");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Chinese)
                    {
                        this.listBoxHelp.Items.Add("错误发生，重试");
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Czech)
                    {
                    }
                    else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
                    {
                        this.listBoxHelp.Items.Add("Error de calibración.");
                        this.listBoxHelp.Items.Add("Inténtalo de nuevo.");
                    }
                    else
                    {
                        this.listBoxHelp.Items.Add("중량조정 에러");
                        this.listBoxHelp.Items.Add("다시 시도해 주세요.");
                    }
                    this.CalibrationButtonEnable(true, false, false);
                    this.labelWeight1.Text = Helper.DoubleToString(data.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    this.labelWeight1.ForeColor = this.NormalColor;
                    this.labelADC1.Text = data.ADCValue;
                    break;
                default:
                    break;
            }
        }
        private void UpdateControlsDisplay()
        {
            this.listBoxHelp.Items.Clear();
            if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.English)
            {
                this.listBoxHelp.Items.Add("Push the start button.");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Chinese)
            {
                this.listBoxHelp.Items.Add("按[开始]键");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
            {
                this.listBoxHelp.Items.Add("Presione el botón [Inicio]");
                this.listBoxHelp.Items.Add("para iniciar la calibración");
            }
            else
            {
                this.listBoxHelp.Items.Add("중량조정을 하시려면");
                this.listBoxHelp.Items.Add("시작 버튼을 누르세요");
            }

            this.CalibrationButtonEnable(true, false, false);

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
            {
                if (this.radioButtonDecimalPlaces0.Checked != true)
                    this.radioButtonDecimalPlaces0.Checked = true;
            }
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
            {
                if (this.radioButtonDecimalPlaces1.Checked != true)
                    this.radioButtonDecimalPlaces1.Checked = true;
            }
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
            {
                if (this.radioButtonDecimalPlaces2.Checked != true)
                    this.radioButtonDecimalPlaces2.Checked = true;
            }
            else
            {
                if (this.radioButtonDecimalPlaces0.Checked != true)
                    this.radioButtonDecimalPlaces0.Checked = true;
            }

            if (this.ParentForm.ParentForm.SystemConfig.Unit == "g")
            {
                if (this.radioButtonUnitG.Checked != true)
                    this.radioButtonUnitG.Checked = true;
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Unit == "kg")
            {
                if (this.radioButtonUnitKg.Checked != true)
                    this.radioButtonUnitKg.Checked = true;
            }
            else
            {
                if (this.radioButtonUnitG.Checked != true)
                    this.radioButtonUnitG.Checked = true;
            }

            if (this.labelMaxWeightUnit.Text != this.ParentForm.ParentForm.SystemConfig.Unit)
                this.labelMaxWeightUnit.Text = this.ParentForm.ParentForm.SystemConfig.Unit;
            if (this.labelBalanceWeightUnit.Text != this.ParentForm.ParentForm.SystemConfig.Unit)
                this.labelBalanceWeightUnit.Text = this.ParentForm.ParentForm.SystemConfig.Unit;
            if (this.labelWeightUnit1.Text != this.ParentForm.ParentForm.SystemConfig.Unit)
                this.labelWeightUnit1.Text = this.labelWeightUnit2.Text = this.ParentForm.ParentForm.SystemConfig.Unit;
        }
        public void UpdateCalibrationItemDisplay(Define.E_EquipmentStatus status, CalibrationItem item)
        {
            string value = "";

            value = Helper.CommunicationWeightValueToString(item.BalanceWeight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelBalanceWeight.Text != value)
                this.labelBalanceWeight.Text = value;

            value = Helper.CommunicationWeightValueToString(item.MaxWeight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelMaxWeight.Text != value)
                this.labelMaxWeight.Text = value;

            value = item.Digit.Trim();
            if (value == "1")
            {
                if (this.radioButtonDigit1.Checked != true)
                    this.radioButtonDigit1.Checked = true;
            }
            else if (value == "2")
            {
                if (this.radioButtonDigit2.Checked != true)
                    this.radioButtonDigit2.Checked = true;
            }
            else if (value == "5")
            {
                if (this.radioButtonDigit5.Checked != true)
                    this.radioButtonDigit5.Checked = true;
            }
            else
            {
                if (this.radioButtonDigit1.Checked != true)
                    this.radioButtonDigit1.Checked = true;
            }

            value = Helper.StringToDecimalPlaces(item.Constant1.Trim(), 6);
            if (this.labelConstant1.Text != value)
                this.labelConstant1.Text = value;

            value = Helper.StringToDecimalPlaces(item.Constant2.Trim(), 6);
            if (this.labelConstant2.Text != value)
                this.labelConstant2.Text = value;
        }
        public void UpdateConstantDisplay(CalibrationItem item)
        {
            string value = "";

            value = Helper.StringToDecimalPlaces(item.Constant1.Trim(), 6);
            if (this.labelConstant1.Text != value)
                this.labelConstant1.Text = value;

            value = Helper.StringToDecimalPlaces(item.Constant2.Trim(), 6);
            if (this.labelConstant2.Text != value)
                this.labelConstant2.Text = value;
        }
        public void UpdateWeightDisplay(Define.E_EquipmentStatus status, WeightData data1, WeightData data2)
        {
            this.UpdateLabelWeight1(data1);
            this.UpdateLabelWeight2(data2);
        }

        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.FlagCalibrationFinish1 = false;
            this.FlagCalibrationFinish2 = false;

            this.buttonLane1.ButtonDown();
            this.buttonLane2.ButtonUp();

            this.UpdateControlsDisplay();

            this.Refresh();
        }
        #endregion

        #region Event Handler
        private void labelMaxWeight_Click(object sender, EventArgs e)
        {
            string value = "";
            int digit = 0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
                digit = 5;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
                digit = 6;
            else
                digit = 5;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelMaxWeight.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces, 
                false, this.ParentForm.ParentForm.SystemConfig.Language);

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

                    value = Helper.StringZeroFillDigits7(this.labelMaxWeight.Text.Replace(".", ""));
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.SubBoardAll, CommunicationAddress._3001_MaxWeight, value);
                }
            }
        }
        private void labelBalanceWeight_Click(object sender, EventArgs e)
        {
            string value = "";
            int digit = 0;

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 0)
                digit = 4;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
                digit = 5;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
                digit = 6;
            else
                digit = 5;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelBalanceWeight.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);

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

                    value = Helper.StringZeroFillDigits7(this.labelBalanceWeight.Text.Replace(".", ""));
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.SubBoardAll, CommunicationAddress._3002_BalanceWeight, value);
                }
            }
        }

        private void radioButtonUnit_Click(object sender, EventArgs e)
        {
            if (this.radioButtonUnitG.Checked == true)
                this.ParentForm.ParentForm.SystemConfig.Unit = "g";
            else if(this.radioButtonUnitKg.Checked == true)
                this.ParentForm.ParentForm.SystemConfig.Unit = "kg";
            else
                this.ParentForm.ParentForm.SystemConfig.Unit = "g";

            this.ParentForm.ParentForm.SaveSystemConfigurationFile1(this.ParentForm.ParentForm.SystemConfig);
            
            this.UpdateUnitDisplay();
        }
        private void radioButtonDigit_Click(object sender, EventArgs e)
        {
            string value = "";

            if (this.radioButtonDigit1.Checked == true)
                value = "1";
            else if (this.radioButtonDigit2.Checked == true)
                value = "2";
            else if (this.radioButtonDigit5.Checked == true)
                value = "5";
            else
                value = "1";

            value = Helper.StringZeroFillDigits4(value);

            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.SubBoardAll, CommunicationAddress._3003_Digit, value);
        }
        private void radioButtonDecimalPlaces_Click(object sender, EventArgs e)
        {
            if (this.radioButtonDecimalPlaces0.Checked == true)
                this.ParentForm.ParentForm.SystemConfig.DecimalPlaces = 0;
            else if(this.radioButtonDecimalPlaces1.Checked == true)
                this.ParentForm.ParentForm.SystemConfig.DecimalPlaces = 1;
            else if(this.radioButtonDecimalPlaces2.Checked == true)
                this.ParentForm.ParentForm.SystemConfig.DecimalPlaces = 2;

            this.ParentForm.ParentForm.SaveSystemConfigurationFile1(this.ParentForm.ParentForm.SystemConfig);

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

        private void buttonZero_Click(object sender, EventArgs e)
        {
            this.ParentForm.ParentForm.TransferData(CommunicationCommand.Zero, CommunicationID.MainBoard);
        }
        private void buttonStart_Click(object sender, EventArgs e)
        {
            this.listBoxHelp.Items.Clear();
            if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.English)
            {
                this.listBoxHelp.Items.Add("Calibration start");
                this.listBoxHelp.Items.Add("Wait a minute.");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Chinese)
            {
                this.listBoxHelp.Items.Add("校准开始按钮");
                this.listBoxHelp.Items.Add("请等待");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
            {
                this.listBoxHelp.Items.Add("Inicio de la calibración");
                this.listBoxHelp.Items.Add("Comprobando cero.");
            }
            else
            {
                this.listBoxHelp.Items.Add("중량조정을 시작합니다");
                this.listBoxHelp.Items.Add("잠시만 기다리세요");
            }

            if (this.buttonLane1.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
            {
                this.FlagCalibrationFinish1 = false;
                this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationStart, CommunicationID.SubBoard1);
            }
            else
            {
                this.FlagCalibrationFinish2 = false;
                this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationStart, CommunicationID.SubBoard2);
            }

            this.CalibrationButtonEnable(false, false, false);
        }
        private void buttonBalance_Click(object sender, EventArgs e)
        {
            this.listBoxHelp.Items.Clear();
            if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.English)
            {
                this.listBoxHelp.Items.Add("Caculating...");
                this.listBoxHelp.Items.Add("Wait a minute.");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Chinese)
            {
                this.listBoxHelp.Items.Add("测量砝码中");
                this.listBoxHelp.Items.Add("请等待");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
            {
                this.listBoxHelp.Items.Add("Caculando...");
                this.listBoxHelp.Items.Add("Esperar.");
            }
            else
            {
                this.listBoxHelp.Items.Add("분동 계산중 입니다");
                this.listBoxHelp.Items.Add("잠시만 기다리세요");
            }

            if (this.buttonLane1.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationBalance, CommunicationID.SubBoard1);
            else
                this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationBalance, CommunicationID.SubBoard2);

            this.CalibrationButtonEnable(false, false, true);
        }
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.listBoxHelp.Items.Clear();
            if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.English)
            {
                this.listBoxHelp.Items.Add("Cancel the calibration");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Chinese)
            {
                this.listBoxHelp.Items.Add("取消重量调节");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
            {
                this.listBoxHelp.Items.Add("Cancelar la calibración");
            }
            else
            {
                this.listBoxHelp.Items.Add("중량조정을 취소합니다");
            }

            if (this.buttonLane1.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationCancel, CommunicationID.SubBoard1);
            else
                this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationCancel, CommunicationID.SubBoard2);

            this.CalibrationButtonEnable(true, false, false);
        }
        #endregion
    }
}
