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

namespace GcDevicePc.ConfigMethod.injectionport
{
    public partial class IPPIP : Form
    {
        HWConfig hwconfig;
        GCMethodConfig methodconfig;

        //private int indexOld = 0;

        public IPPIP(HWConfig hwconfig, ref GCMethodConfig methodconfig)
        {
            InitializeComponent();

            this.hwconfig = hwconfig;
            this.methodconfig = methodconfig;
        }

        int configTemp;

        public void reflashContent(int index)
        {
            this.methodconfig.GetDevInjPortAllPara();

            label1.Text = "进样口类型：";
            label21.Text = "进样口载气：";

            switch (index)
            {
                case 0:
                    configTemp = globaldata.m_hwconfig.hwconfiginfo.u16InjPortFPTemp;
                    break;
                case 1:
                    configTemp = globaldata.m_hwconfig.hwconfiginfo.u16InjPortBPTemp;
                    break;
                default:
                    break;
            }

            txtHeater.Text = this.methodconfig.port[index].HeaterTmpActual.ToString();

            textBox1.Text = configTemp.ToString();

            if (this.hwconfig.hwconfiginfo.u16StartType == 1 || this.hwconfig.hwconfiginfo.u16StartType == 3)
            {
                txtFlow.Text = this.methodconfig.port[index].pipFlowActual.ToString();
                txtPressure.Text = this.methodconfig.port[index].pipPressureActual.ToString();
                txtSeptumBlowingflow.Text = this.methodconfig.port[index].pipSeptumBlowFlowActual.ToString();
            }
            else
            {
                txtFlow.Enabled = false;
                txtPressure.Enabled = false;
                txtSeptumBlowingflow.Enabled = false;
            }
            

            //txtSamplePortType.Text = (this.methodconfig.auxs[index].SamplePortType == 0? "pip":"spl");
            lblSamplePortType.Text = "PIP进样口";
            //txtSampleProtecttemp.Text = this.methodconfig.port[index].Protecttemp.ToString();

            //combControlMode.SelectedIndex = this.methodconfig.port[index].ControlMode;
            try
            {
                combSampleCarrierGas.SelectedIndex = this.methodconfig.port[index].CarrierGasType;

            }
            catch
            {
            }

            if (this.methodconfig.port[index].Heater == 1)
                cbHeater.CheckState = CheckState.Checked;
            else
                cbHeater.CheckState = CheckState.Unchecked;

            //if (this.methodconfig.port[index].pipSeptumBlowFlow == 1)
            //    cbSeptumBlowingflow.CheckState = CheckState.Checked;
            //else
            //    cbSeptumBlowingflow.CheckState = CheckState.Unchecked;

            //txtPressure.Enabled = combControlMode.SelectedIndex == 0 ? true : false;
            //txtFlow.Enabled = combControlMode.SelectedIndex == 1 ? true : false;

            //cbtype1.SelectedIndex = this.methodconfig.epc[index].gasType1;
            //cbtype2.SelectedIndex = this.methodconfig.epc[index].gasType2;
            //cbtype3.SelectedIndex = this.methodconfig.epc[index].gasType3;

            #region 旧代码

            //int index;

            //if (this.hwconfig.hwconfiginfo.u16InjPortNum > 0)
            //{
            //    if (this.hwconfig.hwconfiginfo.u16StartType != 2 || this.hwconfig.hwconfiginfo.u16StartType != 1)
            //    {
            //        this.cargasgroup.Enabled = true;
            //    }
            //    else
            //    {
            //        this.cargasgroup.Enabled = false;
            //    }

            //    InjPortBox.Items.Clear();
            //    Shuntmode.Items.Clear();

            //    this.InjPortBox.Items.Add("前进样口");
            //    this.InjPortBox.Items.Add("后进样口");
            //    this.Shuntmode.Items.Add("分流");
            //    this.Shuntmode.Items.Add("不分流");
            //    Shuntmode.SelectedIndex = 0;
            //    InjPortBox.SelectedIndex = 0;

            //    if (this.hwconfig.hwconfiginfo.u16InjPortNum < 2)
            //        InjPortBox.Enabled = false;

            //    if (this.hwconfig.hwconfiginfo.u16InjPortFront == 1)
            //    {
            //        indexOld = 0;
            //    }
            //    else if (this.hwconfig.hwconfiginfo.u16InjPortBehind == 1)
            //    {
            //        indexOld = 1;
            //    }
            //    else
            //    {
            //        indexOld = 0;
            //    }

            //    index = indexOld;

            //    tb加热.Text = this.methodconfig.auxs[index].fHeaterSetTemp.ToString();
            //    tb压力设定.Text = this.methodconfig.auxs[index].fSetPre.ToString();
            //    tb总流量设定.Text = this.methodconfig.auxs[index].fSetSumFlow.ToString();
            //    tb柱流量.Text = this.methodconfig.auxs[index].fSetColumnFlow.ToString();
            //    tb吹扫流量.Text = this.methodconfig.auxs[index].fSetPurgeFlow.ToString();

            //    if (this.methodconfig.auxs[index].bHeater == true)
            //        cBox加热器.CheckState = CheckState.Checked;
            //    else
            //        cBox加热器.CheckState = CheckState.Unchecked;

            //    ShuntPer.Text = this.methodconfig.auxs[index].u16SplitRatio.ToString();
            //    ShuntFlow.Text = this.methodconfig.auxs[index].fSplitFlow.ToString();
            //    CarrierGas1.Text = this.methodconfig.auxs[index].fCarrierGas1.ToString();
            //    CarrierGas2.Text = this.methodconfig.auxs[index].fCarrierGas2.ToString();
            //    CarrierGas3.Text = this.methodconfig.auxs[index].fCarrierGas3.ToString();

            //    if (this.methodconfig.auxs[index].bCarrierGasSave == true)
            //        cBox省EPC.CheckState = CheckState.Checked;
            //    else
            //        cBox省EPC.CheckState = CheckState.Unchecked;

            //    cargasthrift.Text = this.methodconfig.auxs[index].u16CarrierGasSave.ToString();
            //    tb等待时间.Text = this.methodconfig.auxs[index].fSaveGasTime.ToString();

            //    this.InjPortBox.SelectedIndex = indexOld;

            //    this.groupBox1.Enabled = true;
            //    this.InjPortBox.Enabled = true;
            //    this.Shuntmode.Enabled = true;
            //    this.Shuntmode.Enabled = true;
            //    this.ShuntPer.Enabled = true;
            //    this.ShuntFlow.Enabled = true;
            //    this.CarrierGas1.Enabled = true;
            //    this.CarrierGas2.Enabled = true;
            //    this.CarrierGas3.Enabled = true;
            //}
            //else
            //{
            //    this.groupBox1.Enabled = false;
            //    this.InjPortBox.Enabled = false;
            //    this.Shuntmode.Enabled = false;
            //    this.Shuntmode.Enabled = false;
            //    this.ShuntPer.Enabled = false;
            //    this.ShuntFlow.Enabled = false;
            //    this.CarrierGas1.Enabled = false;
            //    this.CarrierGas2.Enabled = false;
            //    this.CarrierGas3.Enabled = false;
            //    this.cargasgroup.Enabled = false;
            //}

            #endregion
        }
        /// <summary>
        /// 辅助显示
        /// </summary>
        /// <param name="v"></param>
        public void freflashContent(int index)
        {
            this.methodconfig.GetDevAuxs();

            label1.Text = "辅助名：";
            label21.Text = "辅助载气类型：";

            switch (index)
            {
                case 0:
                    configTemp = globaldata.m_hwconfig.hwconfiginfo.u16AuxHeaterPTemp1;
                    break;
                case 1:
                    configTemp = globaldata.m_hwconfig.hwconfiginfo.u16AuxHeaterPTemp2;
                    break;
                default:
                    break;
            }
            
            if (this.hwconfig.hwconfiginfo.u16StartType == 1 || this.hwconfig.hwconfiginfo.u16StartType == 3)
            {
                txtFlow.Text = this.methodconfig.auxs[index].pipFlowActual.ToString();
                txtPressure.Text = this.methodconfig.auxs[index].pipPressureActual.ToString();
                txtSeptumBlowingflow.Text = this.methodconfig.auxs[index].pipSeptumBlowFlowActual.ToString();
            }
            else
            {
                txtFlow.Enabled = false;
                txtPressure.Enabled = false;
                txtSeptumBlowingflow.Enabled = false;
            }

            textBox1.Text = configTemp.ToString();

            //txtFlow.Text = this.methodconfig.auxs[index].pipFlowActual.ToString();
            txtHeater.Text = this.methodconfig.auxs[index].HeaterTmpActual.ToString();
            //txtPressure.Text = this.methodconfig.auxs[index].pipPressureActual.ToString();
            lblSamplePortType.Text = (index == 0 ? this.hwconfig.hwconfiginfo.u16AuxHeaterName1 : this.hwconfig.hwconfiginfo.u16AuxHeaterName2);
            //txtSeptumBlowingflow.Text = this.methodconfig.auxs[index].pipSeptumBlowFlowActual.ToString();
            //txtSampleProtecttemp.Text = this.methodconfig.auxs[index].Protecttemp.ToString();

            //combControlMode.SelectedIndex = this.methodconfig.auxs[index].ControlMode;
            combSampleCarrierGas.SelectedIndex = this.methodconfig.auxs[index].CarrierGasType;

            if (this.methodconfig.auxs[index].Heater == 1)
                cbHeater.CheckState = CheckState.Checked;
            else
                cbHeater.CheckState = CheckState.Unchecked;

            //if (this.methodconfig.auxs[index].pipSeptumBlowFlow == 1)
            //    cbSeptumBlowingflow.CheckState = CheckState.Checked;
            //else
            //    cbSeptumBlowingflow.CheckState = CheckState.Unchecked;

            //txtPressure.Enabled = combControlMode.SelectedIndex == 0 ? true : false;
            //txtFlow.Enabled = combControlMode.SelectedIndex == 1 ? true : false;
        }
        /// <summary>
        /// 辅助设置
        /// </summary>
        /// <param name="v"></param>
        public void frecordCurData(int index)
        {
            this.methodconfig.auxs[index].pipFlowSet = string2float(txtFlow.Text);
            this.methodconfig.auxs[index].HeaterTmpSet = string2float(txtHeater.Text);
            this.methodconfig.auxs[index].pipPressureSet = string2float(txtPressure.Text);
            this.methodconfig.auxs[index].pipSeptumBlowFlowSet = string2float(txtSeptumBlowingflow.Text);
            this.methodconfig.auxs[index].SamplePortType = lblSamplePortType.Text;
            this.methodconfig.auxs[index].CarrierGasType = (ushort)combSampleCarrierGas.SelectedIndex;

            //this.methodconfig.auxs[index].Protecttemp  = 

            if (cbHeater.CheckState == CheckState.Checked)
                this.methodconfig.auxs[index].Heater = 1;
            else
                this.methodconfig.auxs[index].Heater = 0;

            //if (cbSeptumBlowingflow.CheckState == CheckState.Checked)
            //    this.methodconfig.auxs[index].pipSeptumBlowFlow = 1;
            //else
            //    this.methodconfig.auxs[index].pipSeptumBlowFlow = 0;
            this.methodconfig.auxs[index].pipSeptumBlowFlow = 1;


            this.methodconfig.epc[1].fristFlow = string2float(txtFlow.Text);
            this.methodconfig.epc[1].gasType1 = (ushort)0;
            this.methodconfig.epc[1].SecondFlow = string2float(txtPressure.Text);
            this.methodconfig.epc[1].gasType2 = (ushort)0;
            this.methodconfig.epc[1].ThirdFlow = string2float(txtSeptumBlowingflow.Text);
            this.methodconfig.epc[1].gasType3 = (ushort)0;

        }

