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

using ITC81DB_2H.Forms;
using ITC81DB_2H_ImageDll;
using ITC81DB_2H.DialogForms;
using ITC81DB_2H.Datastore;

namespace ITC81DB_2H
{
    public partial class ControlMainDisplayFeedback : UserControl
    {
        #region Field
        private FormMainDisplay m_ParentForm;
        #endregion

        #region Constructor
        public ControlMainDisplayFeedback(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.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engMainFeedbackScreen));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainFeedbackScreen));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainFeedbackScreen));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
            {

            }
            else
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korMainFeedbackScreen));
            }
        }

        public bool IsUsingFeedback()
        {
            int value = int.Parse(this.ParentForm.ParentForm.CurrentSystemParameter1.OptionBoard);
            if (value == 1 && int.Parse(this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1SamplingCount) != 0)
                return true;
            else
                return false;
        }
        public string CaculateDeviation(int value)
        {
            string sValue = "";

            sValue = (value - this.ParentForm.ParentForm.CurrentProductItem.PassRangeInt).ToString();

            return sValue;
        }

        public void ItemChangeEnable(bool value)
        {
            if (value == true)
            {
                this.labelSampleCount.ForeColor = Color.White;
                this.labelDelayCount.ForeColor = Color.White;
                this.labelUpperLimit.ForeColor = Color.White;
                this.labelLowerLimit.ForeColor = Color.White;
                this.labelUpperLimitDev.ForeColor = Color.White;
                this.labelLowerLimitDev.ForeColor = Color.White;
            }
            else
            {
                this.labelSampleCount.ForeColor = Color.DarkGray;
                this.labelDelayCount.ForeColor = Color.DarkGray;
                this.labelUpperLimit.ForeColor = Color.DarkGray;
                this.labelLowerLimit.ForeColor = Color.DarkGray;
                this.labelUpperLimitDev.ForeColor = Color.DarkGray;
                this.labelLowerLimitDev.ForeColor = Color.DarkGray;
            }

            this.labelSampleCount.Enabled = value;
            this.labelDelayCount.Enabled = value;
            this.labelUpperLimit.Enabled = value;
            this.labelLowerLimit.Enabled = value;
            this.labelUpperLimitDev.Enabled = value;
            this.labelLowerLimitDev.Enabled = value;
        }
        public void UpdateDisplayCurrentValue(FeedbackItem parameter)
        {
            string sValue = "";

            sValue = parameter.SampleCount.Trim();
            if (this.labelCurrentSampleCount.Text != sValue)
                this.labelCurrentSampleCount.Text = sValue;

            sValue = parameter.DelayCount.Trim();
            if (this.labelCurrentDelayCount.Text != sValue)
                this.labelCurrentDelayCount.Text = sValue;

            sValue = Helper.StringToDecimalPlaces(parameter.FeedbackWeight.Trim(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            sValue = parameter.Sign.Trim() + sValue;
            if (this.labelCurrentFeedback.Text != sValue)
                this.labelCurrentFeedback.Text = sValue;
        }
        private void UpdateDisplay(SystemParameter2 parameter)
        {
            string sValue = "";

            // 샘플 개수
            sValue = parameter.OPT1SamplingCount;
            if (this.labelSampleCount.Text != sValue)
                this.labelSampleCount.Text = sValue;

            // 지연 개수
            sValue = parameter.OPT1DelayCount;
            if (this.labelDelayCount.Text != sValue)
                this.labelDelayCount.Text = sValue;

            // 기준
            sValue = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentProductItem.PassRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            if (this.labelStandard1.Text != sValue)
                this.labelStandard1.Text = sValue;

            // 피드백 상한
            sValue = Helper.StringToDecimalPlaces(parameter.OPT1OverRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            if (this.labelUpperLimit.Text != sValue)
                this.labelUpperLimit.Text = sValue;

            // 피드백 하한
            sValue = Helper.StringToDecimalPlaces(parameter.OPT1UnderRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            if (this.labelLowerLimit.Text != sValue)
                this.labelLowerLimit.Text = sValue;

            // 상한편차
            sValue = '+' + Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentFeedbackItem.OverRangeDeviation, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            if (this.labelUpperLimitDev.Text != sValue)
                this.labelUpperLimitDev.Text = sValue;

            // 하한편차
            sValue = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentFeedbackItem.UnderRangeDeviation, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            if (this.labelLowerLimitDev.Text != sValue)
                this.labelLowerLimitDev.Text = sValue;

            // 사용/미사용
            sValue = parameter.OPT1IsWeightSetting;
            if (int.Parse(sValue) == 0)
            {
                this.labelUpperLimit.ForeColor = Color.DarkGray;
                this.labelLowerLimit.ForeColor = Color.DarkGray;
                this.labelUpperLimitDev.ForeColor = Color.DarkGray;
                this.labelLowerLimitDev.ForeColor = Color.DarkGray;
                this.labelUpperLimit.Enabled = false;
                this.labelLowerLimit.Enabled = false;
                this.labelUpperLimitDev.Enabled = false;
                this.labelLowerLimitDev.Enabled = false;
            }
            else
            {
                this.labelUpperLimit.ForeColor = Color.White;
                this.labelLowerLimit.ForeColor = Color.White;
                this.labelUpperLimitDev.ForeColor = Color.White;
                this.labelLowerLimitDev.ForeColor = Color.White;
                this.labelUpperLimit.Enabled = true;
                this.labelLowerLimit.Enabled = true;
                this.labelUpperLimitDev.Enabled = true;
                this.labelLowerLimitDev.Enabled = true;
            }
        }
        private void DefaultSetting()
        {
        }
        public void DisplayRefresh(SystemStatus status)
        {
            this.UpdateDisplay(this.ParentForm.ParentForm.CurrentSystemParameter2);
            this.UpdateDisplayCurrentValue(this.ParentForm.ParentForm.CurrentFeedbackItem);
        }
        #endregion

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

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

            before = this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1SamplingCount;

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

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

                    value = Helper.StringZeroFillDigits4(this.labelSampleCount.Text);
                    this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1SamplingCount = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.SaveSystemParameter2File(this.ParentForm.ParentForm.CurrentSystemParameter2);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.OPT1SamplingCount, value);

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

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

            before = this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1DelayCount;

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

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

                    value = Helper.StringZeroFillDigits4(this.labelDelayCount.Text);
                    this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1DelayCount = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.SaveSystemParameter2File(this.ParentForm.ParentForm.CurrentSystemParameter2);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.OPT1DelayCount, value);

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

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

            string value = "";
            string before = "", after = "";
            int digit = 0;

            before = this.labelUpperLimit.Text;

            if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 0)
                digit = 4;
            // V6.2.0
            //else if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 1)
            //    digit = 5;
            //else if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 2)
            //    digit = 6;
            else
                digit = 5;

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem.PassRange.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces))
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    after = this.labelUpperLimit.Text = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1OverRange = myKeyPad.StringValue.Replace(".", "");

                    this.ParentForm.ParentForm.CurrentFeedbackItem.OverRangeDeviation =
                       this.ParentForm.ParentForm.ChildFormMainDisplay.MainFeedback.CaculateDeviation(this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1OverRangeInt);
                    this.labelUpperLimitDev.Text = '+' + Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentFeedbackItem.OverRangeDeviation, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);

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

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

            string value = "";
            string before = "", after = "";
            int digit = 0;

            before = this.labelLowerLimit.Text;

            if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 0)
                digit = 4;
            // V6.2.0
            //else if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 1)
            //    digit = 5;
            //else if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 2)
            //    digit = 6;
            else
                digit = 5;

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue > Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem.PassRange.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces))
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    after = this.labelLowerLimit.Text = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1UnderRange = myKeyPad.StringValue.Replace(".", "");

                    this.ParentForm.ParentForm.CurrentFeedbackItem.UnderRangeDeviation =
    this.ParentForm.ParentForm.ChildFormMainDisplay.MainFeedback.CaculateDeviation(this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1UnderRangeInt);
                    this.labelLowerLimitDev.Text = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentFeedbackItem.UnderRangeDeviation, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);

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

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

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

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

            before = this.labelUpperLimit.Text;

            if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 0)
                digit = 4;
            // V6.2.0
            //else if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 1)
            //    digit = 5;
            //else if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 2)
            //    digit = 6;
            else
                digit = 5;

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                dValue = Helper.StringToWeight(this.ParentForm.ParentForm.CurrentProductItem.PassRange.ToString(), this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces)
                            + myKeyPad.doubleValue;
                sValue = Helper.DoubleToString(dValue, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);

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

                    if (myKeyPad.StringValue.Contains('+') == true)
                        temp = myKeyPad.StringValue.Replace("+", "");
                    else
                        temp = myKeyPad.StringValue;
                    value = string.Format("+{0}", temp);

                    this.labelUpperLimitDev.Text = value;
                    this.labelUpperLimit.Text = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1OverRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);

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

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

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

            before = this.labelLowerLimit.Text;

            if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 0)
                digit = 4;
            // V6.2.0
            //else if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 1)
            //    digit = 5;
            //else if (this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces == 2)
            //    digit = 6;
            else
                digit = 5;

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                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);
                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
                {
                    after = sValue;
                    this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1UnderRange = sValue.Replace(".", "");

                    if (myKeyPad.doubleValue == 0)
                        value = string.Format("-{0}", Helper.DoubleToString(0, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces));
                    else
                        value = myKeyPad.StringValue;

                    this.labelLowerLimitDev.Text = value;
                    this.labelLowerLimit.Text = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentSystemParameter2.OPT1UnderRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);

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

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