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_ImageDll;
using ITC81DB_2.DialogForms;
using ITC81DB_2.Forms;

namespace ITC81DB_2.Controls
{
    public partial class ControlMainLineGraph : UserControl
    {
        #region Field
        private FormMainDisplay m_ParentForm;
        #endregion

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

            this.ParentForm = parent;

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

        #region Property
        public FormMainDisplay 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.buttonOverRange1.DisableImage = this.buttonOverRange2.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLineOverDisable));
                this.buttonOverRange1.DownImage = this.buttonOverRange2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLineOverDown));
                this.buttonOverRange1.UpImage = this.buttonOverRange2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLineOverUp));
                this.buttonPassRange1.DisableImage = this.buttonPassRange2.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLinePassDisable));
                this.buttonPassRange1.DownImage = this.buttonPassRange2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLinePassDown));
                this.buttonPassRange1.UpImage = this.buttonPassRange2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLinePassUp));
                this.buttonUnderRange1.DisableImage = this.buttonUnderRange2.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLineUnderDisable));
                this.buttonUnderRange1.DownImage = this.buttonUnderRange2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLineUnderDown));
                this.buttonUnderRange1.UpImage = this.buttonUnderRange2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunLineUnderUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Chinese)
            {
                this.buttonOverRange1.DisableImage = this.buttonOverRange2.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunLineOverDisable));
                this.buttonOverRange1.DownImage = this.buttonOverRange2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunLineOverDown));
                this.buttonOverRange1.UpImage = this.buttonOverRange2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunLineOverUp));
                this.buttonPassRange1.DisableImage = this.buttonPassRange2.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunLinePassDisable));
                this.buttonPassRange1.DownImage = this.buttonPassRange2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunLinePassDown));
                this.buttonPassRange1.UpImage = this.buttonPassRange2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunLinePassUp));
                this.buttonUnderRange1.DisableImage = this.buttonUnderRange2.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunLineUnderDisable));
                this.buttonUnderRange1.DownImage = this.buttonUnderRange2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunLineUnderDown));
                this.buttonUnderRange1.UpImage = this.buttonUnderRange2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunLineUnderUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
            {
                this.buttonOverRange1.DisableImage = this.buttonOverRange2.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnMainRunLineOverDisable));
                this.buttonOverRange1.DownImage = this.buttonOverRange2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnMainRunLineOverDown));
                this.buttonOverRange1.UpImage = this.buttonOverRange2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnMainRunLineOverUp));
                this.buttonPassRange1.DisableImage = this.buttonPassRange2.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnMainRunLinePassDisable));
                this.buttonPassRange1.DownImage = this.buttonPassRange2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnMainRunLinePassDown));
                this.buttonPassRange1.UpImage = this.buttonPassRange2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnMainRunLinePassUp));
                this.buttonUnderRange1.DisableImage = this.buttonUnderRange2.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnMainRunLineUnderDisable));
                this.buttonUnderRange1.DownImage = this.buttonUnderRange2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnMainRunLineUnderDown));
                this.buttonUnderRange1.UpImage = this.buttonUnderRange2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.spnMainRunLineUnderUp));
            }
            else
            {
                this.buttonOverRange1.DisableImage = this.buttonOverRange2.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunLineOverDisable));
                this.buttonOverRange1.DownImage = this.buttonOverRange2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunLineOverDown));
                this.buttonOverRange1.UpImage = this.buttonOverRange2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunLineOverUp));
                this.buttonPassRange1.DisableImage = this.buttonPassRange2.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunLinePassDisable));
                this.buttonPassRange1.DownImage = this.buttonPassRange2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunLinePassDown));
                this.buttonPassRange1.UpImage = this.buttonPassRange2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunLinePassUp));
                this.buttonUnderRange1.DisableImage = this.buttonUnderRange2.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunLineUnderDisable));
                this.buttonUnderRange1.DownImage = this.buttonUnderRange2.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunLineUnderDown));
                this.buttonUnderRange1.UpImage = this.buttonUnderRange2.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunLineUnderUp));
            }
        }
        private void DefaultSetting()
        {
            this.InitializeDraw();
        }

        private void InitializeDraw()
        {
            SmartDraw.CHARTREFRESH charRefreshType = SmartDraw.CHARTREFRESH.LEFTSCROLL;
            SmartDraw.CHARTPENSTYLE[] charPenStyle;
            charPenStyle = new SmartX.SmartDraw.CHARTPENSTYLE[1];
            charPenStyle[0].m_chColor = Color.White;
            charPenStyle[0].m_iPenWidth = 2;

            this.draw1.SetChartCfg(2, 210, 268, 210, 5, charRefreshType, 1);
            this.draw1.ChartChannelPenStyle = charPenStyle;
            this.draw1.ChartDrawStep = 1;

            this.draw2.SetChartCfg(2, 210, 268, 210, 5, charRefreshType, 1);
            this.draw2.ChartChannelPenStyle = charPenStyle;
            this.draw2.ChartDrawStep = 1;
        }
        private void SetDrawData(SmartDraw draw, string stringWeight, ProductItem product)
        {
            int value = 0, gap = 0, startingPoint = 0, weight = 0;

            try
            {
                weight = int.Parse(stringWeight);
                gap = int.Parse(product.OverRange.Replace(".", "")) - int.Parse(product.UnderRange.Replace(".", ""));
                startingPoint = int.Parse(product.UnderRange.Replace(".", "")) - gap;

                value = weight - startingPoint;
                value = (value * 70) / gap;

                if (value > 210)
                    value = 210;
                if (value < 1)
                    value = 1;

                draw.PutData(value);
            }
            catch
            {
            }
        }
        private void SetProduct1(ProductItem item)
        {
            string underValue = "", passValue = "", overValue = "";

            switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
            {
                case Define.E_WeightInputMode.Weight:
                    underValue = Helper.StringToDecimalPlaces(item.UnderRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    passValue = Helper.StringToDecimalPlaces(item.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    overValue = Helper.StringToDecimalPlaces(item.OverRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    break;
                case Define.E_WeightInputMode.Deviation:
                    if (item.UnderRangeDeviation == 0)
                        underValue = string.Format("-{0}", Helper.DoubleToString(0, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces));
                    else
                        underValue = Helper.StringToDecimalPlaces(item.UnderRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    passValue = Helper.StringToDecimalPlaces(item.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    overValue = Helper.StringToDecimalPlaces(item.OverRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    overValue = string.Format("+{0}", overValue);
                    break;
                default:
                    break;
            }

            if (this.buttonUnderRange1.Text != underValue)
                this.buttonUnderRange1.Text = underValue;

            if (this.buttonPassRange1.Text != passValue)
                this.buttonPassRange1.Text = passValue;

            if (this.buttonOverRange1.Text != overValue)
                this.buttonOverRange1.Text = overValue;
        }
        private void SetProduct2(ProductItem item)
        {
            string underValue = "", passValue = "", overValue = "";

            switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
            {
                case Define.E_WeightInputMode.Weight:
                    underValue = Helper.StringToDecimalPlaces(item.UnderRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    passValue = Helper.StringToDecimalPlaces(item.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    overValue = Helper.StringToDecimalPlaces(item.OverRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    break;
                case Define.E_WeightInputMode.Deviation:
                    if (item.UnderRangeDeviation == 0)
                        underValue = string.Format("-{0}", Helper.DoubleToString(0, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces));
                    else
                        underValue = Helper.StringToDecimalPlaces(item.UnderRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    passValue = Helper.StringToDecimalPlaces(item.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    overValue = Helper.StringToDecimalPlaces(item.OverRangeDeviation.ToString(), this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    overValue = string.Format("+{0}", overValue);
                    break;
                default:
                    break;
            }

            if (this.buttonUnderRange2.Text != underValue)
                this.buttonUnderRange2.Text = underValue;

            if (this.buttonPassRange2.Text != passValue)
                this.buttonPassRange2.Text = passValue;

            if (this.buttonOverRange2.Text != overValue)
                this.buttonOverRange2.Text = overValue;
        }
        private void SetCount1(WeightData data)
        {
            string value = "";

            #region Count
            value = data.UnderCount.ToString();
            if (this.labelUnderCount1.Text != value)
                this.labelUnderCount1.Text = value;

            value = data.PassCount.ToString();
            if (this.labelPassCount1.Text != value)
                this.labelPassCount1.Text = value;

            value = data.OverCount.ToString();
            if (this.labelOverCount1.Text != value)
                this.labelOverCount1.Text = value; 
            #endregion

            #region Ratio
            value = data.UnderRatio;
            if (this.labelUnderRatio1.Text != value)
                this.labelUnderRatio1.Text = value;

            value = data.PassRatio;
            if (this.labelPassRatio1.Text != value)
                this.labelPassRatio1.Text = value;

            value = data.OverRatio;
            if (this.labelOverRatio1.Text != value)
                this.labelOverRatio1.Text = value;
            #endregion
        }
        private void SetCount2(WeightData data)
        {
            string value = "";

            #region Count
            value = data.UnderCount.ToString();
            if (this.labelUnderCount2.Text != value)
                this.labelUnderCount2.Text = value;

            value = data.PassCount.ToString();
            if (this.labelPassCount2.Text != value)
                this.labelPassCount2.Text = value;

            value = data.OverCount.ToString();
            if (this.labelOverCount2.Text != value)
                this.labelOverCount2.Text = value; 
            #endregion

            #region Ratio
            value = data.UnderRatio;
            if (this.labelUnderRatio2.Text != value)
                this.labelUnderRatio2.Text = value;

            value = data.PassRatio;
            if (this.labelPassRatio2.Text != value)
                this.labelPassRatio2.Text = value;

            value = data.OverRatio;
            if (this.labelOverRatio2.Text != value)
                this.labelOverRatio2.Text = value;
            #endregion
        }
        private void UpdateDisplayUser(UserItem user)
        {
            switch (user.Group)
            {
                case Define.E_UserGroup.LogOut:
                    this.buttonUnderRange1.Enabled = this.buttonUnderRange2.Enabled = false;
                    this.buttonPassRange1.Enabled = this.buttonPassRange2.Enabled = false;
                    this.buttonOverRange1.Enabled = this.buttonOverRange2.Enabled = false;
                    break;
                case Define.E_UserGroup.Level1:
                    this.buttonUnderRange1.Enabled = this.buttonUnderRange2.Enabled = true;
                    this.buttonPassRange1.Enabled = this.buttonPassRange2.Enabled = true;
                    this.buttonOverRange1.Enabled = this.buttonOverRange2.Enabled = true;
                    break;
                case Define.E_UserGroup.Level2:
                    this.buttonUnderRange1.Enabled = this.buttonUnderRange2.Enabled = true;
                    this.buttonPassRange1.Enabled = this.buttonPassRange2.Enabled = true;
                    this.buttonOverRange1.Enabled = this.buttonOverRange2.Enabled = true;
                    break;
                case Define.E_UserGroup.Level3:
                    this.buttonUnderRange1.Enabled = this.buttonUnderRange2.Enabled = true;
                    this.buttonPassRange1.Enabled = this.buttonPassRange2.Enabled = true;
                    this.buttonOverRange1.Enabled = this.buttonOverRange2.Enabled = true;
                    break;
                case Define.E_UserGroup.Developer:
                    this.buttonUnderRange1.Enabled = this.buttonUnderRange2.Enabled = true;
                    this.buttonPassRange1.Enabled = this.buttonPassRange2.Enabled = true;
                    this.buttonOverRange1.Enabled = this.buttonOverRange2.Enabled = true;
                    break;
                default:
                    this.buttonUnderRange1.Enabled = this.buttonUnderRange2.Enabled = false;
                    this.buttonPassRange1.Enabled = this.buttonPassRange2.Enabled = false;
                    this.buttonOverRange1.Enabled = this.buttonOverRange2.Enabled = false;
                    break;
            }
        }
        
        public void UpdateCurrentProductDisplay(Define.E_EquipmentStatus status, ProductItem pItem1, ProductItem pItem2, WeightData wData1, WeightData wData2)
        {
            this.SetProduct1(pItem1);
            this.SetProduct2(pItem2);

            this.SetCount1(wData1);
            this.SetCount2(wData2);

            this.draw1.PutDataAllClear();
            this.draw2.PutDataAllClear();
        }

        public void UpdateStartWeightDisplay1(Define.E_EquipmentStatus status, WeightData data)
        {
            string value = "";

            if (data.JudgmentStatus == Define.E_JudgmentStatus.Empty)
                return;

            this.SetDrawData(this.draw1, data.WeightString, this.ParentForm.ParentForm.CurrentProductItem1);
            this.SetCount1(data);
        }
        public void UpdateStartWeightDisplay2(Define.E_EquipmentStatus status, WeightData data)
        {
            string value = "";

            if (data.JudgmentStatus == Define.E_JudgmentStatus.Empty)
                return;

            this.SetDrawData(this.draw2, data.WeightString, this.ParentForm.ParentForm.CurrentProductItem2);
            this.SetCount2(data);
        }

        public void Clear()
        {
            this.SetCount1(this.ParentForm.ParentForm.CurrentWeightData1);
            this.SetCount2(this.ParentForm.ParentForm.CurrentWeightData2);
            this.draw1.PutDataAllClear();
            this.draw2.PutDataAllClear();
        }
        public void DisplayRefresh(SystemStatus status)
        {
            this.UpdateDisplayUser(status.CurrentUser);

            this.SetProduct1(this.ParentForm.ParentForm.CurrentProductItem1);
            this.SetProduct2(this.ParentForm.ParentForm.CurrentProductItem2);
            this.SetCount1(this.ParentForm.ParentForm.CurrentWeightData1);
            this.SetCount2(this.ParentForm.ParentForm.CurrentWeightData2);
        }
        #endregion

        #region Event Handler
        private void buttonUnderRange1_Click(object sender, EventArgs e)
        {
            string value = "", sValue = "";
            double dValue = 0.0;
            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.buttonUnderRange1.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
               false, this.ParentForm.ParentForm.SystemConfig.Language);

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

            switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
            {
                case Define.E_WeightInputMode.Weight:
                    dValue = myKeyPad.doubleValue;
                    sValue = myKeyPad.StringValue;
                    break;
                case Define.E_WeightInputMode.Deviation:
                    dValue = Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem1.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)
                        - Math.Abs(myKeyPad.doubleValue);
                    sValue = Helper.DoubleToString(dValue, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    break;
                default:
                    dValue = myKeyPad.doubleValue;
                    sValue = myKeyPad.StringValue;
                    break;
            }

            if (dValue > Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem1.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces) || dValue < 0)
            {
                // 입력범위를 확인하세요
                DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                myMsg.ShowDialog();
            }
            else
            {
                this.ParentForm.ParentForm.CurrentProductItem1.UnderRange = sValue.Replace(".", "");

                switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
                {
                    case Define.E_WeightInputMode.Weight:
                        value = myKeyPad.StringValue;
                        break;
                    case Define.E_WeightInputMode.Deviation:
                        if (myKeyPad.doubleValue == 0)
                            value = string.Format("-{0}", Helper.DoubleToString(0, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces));
                        else
                            value = myKeyPad.StringValue;
                        break;
                    default:
                        value = myKeyPad.StringValue;
                        break;
                }

                this.buttonUnderRange1.Text = value;

                this.ParentForm.RescaleControl();
                this.ParentForm.ParentForm.SaveProductFile1(this.ParentForm.ParentForm.CurrentProductItem1, this.ParentForm.ParentForm.SystemConfig.ProductNumber - 1);

                if (this.ParentForm.MainFeedback.IsUsingFeedback() == true)
                {
                    string feedbackValue = "";

                    if (this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1IsWeightSetting == "0")
                    {
                        this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1UnderRange1 = this.ParentForm.ParentForm.CurrentProductItem1.UnderRange;
                        this.ParentForm.ParentForm.CurrentFeedbackItem1.UnderRangeDeviation = this.ParentForm.ParentForm.CurrentProductItem1.UnderRangeDeviation.ToString();

                        feedbackValue = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1UnderRange1);
                        this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._6206_OPT1UnderRange1, feedbackValue);
                    }
                }

                value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem1.UnderRange);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._2002_UnderRange, value);
            }
        }
        private void buttonPassRange1_Click(object sender, EventArgs e)
        {
            string value = "", underRange = "", overRange = "", viewValue = "";
            int digit = 0, temp = 0, range = 0;
            StringBuilder sb = new StringBuilder();

            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;

            if (this.ParentForm.ParentForm.CurrentWeightData1.Weight < 1)
                viewValue = this.buttonPassRange1.Text;
            else
                viewValue = Helper.DoubleToString(this.ParentForm.ParentForm.CurrentWeightData1.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

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

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

            this.ParentForm.ParentForm.CurrentProductItem1.PassRange = myKeyPad.StringValue.Replace(".", "");

            temp = this.ParentForm.ParentForm.CurrentProductItem1.PassRangeInt + this.ParentForm.ParentForm.CurrentProductItem1.UnderRangeDeviation;
            if (temp < 0)
                this.ParentForm.ParentForm.CurrentProductItem1.UnderRange = "0";
            else
                this.ParentForm.ParentForm.CurrentProductItem1.UnderRange = temp.ToString();

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
                range = 1000000;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
                range = 100000;
            else
                range = 10000;
            temp = this.ParentForm.ParentForm.CurrentProductItem1.PassRangeInt + this.ParentForm.ParentForm.CurrentProductItem1.OverRangeDeviation;
            if (temp >= range)
                this.ParentForm.ParentForm.CurrentProductItem1.OverRange = this.ParentForm.ParentForm.CurrentProductItem1.PassRange;
            else
                this.ParentForm.ParentForm.CurrentProductItem1.OverRange = temp.ToString();

            this.ParentForm.RescaleControl();

            this.ParentForm.ParentForm.SaveProductFile1(this.ParentForm.ParentForm.CurrentProductItem1, this.ParentForm.ParentForm.SystemConfig.ProductNumber - 1);

            sb.Append(Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem1.UnderRange));
            sb.Append(Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem1.PassRange));
            sb.Append(Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem1.OverRange));
            sb.Append(Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem1.TareRange));
            sb.Append(Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.SystemConfig.ProductNumber.ToString()));
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.SubBoard1, CommunicationAddress._9024_ParameterWeightSetting, sb.ToString());

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

            if (this.ParentForm.MainFeedback.IsUsingFeedback() == true)
                this.ParentForm.ParentForm.UpdateFeedbackData1(this.ParentForm.ParentForm.CurrentProductItem1, this.ParentForm.ParentForm.CurrentSystemParameter9508);

            this.buttonPassRange1.Text = myKeyPad.StringValue;
            this.buttonUnderRange1.Text = underRange;
            this.buttonOverRange1.Text = overRange;
        }
        private void buttonOverRange1_Click(object sender, EventArgs e)
        {
            string value = "", sValue = "";
            double dValue = 0.0;
            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.buttonOverRange1.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);

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

            switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
            {
                case Define.E_WeightInputMode.Weight:
                    dValue = myKeyPad.doubleValue;
                    sValue = myKeyPad.StringValue;
                    break;
                case Define.E_WeightInputMode.Deviation:
                    dValue = Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem1.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)
                        + myKeyPad.doubleValue;
                    sValue = Helper.DoubleToString(dValue, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    break;
                default:
                    dValue = myKeyPad.doubleValue;
                    sValue = myKeyPad.StringValue;
                    break;
            }

            if (dValue < Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem1.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces) ||
                dValue >= 10000)
            {
                // 입력범위를 확인하세요
                DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                myMsg.ShowDialog();
            }
            else
            {
                this.ParentForm.ParentForm.CurrentProductItem1.OverRange = sValue.Replace(".", "");

                switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
                {
                    case Define.E_WeightInputMode.Weight:
                        value = myKeyPad.StringValue;
                        break;
                    case Define.E_WeightInputMode.Deviation:
                        value = string.Format("+{0}", myKeyPad.StringValue);
                        break;
                    default:
                        value = myKeyPad.StringValue;
                        break;
                }
                this.buttonOverRange1.Text = value;

                this.ParentForm.RescaleControl();
                this.ParentForm.ParentForm.SaveProductFile1(this.ParentForm.ParentForm.CurrentProductItem1, this.ParentForm.ParentForm.SystemConfig.ProductNumber - 1);

                if (this.ParentForm.MainFeedback.IsUsingFeedback() == true)
                {
                    string feedbackValue = "";

                    if (this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1IsWeightSetting == "0")
                    {
                        this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1OverRange1 = this.ParentForm.ParentForm.CurrentProductItem1.OverRange;
                        this.ParentForm.ParentForm.CurrentFeedbackItem1.OverRangeDeviation = this.ParentForm.ParentForm.CurrentProductItem1.OverRangeDeviation.ToString();

                        feedbackValue = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1OverRange1);
                        this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._6205_OPT1OverRange1, feedbackValue);
                    }
                }

                value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem1.OverRange);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._2004_OverRange, value);
            }
        }
        private void buttonUnderRange2_Click(object sender, EventArgs e)
        {
            string value = "", sValue = "";
            double dValue = 0.0;
            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.buttonUnderRange2.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
               false, this.ParentForm.ParentForm.SystemConfig.Language);

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

            switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
            {
                case Define.E_WeightInputMode.Weight:
                    dValue = myKeyPad.doubleValue;
                    sValue = myKeyPad.StringValue;
                    break;
                case Define.E_WeightInputMode.Deviation:
                    dValue = Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem2.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)
                        - Math.Abs(myKeyPad.doubleValue);
                    sValue = Helper.DoubleToString(dValue, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    break;
                default:
                    dValue = myKeyPad.doubleValue;
                    sValue = myKeyPad.StringValue;
                    break;
            }

            if (dValue > Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem2.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces) || dValue < 0)
            {
                // 입력범위를 확인하세요
                DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                myMsg.ShowDialog();
            }
            else
            {
                this.ParentForm.ParentForm.CurrentProductItem2.UnderRange = sValue.Replace(".", "");

                switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
                {
                    case Define.E_WeightInputMode.Weight:
                        value = myKeyPad.StringValue;
                        break;
                    case Define.E_WeightInputMode.Deviation:
                        if (myKeyPad.doubleValue == 0)
                            value = string.Format("-{0}", Helper.DoubleToString(0, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces));
                        else
                            value = myKeyPad.StringValue;
                        break;
                    default:
                        value = myKeyPad.StringValue;
                        break;
                }

                this.buttonUnderRange2.Text = value;

                this.ParentForm.RescaleControl();
                this.ParentForm.ParentForm.SaveProductFile2(this.ParentForm.ParentForm.CurrentProductItem2, this.ParentForm.ParentForm.SystemConfig.ProductNumber - 1);

                if (this.ParentForm.MainFeedback.IsUsingFeedback() == true)
                {
                    string feedbackValue = "";

                    if (this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1IsWeightSetting == "0")
                    {
                        this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1UnderRange2 = this.ParentForm.ParentForm.CurrentProductItem2.UnderRange;
                        this.ParentForm.ParentForm.CurrentFeedbackItem2.UnderRangeDeviation = this.ParentForm.ParentForm.CurrentProductItem2.UnderRangeDeviation.ToString();

                        feedbackValue = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1UnderRange2);
                        this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._6217_OPT1UnderRange2, feedbackValue);
                    }
                }

                value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem2.UnderRange);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._2006_UnderRange, value);
            }
        }
        private void buttonPassRange2_Click(object sender, EventArgs e)
        {
            string value = "", underRange = "", overRange = "", viewValue = "";
            int digit = 0, temp = 0, range = 0;
            StringBuilder sb = new StringBuilder();

            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;

            if (this.ParentForm.ParentForm.CurrentWeightData2.Weight < 1)
                viewValue = this.buttonPassRange2.Text;
            else
                viewValue = Helper.DoubleToString(this.ParentForm.ParentForm.CurrentWeightData2.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);

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

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

            this.ParentForm.ParentForm.CurrentProductItem2.PassRange = myKeyPad.StringValue.Replace(".", "");

            temp = this.ParentForm.ParentForm.CurrentProductItem2.PassRangeInt + this.ParentForm.ParentForm.CurrentProductItem2.UnderRangeDeviation;
            if (temp < 0)
                this.ParentForm.ParentForm.CurrentProductItem2.UnderRange = "0";
            else
                this.ParentForm.ParentForm.CurrentProductItem2.UnderRange = temp.ToString();

            if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 2)
                range = 1000000;
            else if (this.ParentForm.ParentForm.SystemConfig.DecimalPlaces == 1)
                range = 100000;
            else
                range = 10000;
            temp = this.ParentForm.ParentForm.CurrentProductItem2.PassRangeInt + this.ParentForm.ParentForm.CurrentProductItem2.OverRangeDeviation;
            if (temp >= range)
                this.ParentForm.ParentForm.CurrentProductItem2.OverRange = this.ParentForm.ParentForm.CurrentProductItem2.PassRange;
            else
                this.ParentForm.ParentForm.CurrentProductItem2.OverRange = temp.ToString();

            this.ParentForm.RescaleControl();

            this.ParentForm.ParentForm.SaveProductFile2(this.ParentForm.ParentForm.CurrentProductItem2, this.ParentForm.ParentForm.SystemConfig.ProductNumber - 1);

            sb.Append(Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem2.UnderRange));
            sb.Append(Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem2.PassRange));
            sb.Append(Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem2.OverRange));
            sb.Append(Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem2.TareRange));
            sb.Append(Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.SystemConfig.ProductNumber.ToString()));
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.SubBoard2, CommunicationAddress._9024_ParameterWeightSetting, sb.ToString());

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

            if (this.ParentForm.MainFeedback.IsUsingFeedback() == true)
                this.ParentForm.ParentForm.UpdateFeedbackData2(this.ParentForm.ParentForm.CurrentProductItem2, this.ParentForm.ParentForm.CurrentSystemParameter9508);

            this.buttonPassRange2.Text = myKeyPad.StringValue;
            this.buttonUnderRange2.Text = underRange;
            this.buttonOverRange2.Text = overRange;
        }
        private void buttonOverRange2_Click(object sender, EventArgs e)
        {
            string value = "", sValue = "";
            double dValue = 0.0;
            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.buttonOverRange2.Text, digit, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig.Language);

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

            switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
            {
                case Define.E_WeightInputMode.Weight:
                    dValue = myKeyPad.doubleValue;
                    sValue = myKeyPad.StringValue;
                    break;
                case Define.E_WeightInputMode.Deviation:
                    dValue = Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem2.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)
                        + myKeyPad.doubleValue;
                    sValue = Helper.DoubleToString(dValue, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    break;
                default:
                    dValue = myKeyPad.doubleValue;
                    sValue = myKeyPad.StringValue;
                    break;
            }

            if (dValue < Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem2.PassRange, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces) ||
                dValue >= 10000)
            {
                // 입력범위를 확인하세요
                DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                myMsg.ShowDialog();
            }
            else
            {
                this.ParentForm.ParentForm.CurrentProductItem2.OverRange = sValue.Replace(".", "");

                switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
                {
                    case Define.E_WeightInputMode.Weight:
                        value = myKeyPad.StringValue;
                        break;
                    case Define.E_WeightInputMode.Deviation:
                        value = string.Format("+{0}", myKeyPad.StringValue);
                        break;
                    default:
                        value = myKeyPad.StringValue;
                        break;
                }
                this.buttonOverRange2.Text = value;

                this.ParentForm.RescaleControl();
                this.ParentForm.ParentForm.SaveProductFile2(this.ParentForm.ParentForm.CurrentProductItem2, this.ParentForm.ParentForm.SystemConfig.ProductNumber - 1);

                if (this.ParentForm.MainFeedback.IsUsingFeedback() == true)
                {
                    string feedbackValue = "";

                    if (this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1IsWeightSetting == "0")
                    {
                        this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1OverRange2 = this.ParentForm.ParentForm.CurrentProductItem2.OverRange;
                        this.ParentForm.ParentForm.CurrentFeedbackItem2.OverRangeDeviation = this.ParentForm.ParentForm.CurrentProductItem2.OverRangeDeviation.ToString();

                        feedbackValue = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentSystemParameter9508.OPT1OverRange2);
                        this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._6216_OPT1OverRange2, feedbackValue);
                    }
                }

                value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem2.OverRange);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._2008_OverRange, value);
            }
        }
        #endregion
    }
}