        float string2float(string s)
        {
            float ret;

            if (s == "" || s == null)
            {
                ret = 0;
            }
            else
            {
                ret = float.Parse(s);
            }

            return ret;
        }

        public void recordCurData(int index)
        {
            this.methodconfig.port[index].pipFlowSet = string2float(txtFlow.Text);
            this.methodconfig.port[index].HeaterTmpSet = string2float(txtHeater.Text);
            this.methodconfig.port[index].pipPressureSet = string2float(txtPressure.Text);
            this.methodconfig.port[index].pipSeptumBlowFlowSet = string2float(txtSeptumBlowingflow.Text);
            //this.methodconfig.port[index].SamplePortType = (ushort)(lblSamplePortType.Text == "pip" ? 0 : 1);
            this.methodconfig.port[index].SamplePortType = "0";
            this.methodconfig.port[index].CarrierGasType = (ushort)combSampleCarrierGas.SelectedIndex;

            if (cbHeater.CheckState == CheckState.Checked)
                this.methodconfig.port[index].Heater = 1;
            else
                this.methodconfig.port[index].Heater = 0;

            //if (cbSeptumBlowingflow.CheckState == CheckState.Checked)
            //    this.methodconfig.port[index].pipSeptumBlowFlow = 1;
            //else
            //    this.methodconfig.port[index].pipSeptumBlowFlow = 0;
            this.methodconfig.port[index].pipSeptumBlowFlow = 1;


            #region 旧代码

            //int index;
            //index = indexOld;

            //if (index < 0)
            //    return;

            //if (this.hwconfig.hwconfiginfo.u16InjPortFront == 1 && index == 0)
            //{

            //}
            //else if (this.hwconfig.hwconfiginfo.u16InjPortBehind == 1 && index == 1)
            //{

            //}
            //else
            //{
            //    return;
            //}

            //if (tb加热.Text != "")
            //{
            //    this.methodconfig.port[index].fHeaterSetTemp = float.Parse(tb加热.Text);
            //    this.methodconfig.port[index].fHeaterSetTempm = float.Parse(tb加热.Text);
            //    this.methodconfig.port[index].fSetPre = float.Parse(tb压力设定.Text);
            //    this.methodconfig.port[index].fSetSumFlow = float.Parse(tb总流量设定.Text);
            //    this.methodconfig.port[index].fSetColumnFlow = float.Parse(tb柱流量.Text);
            //    this.methodconfig.port[index].fSetPurgeFlow = float.Parse(tb吹扫流量.Text);

            //    if (cBox加热器.CheckState == CheckState.Checked)
            //        this.methodconfig.port[index].bHeater = true;
            //    else
            //        this.methodconfig.port[index].bHeater = false;

            //    this.methodconfig.port[index].u16SplitRatio = System.UInt16.Parse(ShuntPer.Text);
            //    this.methodconfig.port[index].fSplitFlow = float.Parse(ShuntFlow.Text);
            //    this.methodconfig.port[index].fCarrierGas1 = float.Parse(CarrierGas1.Text);
            //    this.methodconfig.port[index].fCarrierGas2 = float.Parse(CarrierGas2.Text);
            //    this.methodconfig.port[index].fCarrierGas3 = float.Parse(CarrierGas3.Text);

            //    if (cBox省EPC.CheckState == CheckState.Checked)
            //    {
            //        this.methodconfig.port[index].bCarrierGasSave = true;
            //        this.methodconfig.port[index].u16CarrierGasSave = System.UInt16.Parse(cargasthrift.Text);
            //        this.methodconfig.port[index].fSaveGasTime = float.Parse(tb等待时间.Text);
            //    }
            //    else
            //    {
            //        this.methodconfig.port[index].bCarrierGasSave = false;
            //        this.methodconfig.port[index].u16CarrierGasSave = System.UInt16.Parse(cargasthrift.Text);
            //        this.methodconfig.port[index].fSaveGasTime = float.Parse(tb等待时间.Text);
            //    }
            //}

            #endregion
        }

