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 INT_PT002.Forms;
using INT_PT002.DataStore;
using INT_PT002.DialogForms;

namespace INT_PT002.Controls
{
    public partial class ControlMenuRecipeSetting : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;
        private Recipe SelectedRecipe;
        #endregion

        #region Constructor
        public ControlMenuRecipeSetting(FormMenu parent)
        {
            InitializeComponent();

            this.ParentForm = parent;
            this.Initialize();
        }
        #endregion

        #region Property
        public FormMenu ParentForm
        {
            get { return this.m_ParentForm; }
            private set { this.m_ParentForm = value; }
        }
        #endregion

        #region Method
        private void Initialize()
        {
            this.smartGroupBox1.Text = "Recipe > Setting";
            this.SelectedRecipe = new Recipe();
        }

        private void UpdateDisplayContnrolData(Recipe item)
        {
            string value = "";

            // Number
            value = item.NUMBER.ToString();
            if (this.labelNumber.Text != value)
                this.labelNumber.Text = value;
            // Pressure Level
            value = item.VACUUM_GUAGE_LEVEL;
            if (this.labelPressureLevel.Text != value)
                this.labelPressureLevel.Text = value;

            // Displacement Sensor
            // STD_Level
            value = item.DISP_JUDG_STD_LEVEL;
            if (this.labelDispStdLevel.Text != value)
                this.labelDispStdLevel.Text = value;
            // Min_Level
            value = item.DISP_JUDG_MIN_LEVEL;
            if (this.labelDispMinLevel.Text != value)
                this.labelDispMinLevel.Text = value;
            // Empty_Level
            value = item.DISP_JUDG_EMPTY_LEVEL;
            if (this.labelDispEmptyLevel.Text != value)
                this.labelDispEmptyLevel.Text = value;
            // Hold_Delay
            value = item.DISP_HOLD_DELAY_MSEC.ToString();
            if (this.labelDispHoldDelay.Text != value)
                this.labelDispHoldDelay.Text = value;

            // Differential Pressure Sensor
            // LR_Second
            value = item.DIFF_LR_SECOND;
            if (this.labelDiffLrSecond.Text != value)
                this.labelDiffLrSecond.Text = value;
            // LR_Total
            value = item.DIFF_LR_TOTAL;
            if (this.labelDiffLrTotal.Text != value)
                this.labelDiffLrTotal.Text = value;
            // LR_Mean
            value = item.DIFF_LR_MEAN;
            if (this.labelDiffLrMean.Text != value)
                this.labelDiffLrMean.Text = value;
            // Hold_Delay
            value = item.DIFF_HOLD_DELAY_MSEC.ToString();
            if (this.labelDiffHoldDelay.Text != value)
                this.labelDiffHoldDelay.Text = value;

            // Pressure data
            // Vacuum Relief
            value = item.VACUUM_RELIEF.ToString();
            if (this.labelVacuumRelief.Text != value)
                this.labelVacuumRelief.Text = value;
            // Vacuum Hold1
            value = item.VACUUM_HOLD1.ToString();
            if (this.labelVacuumHold1.Text != value)
                this.labelVacuumHold1.Text = value;
            // Vacuum Hold2
            value = item.VACUUM_HOLD2.ToString();
            if (this.labelVacuumHold2.Text != value)
                this.labelVacuumHold2.Text = value;
            // Vacuum Blowoff
            value = item.VACUUM_BLOWOFF.ToString();
            if (this.labelVacuumBlowoff.Text != value)
                this.labelVacuumBlowoff.Text = value;
        }

        public void UpdateDisplayRecipeData(int no)
        {
            Recipe item = new Recipe();

            this.ParentForm.ParentForm.LoadRecipeFile(ref item, no - 1);

            this.SelectedRecipe.NUMBER = item.NUMBER;
            this.SelectedRecipe.NAME = item.NAME;
            this.SelectedRecipe.LOT = item.LOT;

            this.SelectedRecipe.DISP_JUDG_STD_LEVEL = item.DISP_JUDG_STD_LEVEL;
            this.SelectedRecipe.DISP_JUDG_MIN_LEVEL = item.DISP_JUDG_MIN_LEVEL;
            this.SelectedRecipe.DISP_JUDG_EMPTY_LEVEL = item.DISP_JUDG_EMPTY_LEVEL;
            this.SelectedRecipe.DISP_HOLD_DELAY_MSEC = item.DISP_HOLD_DELAY_MSEC;

            this.SelectedRecipe.DIFF_LR_SECOND = item.DIFF_LR_SECOND;
            this.SelectedRecipe.DIFF_LR_TOTAL = item.DIFF_LR_TOTAL;
            this.SelectedRecipe.DIFF_LR_MEAN = item.DIFF_LR_MEAN;
            this.SelectedRecipe.DIFF_HOLD_DELAY_MSEC = item.DIFF_HOLD_DELAY_MSEC;

            this.SelectedRecipe.VACUUM_RELIEF = item.VACUUM_RELIEF;
            this.SelectedRecipe.VACUUM_HOLD1 = item.VACUUM_HOLD1;
            this.SelectedRecipe.VACUUM_HOLD2 = item.VACUUM_HOLD2;
            this.SelectedRecipe.VACUUM_BLOWOFF = item.VACUUM_BLOWOFF;
            this.SelectedRecipe.VACUUM_GUAGE_LEVEL = item.VACUUM_GUAGE_LEVEL;

            this.UpdateDisplayContnrolData(this.SelectedRecipe);
        }

