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 IPSPL : Form
    {
        HWConfig hwconfig;
        GCMethodConfig methodconfig;

        //private int indexOld = 0;

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

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

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

            txtBlowFlow.Text = this.methodconfig.port[index].splBlowFlowActual.ToString();
            //txtCarrierGasPressure.Text = this.methodconfig.port[index].splCarrierGasPressure.ToString();
            txtColumnFlow.Text = this.methodconfig.port[index].splColumnFlowActual.ToString();
            txtHeater.Text = this.methodconfig.port[index].HeaterTmpActual.ToString();
            txtPrecolumnPressure.Text = this.methodconfig.port[index].splPrecolumnPressureActual.ToString();

            lblSamplePortType.Text = "SPL进样口";

            txtTotalFlow.Text = this.methodconfig.port[index].splTotalFlowActual.ToString();

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

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

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

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

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

            combCarrierGasType.SelectedIndex = this.methodconfig.port[index].CarrierGasType;
            combControlMode.SelectedIndex = this.methodconfig.port[index].ControlMode;
            combShuntMode.SelectedIndex = this.methodconfig.port[index].splShuntMode;
            
            txtShuntRatio.Text = this.methodconfig.port[index].splShuntRatio.ToString();
            txtShuntFlow.Text = this.methodconfig.port[index].splShuntFlow.ToString();
            txtSaveAmount.Text = this.methodconfig.port[index].splSaveAmount.ToString();
            
        }

        float string2float(string s)
        {
            float ret;

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

            return ret;
        }

        //private void InjPortBox_SelectedIndexChanged(object sender, EventArgs e)
        //{

        //    recordCurData();

        //    int index;
        //    indexOld = this.InjPortBox.SelectedIndex;
        //    index = indexOld;

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

        //    tb加热.Text = this.methodconfig.port[index].fHeaterSetTemp.ToString();
        //    tb压力设定.Text = this.methodconfig.port[index].fSetPre.ToString();
        //    tb总流量设定.Text = this.methodconfig.port[index].fSetSumFlow.ToString();
        //    tb柱流量.Text = this.methodconfig.port[index].fSetColumnFlow.ToString();
        //    tb吹扫流量.Text = this.methodconfig.port[index].fSetPurgeFlow.ToString();
        //    if (this.methodconfig.port[index].bHeater == true)
        //        cBox加热器.CheckState = CheckState.Checked;
        //    else
        //        cBox加热器.CheckState = CheckState.Unchecked;
        //    ShuntPer.Text = this.methodconfig.port[index].u16SplitRatio.ToString();
        //    ShuntFlow.Text = this.methodconfig.port[index].fSplitFlow.ToString();
        //    CarrierGas1.Text = this.methodconfig.port[index].fCarrierGas1.ToString();
        //    CarrierGas2.Text = this.methodconfig.port[index].fCarrierGas2.ToString();
        //    CarrierGas3.Text = this.methodconfig.port[index].fCarrierGas3.ToString();

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

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

        public void recordCurData(int index)
        {
            this.methodconfig.port[index].splBlowFlowSet = string2float(txtBlowFlow.Text);


            this.methodconfig.port[index].SamplePortType = "1";
            this.methodconfig.port[index].splColumnFlowSet = string2float(txtColumnFlow.Text);
            this.methodconfig.port[index].HeaterTmpSet = string2float(txtHeater.Text);
            this.methodconfig.port[index].splPrecolumnPressureSet = string2float(txtPrecolumnPressure.Text);
            this.methodconfig.port[index].splTotalFlowSet = string2float(txtTotalFlow.Text);
            
            this.methodconfig.port[index].splShuntRatio = string2float(txtShuntRatio.Text);
            this.methodconfig.port[index].splShuntFlow = string2float(txtShuntFlow.Text);
            this.methodconfig.port[index].splSaveAmount = string2float(txtSaveAmount.Text);

            this.methodconfig.port[index].CarrierGasType = (ushort)combCarrierGasType.SelectedIndex;
            this.methodconfig.port[index].ControlMode = (ushort)combControlMode.SelectedIndex;
            this.methodconfig.port[index].splShuntMode = (ushort)combShuntMode.SelectedIndex;

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

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

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

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

            if (cbSaveEPC.CheckState == CheckState.Checked)
                this.methodconfig.port[index].splSaveEPC = 1;
            else
                this.methodconfig.port[index].splSaveEPC = 0;
        }

        private void Control_CheckChanged(object sender, EventArgs e)
        {
            Control cb = sender as Control;

            if (cb.Name == "combControlMode")
            {
                if (combControlMode.SelectedIndex == 0)
                {
                    cbPrecolumnPressure.CheckState = CheckState.Checked;
                    cbColumnFlow.CheckState = CheckState.Unchecked;
                    cbColumnFlow.Enabled = false;
                    cbPrecolumnPressure.Enabled = true;
                    txtColumnFlow.Text = string.Empty;
                    txtPrecolumnPressure.Enabled = true;
                    txtColumnFlow.Enabled = false;

                }
                else
                {
                    cbPrecolumnPressure.CheckState = CheckState.Unchecked;
                    cbColumnFlow.CheckState = CheckState.Checked;
                    cbPrecolumnPressure.Enabled = false;
                    cbColumnFlow.Enabled = true;
                    txtPrecolumnPressure.Text = string.Empty;
                    txtPrecolumnPressure.Enabled = false;
                    txtColumnFlow.Enabled = true;
                }
            }
            else if (cb.Name == "cbPrecolumnPressure")
            {
                if (cbPrecolumnPressure.Checked)
                {
                    cbColumnFlow.CheckState = CheckState.Unchecked;
                    cbColumnFlow.Enabled = false;
                    txtColumnFlow.Text = string.Empty;
                    txtColumnFlow.Enabled = false;
                    combControlMode.SelectedIndex = 0;
                }
                else
                {
                    cbColumnFlow.Enabled = true;
                    txtColumnFlow.Enabled = true;
                }
            }
            else if (cb.Name == "cbColumnFlow")
            {
                if (cbColumnFlow.Checked)
                {
                    cbPrecolumnPressure.CheckState = CheckState.Unchecked;
                    cbPrecolumnPressure.Enabled = false;
                    txtPrecolumnPressure.Text = string.Empty;
                    txtPrecolumnPressure.Enabled = false;
                    combControlMode.SelectedIndex = 1;
                }
                else
                {
                    cbPrecolumnPressure.Enabled = true;
                    txtPrecolumnPressure.Enabled = true;
                }
            }
            else
            {

            }
        }
    }
}