        private double lastnum;
        private void Text_KeyDown(object sender, KeyEventArgs e)
        {
           if (!string.IsNullOrEmpty((sender as TextBox).Text))
            {
                lastnum = Convert.ToDouble((sender as TextBox).Text);
            }
            else
            {
                lastnum = 0;
            }
        }

        private void Text_KeyUp(object sender, KeyEventArgs e)
        {
            if (!string.IsNullOrEmpty((sender as TextBox).Text))
            {
                double nextnum = Convert.ToDouble((sender as TextBox).Text);
                if (nextnum < 5)
                {
                    MessageBox.Show("流量设置不能小于5");
                    (sender as TextBox).Text = lastnum.ToString();
                }
                if (nextnum > 200)
                {
                    MessageBox.Show("流量设置不能大于200");
                    (sender as TextBox).Text = lastnum.ToString();
                }
            }
        }
        bool isone = false;
        private void TxtHeater_TextChanged(object sender, EventArgs e)
        {

            float tmpval = string2float((sender as TextBox).Text);


            if (tmpval > 400.0f)
            {
                MessageBox.Show("设置温度需要小于400");
                (sender as TextBox).Text = "0";
            }
            else
            {
                if (isone)
                {
                    if (tmpval > configTemp)
                    {
                        MessageBox.Show("设置温度不能大于保护温度");
                        (sender as TextBox).Text = "0";
                    }
                }
                else
                {
                    isone = true;
                }
            }
        }

        private void TxtFlow_TextChanged(object sender, EventArgs e)
        {
            //if (!string.IsNullOrEmpty((sender as TextBox).Text))
            //{
            //    double nextnum = Convert.ToDouble((sender as TextBox).Text);
            //    if (nextnum < 5)
            //    {
            //        MessageBox.Show("流量设置不能小于5");
            //        (sender as TextBox).Text = "10";
            //    }
            //    if (nextnum > 200)
            //    {
            //        MessageBox.Show("流量设置不能大于200");
            //        (sender as TextBox).Text = "10";
            //    }
            //}
        }

        //private void combControlMode_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (combControlMode.SelectedIndex == 0)
        //    {
        //        txtPressure.Enabled = true;
        //        txtFlow.Enabled = false;
        //        txtFlow.Text = string.Empty;
        //    }
        //    else
        //    {
        //        txtPressure.Enabled = false;
        //        txtFlow.Enabled = true;
        //        txtPressure.Text = string.Empty;
        //    }

        //    //txtPressure.Enabled = combControlMode.SelectedIndex == 0 ? true : false;
        //    //txtFlow.Enabled = combControlMode.SelectedIndex == 1 ? true : false;
        //}
    }
}
