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

namespace ITC81DB_2H.Controls
{
    public partial class ControlMainDisplayStart : UserControl
    {
        #region Field
        private FormMainDisplay m_ParentForm;
        #endregion
        
        #region Constructor
        public ControlMainDisplayStart(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.SystemConfig1.Language == Define.E_LanguageID.English)
            {
                this.labelTitleSpeed.Text = "Speed";
                this.labelTitleExNgCount.Text = "Other faulty";
                this.labelTitleTotalCount.Text = "Total";

                 this.buttonOverRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunOverDisable));
                this.buttonOverRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunOverDown));
                this.buttonOverRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunOverUp));
                this.buttonPassRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunPassDisable));
                this.buttonPassRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunPassDown));
                this.buttonPassRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunPassUp));
                this.buttonUnderRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunUnderDisable));
                this.buttonUnderRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunUnderDown));
                this.buttonUnderRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunUnderUp));
                this.buttonTareRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunTareDisable));
                this.buttonTareRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunTareDown));
                this.buttonTareRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engMainRunTareUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
            {
                this.buttonOverRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunOverDisable));
                this.buttonOverRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunOverDown));
                this.buttonOverRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunOverUp));
                this.buttonPassRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunPassDisable));
                this.buttonPassRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunPassDown));
                this.buttonPassRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunPassUp));
                this.buttonUnderRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunUnderDisable));
                this.buttonUnderRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunUnderDown));
                this.buttonUnderRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunUnderUp));
                this.buttonTareRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunTareDisable));
                this.buttonTareRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunTareDown));
                this.buttonTareRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainRunTareUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
            {
                this.buttonOverRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunOverDisable));
                this.buttonOverRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunOverDown));
                this.buttonOverRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunOverUp));
                this.buttonPassRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunPassDisable));
                this.buttonPassRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunPassDown));
                this.buttonPassRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunPassUp));
                this.buttonUnderRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunUnderDisable));
                this.buttonUnderRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunUnderDown));
                this.buttonUnderRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunUnderUp));
                this.buttonTareRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunTareDisable));
                this.buttonTareRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunTareDown));
                this.buttonTareRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.rusMainRunTareUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
            {
                this.buttonOverRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunOverDisable));
                this.buttonOverRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunOverDown));
                this.buttonOverRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunOverUp));
                this.buttonPassRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunPassDisable));
                this.buttonPassRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunPassDown));
                this.buttonPassRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunPassUp));
                this.buttonUnderRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunUnderDisable));
                this.buttonUnderRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunUnderDown));
                this.buttonUnderRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunUnderUp));
                this.buttonTareRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunTareDisable));
                this.buttonTareRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunTareDown));
                this.buttonTareRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainRunTareUp));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
            {

            }
            else
            {
                this.labelTitleSpeed.Text = "속도";
                this.labelTitleExNgCount.Text = "기타불량";
                this.labelTitleTotalCount.Text = "총계";

                this.buttonOverRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunOverDisable));
                this.buttonOverRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunOverDown));
                this.buttonOverRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunOverUp));
                this.buttonPassRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunPassDisable));
                this.buttonPassRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunPassDown));
                this.buttonPassRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunPassUp));
                this.buttonUnderRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunUnderDisable));
                this.buttonUnderRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunUnderDown));
                this.buttonUnderRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunUnderUp));
                this.buttonTareRange.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunTareDisable));
                this.buttonTareRange.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunTareDown));
                this.buttonTareRange.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.korMainRunTareUp));
            }
        }
        private void DefaultSetting()
        {
        }

        public void RescaleProgressBar(ProductItem item)
        {
            int value = 0;

            value = item.ProgressBarMaximum;
            if (this.progressBar.Maximum != value)
                this.progressBar.Maximum = value;

            value = item.ProgressBarMinimum;
            if (this.progressBar.Minimum != value)
                this.progressBar.Minimum = value;

            this.progressBar.Value = this.progressBar.Minimum;
        }
        private void SetProduct(ProductItem item)
        {
            string underValue = "", passValue = "", overValue = "", tareValue = "";

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

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

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

            if (this.buttonOverRange.Text != overValue)
                this.buttonOverRange.Text = overValue;

            if (this.buttonTareRange.Text != tareValue)
                this.buttonTareRange.Text = tareValue;
        }
        private void SetCount(WeightData data)
        {
            string value = "";

            value = data.UnderCount.ToString();
            if (this.labelUnderCount.Text != value)
                this.labelUnderCount.Text = value;

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

            value = data.OverCount.ToString();
            if (this.labelOverCount.Text != value)
                this.labelOverCount.Text = value;

            if (this.ParentForm.IsCurrentValueDouble == false)
                value = data.ExNGCount.ToString();
            else
                value = data.DoubleEntryCount.ToString();
            if (this.labelExNGCount.Text != value)
                this.labelExNGCount.Text = value;

            value = data.TotalCount.ToString();
            if (this.labelTotalCount.Text != value)
                this.labelTotalCount.Text = value;
        }
        private void SetProgressBarValue(SmartProgressBar progressBar, double weight, Define.E_JudgmentStatus judStatus, int decimalPlaces)
        {
            int value = 0;

            if (decimalPlaces == 0)
                value = int.Parse(string.Format("{0}", weight));
            else if (decimalPlaces == 1)
                value = int.Parse(string.Format("{0}", weight * 10));
            else if (decimalPlaces == 2)
                value = int.Parse(string.Format("{0}", weight * 100));
            else if (decimalPlaces == 3)
                value = int.Parse(string.Format("{0}", weight * 1000));

            if (progressBar.Value != value)
                progressBar.Value = value;

            if (judStatus == Define.E_JudgmentStatus.Over)
            {
                if (progressBar.BarColor1 != Color.Yellow)
                    progressBar.BarColor1 = Color.Yellow;
            }
            else if (judStatus == Define.E_JudgmentStatus.Under || judStatus == Define.E_JudgmentStatus.Double || judStatus == Define.E_JudgmentStatus.ExNg || judStatus == Define.E_JudgmentStatus.Metal)
            {
                if (progressBar.BarColor1 != Color.Red)
                    progressBar.BarColor1 = Color.Red;
            }
            else
            {
                if (progressBar.BarColor1 != Color.Lime)
                    progressBar.BarColor1 = Color.Lime;
            }
        }
        private void UpdateDisplayUser(UserItem user)
        {
            switch (user.Group)
            {
                case Define.E_UserGroup.LogOut:
                    this.buttonUnderRange.Enabled = false;
                    this.buttonPassRange.Enabled = false;
                    this.buttonOverRange.Enabled = false;
                    this.buttonTareRange.Enabled = false;
                    break;
                case Define.E_UserGroup.Level1:
                    this.buttonUnderRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMainDisplayWeightSetting;
                    this.buttonPassRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMainDisplayWeightSetting;
                    this.buttonOverRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMainDisplayWeightSetting;
                    this.buttonTareRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level1.IsMainDisplayWeightSetting;
                    break;
                case Define.E_UserGroup.Level2:
                    this.buttonUnderRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMainDisplayWeightSetting;
                    this.buttonPassRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMainDisplayWeightSetting;
                    this.buttonOverRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMainDisplayWeightSetting;
                    this.buttonTareRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level2.IsMainDisplayWeightSetting;
                    break;
                case Define.E_UserGroup.Level3:
                    this.buttonUnderRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMainDisplayWeightSetting;
                    this.buttonPassRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMainDisplayWeightSetting;
                    this.buttonOverRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMainDisplayWeightSetting;
                    this.buttonTareRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.Level3.IsMainDisplayWeightSetting;
                    break;
                case Define.E_UserGroup.Admin:
                case Define.E_UserGroup.Developer:
                    this.buttonUnderRange.Enabled = true;
                    this.buttonPassRange.Enabled = true;
                    this.buttonOverRange.Enabled = true;
                    this.buttonTareRange.Enabled = true;
                    break;
                case Define.E_UserGroup.NotLogin:
                    this.buttonUnderRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.NotLogin.IsMainDisplayWeightSetting;
                    this.buttonPassRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.NotLogin.IsMainDisplayWeightSetting;
                    this.buttonOverRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.NotLogin.IsMainDisplayWeightSetting;
                    this.buttonTareRange.Enabled = this.ParentForm.ParentForm.CurrentUserGroup.NotLogin.IsMainDisplayWeightSetting;
                    break;
                default:
                    this.buttonUnderRange.Enabled = false;
                    this.buttonPassRange.Enabled = false;
                    this.buttonOverRange.Enabled = false;
                    this.buttonTareRange.Enabled = false;
                    break;
            }
        }

        public void UpdateEquipmentStatusDisplay(Define.E_EquipmentStatus status)
        {
            this.labelSpeed.Text = "0";
            this.progressBar.Value = this.progressBar.Minimum;
        }
        public void UpdateCurrentProductDisplay(Define.E_EquipmentStatus status, ProductItem pItem, WeightData wData)
        {
            this.SetProduct(pItem);
            this.SetCount(wData);

            this.RescaleProgressBar(pItem);
        }
        public void UpdateStartWeightDisplay(Define.E_EquipmentStatus status, WeightData data)
        {
            string value = "";

            if (data.ProductionSpeed <= 500)
            {
                value = data.ProductionSpeed.ToString();
                if (this.labelSpeed.Text != value)
                    this.labelSpeed.Text = value;
            }

            this.SetProgressBarValue(this.progressBar, data.Weight, data.JudgmentStatus, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);

            this.SetCount(data);
        }

        private void DisplayExNG(bool bValue)
        {
            if (bValue == false)
            {
                if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.English)
                    this.labelTitleExNgCount.Text = "Other faulty";
                else
                    this.labelTitleExNgCount.Text = "기타불량";

                this.labelExNGCount.Text = this.ParentForm.ParentForm.CurrentWeightData.ExNGCount.ToString();
            }
            else
            {
                if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.English)
                    this.labelTitleExNgCount.Text = "Double";
                else
                    this.labelTitleExNgCount.Text = "이중진입";

                this.labelExNGCount.Text = this.ParentForm.ParentForm.CurrentWeightData.DoubleEntryCount.ToString();
            }
        }
        public void Clear()
        {
            this.SetCount(this.ParentForm.ParentForm.CurrentWeightData);

            this.progressBar.Value = this.progressBar.Minimum;
            this.labelSpeed.Text = "0";
        }
        public void DisplayRefresh(SystemStatus status)
        {
            this.UpdateDisplayUser(status.CurrentUser);
            this.SetProduct(this.ParentForm.ParentForm.CurrentProductItem);
            this.SetCount(this.ParentForm.ParentForm.CurrentWeightData);
            this.DisplayExNG(this.ParentForm.IsCurrentValueDouble);
        }
        #endregion

        #region Event Handler
        private void labelTitleExNgCount_Click(object sender, EventArgs e)
        {
            // Automatic Logout Reset
            if (this.ParentForm.ParentForm.SystemConfig3.IsPart11 == true)
                this.ParentForm.Part11AutomaticLogoutReset();

            if (this.ParentForm.IsCurrentValueDouble == false)
                this.ParentForm.IsCurrentValueDouble = true;
            else
                this.ParentForm.IsCurrentValueDouble = false;

            this.DisplayExNG(this.ParentForm.IsCurrentValueDouble);
        }

        private void buttonUnderRange_Click(object sender, EventArgs e)
        {
            // Automatic Logout Reset
            if (this.ParentForm.ParentForm.SystemConfig3.IsPart11 == true)
                this.ParentForm.Part11AutomaticLogoutReset();

            string value = "", sValue = "", before = "", after = "";
            double dValue = 0.0;

            before = this.buttonUnderRange.Text;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.buttonUnderRange.Text, 5, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces,
               false, this.ParentForm.ParentForm.SystemConfig1.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.CurrentProductItem.PassRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces)
                        - Math.Abs(myKeyPad.doubleValue);
                    sValue = Helper.DoubleToString(dValue, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    break;
                default:
                    dValue = myKeyPad.doubleValue;
                    sValue = myKeyPad.StringValue;
                    break;
            }

            if (dValue > Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem.PassRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces) || dValue < 0)
            {
                // 입력범위를 확인하세요
                DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language);
                myMsg.ShowDialog();
            }
            else
            {
                this.ParentForm.ParentForm.CurrentProductItem.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.SystemConfig1.DecimalPlaces));
                        else
                            value = myKeyPad.StringValue;
                        break;
                    default:
                        value = myKeyPad.StringValue;
                        break;
                }

                after = this.buttonUnderRange.Text = value;
                this.ParentForm.RescaleControl(this.ParentForm.ParentForm.CurrentProductItem);
                this.ParentForm.ParentForm.SaveProductFile(this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);

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

                    if (this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1IsWeightSetting == "0")
                    {
                        this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1UnderRange = this.ParentForm.ParentForm.CurrentProductItem.UnderRange;
                        this.ParentForm.ParentForm.CurrentFeedbackItem.UnderRangeDeviation = this.ParentForm.ParentForm.CurrentProductItem.UnderRangeDeviation.ToString();

                        this.ParentForm.ParentForm.SaveSystemParameter2File(this.ParentForm.ParentForm.CurrentSystemParameter2);
                        feedbackValue = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1UnderRange);
                        this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.OPT1UnderRange, feedbackValue);
                    }
                }

                this.ParentForm.ParentForm.Update30000ModbusItem();
                value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem.UnderRange);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.UnderRange, value);

                if (before != after)
                    this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.UnderRange, "", before, after);
            }
        }
        private void buttonPassRange_Click(object sender, EventArgs e)
        {
            // Automatic Logout Reset
            if (this.ParentForm.ParentForm.SystemConfig3.IsPart11 == true)
                this.ParentForm.Part11AutomaticLogoutReset();

            string value = "", underRange = "", overRange = "", viewValue = "";
            string beforeOver = "", beforeUnder = "", afterOver = "", afterUnder = "", before = "", after = "";
            int digit = 0, temp = 0;
            StringBuilder sb = new StringBuilder();
            int oldUnderRangeDeviation = this.ParentForm.ParentForm.CurrentProductItem.UnderRangeDeviation;
            int oldOverRangeDeviation = this.ParentForm.ParentForm.CurrentProductItem.OverRangeDeviation;

            if (this.ParentForm.ParentForm.CurrentWeightData.Weight < 1)
                viewValue = this.buttonPassRange.Text;
            else
                viewValue = Helper.DoubleToString(this.ParentForm.ParentForm.CurrentWeightData.Weight, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);

            beforeOver = this.buttonOverRange.Text;
            beforeUnder = this.buttonUnderRange.Text;
            before = this.buttonPassRange.Text;

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

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

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

            #region UnderRange
            temp = this.ParentForm.ParentForm.CurrentProductItem.PassRangeInt + oldUnderRangeDeviation;
            if (temp < 0)
                value = "0";
            else
                value = temp.ToString();

            this.ParentForm.ParentForm.CurrentProductItem.UnderRange = value;
            #endregion

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

            this.ParentForm.ParentForm.CurrentProductItem.OverRange = value;
            #endregion

            this.ParentForm.RescaleControl(this.ParentForm.ParentForm.CurrentProductItem);
            this.ParentForm.ParentForm.SaveProductFile(this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);

            this.ParentForm.ParentForm.Update30000ModbusItem();
            sb.Append(Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem.UnderRange));
            sb.Append(Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem.PassRange));
            sb.Append(Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem.OverRange));
            sb.Append(Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem.TareRange));
            sb.Append(Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.SystemConfig1.ProductNumber.ToString()));
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.ParameterWeightSetting, sb.ToString());

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

            if (this.ParentForm.ParentForm.ChildFormMainDisplay.MainFeedback.IsUsingFeedback() == true)
                this.ParentForm.ParentForm.UpdateFeedbackData(this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.CurrentSystemParameter2);

            after = this.buttonPassRange.Text = myKeyPad.StringValue;
            afterUnder = this.buttonUnderRange.Text = underRange;
            afterOver = this.buttonOverRange.Text = overRange;

            if (before != after)
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.PassRange, "", before, after);
            if (beforeUnder != afterUnder)
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.UnderRange, "Change Pass Value", beforeUnder, afterUnder);
            if (beforeOver != afterOver)
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.OverRange, "Change Pass Value", beforeOver, afterOver);
        }
        private void buttonOverRange_Click(object sender, EventArgs e)
        {
            // Automatic Logout Reset
            if (this.ParentForm.ParentForm.SystemConfig3.IsPart11 == true)
                this.ParentForm.Part11AutomaticLogoutReset();

            string value = "", sValue = "", before = "", after = "";
            double dValue = 0.0;

            before = this.buttonOverRange.Text;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.buttonOverRange.Text, 5, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces,
                false, this.ParentForm.ParentForm.SystemConfig1.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.CurrentProductItem.PassRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces)
                        + myKeyPad.doubleValue;
                    sValue = Helper.DoubleToString(dValue, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
                    break;
                default:
                    dValue = myKeyPad.doubleValue;
                    sValue = myKeyPad.StringValue;
                    break;
            }

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

                switch (this.ParentForm.ParentForm.CurrentSystemStatus.CurrentWeightInputMode)
                {
                    case Define.E_WeightInputMode.Weight:
                        value = myKeyPad.StringValue;
                        break;
                    case Define.E_WeightInputMode.Deviation:
                        if (myKeyPad.StringValue.Contains('+') == true)
                            temp = myKeyPad.StringValue.Replace("+", "");
                        else
                            temp = myKeyPad.StringValue;
                        value = string.Format("+{0}", temp);
                        break;
                    default:
                        value = myKeyPad.StringValue;
                        break;
                }
                after = this.buttonOverRange.Text = value;

                this.ParentForm.RescaleControl(this.ParentForm.ParentForm.CurrentProductItem);

                this.ParentForm.ParentForm.SaveProductFile(this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);

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

                    if (this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1IsWeightSetting == "0")
                    {
                        this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1OverRange = this.ParentForm.ParentForm.CurrentProductItem.OverRange;
                        this.ParentForm.ParentForm.CurrentFeedbackItem.OverRangeDeviation = this.ParentForm.ParentForm.CurrentProductItem.OverRangeDeviation.ToString();

                        this.ParentForm.ParentForm.SaveSystemParameter2File(this.ParentForm.ParentForm.CurrentSystemParameter2);
                        feedbackValue = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1OverRange);
                        this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.OPT1OverRange, feedbackValue);
                    }
                }

                this.ParentForm.ParentForm.Update30000ModbusItem();
                value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem.OverRange);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.OverRange, value);

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

        private void buttonTareRange_Click(object sender, EventArgs e)
        {
            // Automatic Logout Reset
            if (this.ParentForm.ParentForm.SystemConfig3.IsPart11 == true)
                this.ParentForm.Part11AutomaticLogoutReset();

            string value = "", before = "", after = "";

            before = this.buttonTareRange.Text;

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

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

                    this.ParentForm.ParentForm.SaveProductFile(this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);

                    // NET Icon 표시
                    this.ParentForm.MainWeightBigScreen.SetIconNET(this.ParentForm.ParentForm.CurrentProductItem);
                    this.ParentForm.MainWeightSmall.SetIconNET(this.ParentForm.ParentForm.CurrentProductItem);

                    this.ParentForm.ParentForm.Update30000ModbusItem();
                    value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentProductItem.TareRange);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.TareRange, value);

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