        public void DisplayRefresh()
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = Define.E_FormStore.FormProduct;
            this.ParentForm.ParentForm.SetDisplayMode(Define.E_DisplayMode.Menu);
            this.UpdateDisplayRecipeData(this.ParentForm.ParentForm.SystemConfig.RECIPE_NUMBER);
        }
        #endregion

        #region Event Handler
        private void labelNumber_Click(object sender, EventArgs e)
        {

        }
        private void labelPressureLevel_Click(object sender, EventArgs e)
        {
            string value = "";

            DialogFormNumKeyPad myKeypad = new DialogFormNumKeyPad(this.smartLabel15.Text, this.labelPressureLevel.Text, 5, 1, false);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                this.labelPressureLevel.Text = myKeypad.StringValue;
                this.SelectedRecipe.VACUUM_GUAGE_LEVEL = myKeypad.StringValue;
                this.ParentForm.ParentForm.SaveRecipeFile(this.SelectedRecipe, this.SelectedRecipe.NUMBER - 1);

                value = Helper.StringBlankFillDigits6(myKeypad.StringValue);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._5517_VacuumGaugeStdLevel, value);
            }
        }

        // 밸브
        private void labelVacuumRelief_Click(object sender, EventArgs e)
        {
            string value = "";

            DialogFormNumKeyPad myKeypad = new DialogFormNumKeyPad(this.smartLabel22.Text, this.labelVacuumRelief.Text, 4, 0, false);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                this.labelVacuumRelief.Text = myKeypad.StringValue;
                this.SelectedRecipe.VACUUM_RELIEF = myKeypad.IntValue;
                this.ParentForm.ParentForm.SaveRecipeFile(this.SelectedRecipe, this.SelectedRecipe.NUMBER - 1);

                value = Helper.StringBlankFillDigits4(myKeypad.StringValue);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._5504_VacuumRelief, value);
            }
        }
        private void labelVacuumHold1_Click(object sender, EventArgs e)
        {
            string value = "";

            DialogFormNumKeyPad myKeypad = new DialogFormNumKeyPad(this.smartLabel24.Text, this.labelVacuumHold1.Text, 4, 0, false);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                this.labelVacuumHold1.Text = myKeypad.StringValue;
                this.SelectedRecipe.VACUUM_HOLD1 = myKeypad.IntValue;
                this.ParentForm.ParentForm.SaveRecipeFile(this.SelectedRecipe, this.SelectedRecipe.NUMBER - 1);

                value = Helper.StringBlankFillDigits4(myKeypad.StringValue);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._5505_VacuumHold1, value);
            }
        }
        private void labelVacuumHold2_Click(object sender, EventArgs e)
        {
            string value = "";

            DialogFormNumKeyPad myKeypad = new DialogFormNumKeyPad(this.smartLabel26.Text, this.labelVacuumHold2.Text, 4, 0, false);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                this.labelVacuumHold2.Text = myKeypad.StringValue;
                this.SelectedRecipe.VACUUM_HOLD2 = myKeypad.IntValue;
                this.ParentForm.ParentForm.SaveRecipeFile(this.SelectedRecipe, this.SelectedRecipe.NUMBER - 1);

                value = Helper.StringBlankFillDigits4(myKeypad.StringValue);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._5506_VacuumHold2, value);
            }
        }
        private void labelVacuumBlowoff_Click(object sender, EventArgs e)
        {
            string value = "";

            DialogFormNumKeyPad myKeypad = new DialogFormNumKeyPad(this.smartLabel2.Text, this.labelVacuumBlowoff.Text, 4, 0, false);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                this.labelVacuumBlowoff.Text = myKeypad.StringValue;
                this.SelectedRecipe.VACUUM_BLOWOFF = myKeypad.IntValue;
                this.ParentForm.ParentForm.SaveRecipeFile(this.SelectedRecipe, this.SelectedRecipe.NUMBER - 1);

                value = Helper.StringBlankFillDigits4(myKeypad.StringValue);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._5507_VacuumBlowoff, value);
            }
        }

        // 변위센서
        private void labelDispStdLevel_Click(object sender, EventArgs e)
        {
            string value = "";

            DialogFormNumKeyPad myKeypad = new DialogFormNumKeyPad(this.smartLabel9.Text, this.labelDispStdLevel.Text, 5, 2, false);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                this.labelDispStdLevel.Text = myKeypad.StringValue;
                this.SelectedRecipe.DISP_JUDG_STD_LEVEL = myKeypad.StringValue;
                this.ParentForm.ParentForm.SaveRecipeFile(this.SelectedRecipe, this.SelectedRecipe.NUMBER - 1);

                value = Helper.StringBlankFillDigits6(myKeypad.StringValue);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._5509_DispStdLevel, value);
            }
        }
        private void labelDispMinLevel_Click(object sender, EventArgs e)
        {
            string value = "";

            DialogFormNumKeyPad myKeypad = new DialogFormNumKeyPad(this.smartLabel7.Text, this.labelDispMinLevel.Text, 5, 2, false);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                this.labelDispMinLevel.Text = myKeypad.StringValue;
                this.SelectedRecipe.DISP_JUDG_MIN_LEVEL = myKeypad.StringValue;
                this.ParentForm.ParentForm.SaveRecipeFile(this.SelectedRecipe, this.SelectedRecipe.NUMBER - 1);

                value = Helper.StringBlankFillDigits6(myKeypad.StringValue);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._5510_DispMinLevel, value);
            }
        }
        private void labelDispEmptyLevel_Click(object sender, EventArgs e)
        {
            string value = "";

            DialogFormNumKeyPad myKeypad = new DialogFormNumKeyPad(this.smartLabel5.Text, this.labelDispEmptyLevel.Text, 5, 2, false);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                this.labelDispEmptyLevel.Text = myKeypad.StringValue;
                this.SelectedRecipe.DISP_JUDG_EMPTY_LEVEL = myKeypad.StringValue;
                this.ParentForm.ParentForm.SaveRecipeFile(this.SelectedRecipe, this.SelectedRecipe.NUMBER - 1);

                value = Helper.StringBlankFillDigits6(myKeypad.StringValue);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._5511_DispEmptyLevel, value);
            }
        }
        private void labelDispHoldDelay_Click(object sender, EventArgs e)
        {
            string value = "";

            DialogFormNumKeyPad myKeypad = new DialogFormNumKeyPad(this.smartLabel3.Text, this.labelDispHoldDelay.Text, 4, 0, false);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                this.labelDispHoldDelay.Text = myKeypad.StringValue;
                this.SelectedRecipe.DISP_HOLD_DELAY_MSEC = myKeypad.IntValue;
                this.ParentForm.ParentForm.SaveRecipeFile(this.SelectedRecipe, this.SelectedRecipe.NUMBER - 1);

                value = Helper.StringBlankFillDigits4(myKeypad.StringValue);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._5512_DispHoldDelay, value);
            }
        }

        // 차압센서
        private void labelDiffLrSecond_Click(object sender, EventArgs e)
        {
            string value = "";

            DialogFormNumKeyPad myKeypad = new DialogFormNumKeyPad(this.smartLabel13.Text, this.labelDiffLrSecond.Text, 7, 2, false);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                this.labelDiffLrSecond.Text = myKeypad.StringValue;
                this.SelectedRecipe.DIFF_LR_SECOND = myKeypad.StringValue;
                this.ParentForm.ParentForm.SaveRecipeFile(this.SelectedRecipe, this.SelectedRecipe.NUMBER - 1);

                value = Helper.StringBlankFillDigits8(myKeypad.StringValue);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._5513_DiffLrSecond, value);
            }
        }
        private void labelDiffLrTotal_Click(object sender, EventArgs e)
        {
            string value = "";

            DialogFormNumKeyPad myKeypad = new DialogFormNumKeyPad(this.smartLabel11.Text, this.labelDiffLrTotal.Text, 7, 2, false);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                this.labelDiffLrTotal.Text = myKeypad.StringValue;
                this.SelectedRecipe.DIFF_LR_TOTAL = myKeypad.StringValue;
                this.ParentForm.ParentForm.SaveRecipeFile(this.SelectedRecipe, this.SelectedRecipe.NUMBER - 1);

                value = Helper.StringBlankFillDigits8(myKeypad.StringValue);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._5514_DiffLrTotal, value);
            }
        }
        private void labelDiffLrMean_Click(object sender, EventArgs e)
        {
            string value = "";

            DialogFormNumKeyPad myKeypad = new DialogFormNumKeyPad(this.smartLabel8.Text, this.labelDiffLrMean.Text, 7, 2, false);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                this.labelDiffLrMean.Text = myKeypad.StringValue;
                this.SelectedRecipe.DIFF_LR_MEAN = myKeypad.StringValue;
                this.ParentForm.ParentForm.SaveRecipeFile(this.SelectedRecipe, this.SelectedRecipe.NUMBER - 1);

                value = Helper.StringBlankFillDigits8(myKeypad.StringValue);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._5515_DiffLrMean, value);
            }
        }
        private void labelDiffHoldDelay_Click(object sender, EventArgs e)
        {
            string value = "";

            DialogFormNumKeyPad myKeypad = new DialogFormNumKeyPad(this.smartLabel4.Text, this.labelDiffHoldDelay.Text, 4, 0, false);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                this.labelDiffHoldDelay.Text = myKeypad.StringValue;
                this.SelectedRecipe.DIFF_HOLD_DELAY_MSEC = myKeypad.IntValue;
                this.ParentForm.ParentForm.SaveRecipeFile(this.SelectedRecipe, this.SelectedRecipe.NUMBER - 1);

                value = Helper.StringBlankFillDigits4(myKeypad.StringValue);
                this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress._5516_DiffHoldDelay, value);
            }
        }
        #endregion
    }
}
