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.Forms;
using ITC81DB_ImageDll;
using ITC81DB.DialogForms;

namespace ITC81DB
{
    public partial class ControlMainFeedback : UserControl
    {
        #region Field
        private FormMainDisplay m_ParentForm;
        #endregion

        #region Constructor
        public ControlMainFeedback(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 == DataStore.LanguageID.English)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engMainFeedbackScreen));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnMainFeedbackScreen));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerMainFeedbackScreen));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Spanish)
            {
                this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engMainFeedbackScreen));
            }
            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)
        {
            string value = "";
            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
                {
                    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);
                }
            }
        }
        private void labelDelayCount_Click(object sender, EventArgs e)
        {
            string value = "";
            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
                {
                    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);
                }
            }
        }

        private void labelUpperLimit_Click(object sender, EventArgs e)
        {
            string value = "";
            int digit = 0;

            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
                {
                    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);
                }
            }
        }
        private void labelLowerLimit_Click(object sender, EventArgs e)
        {
            string value = "";
            int digit = 0;

            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
                {
                    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);
                }
            }
        }

        private void labelUpperLimitDev_Click(object sender, EventArgs e)
        {
            string value = "", sValue = "";
            double dValue = 0.0;
            int digit = 0;

            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 = "";
                    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);
                }
            }
        }
        private void labelLowerLimitDev_Click(object sender, EventArgs e)
        {
            string value = "", sValue = "";
            double dValue = 0.0;
            int digit = 0;

            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
                {
                    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);
                }
            }
        }
        #endregion
    }
}