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

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

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

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

        float string2float(string s)
        {
            float ret;

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

            return ret;
        }

        public void reflashContent(int index)
        {
            this.methodconfig.GetDetectorByID(index,0);
            lblDetector.Text = "TCD";
            txtHeater.Text = this.methodconfig.detector[index].epcHeaterTmpActual.ToString();
            txtRGFR.Text = this.methodconfig.detector[index].epctRGFRActual.ToString();
            //txtAutoBridgeCurrent.Text = this.methodconfig.detector[index].epctAutoBridgeCurrentActual == 0 ? "关" : "开";
            //txtPolarity.Text = this.methodconfig.detector[index].epcPolarityActual == 0 ? "+" : "-";
            txtBridgeCurrent.Text = this.methodconfig.detector[index].epctBridgeCurrentActual.ToString();
            txtElectricSignal.Text = this.methodconfig.detector[index].epctElectricSignalActual.ToString();
            combPolarity.SelectedIndex = this.methodconfig.detector[index].epcPolarityActual;
            combAutoBridgeCurrent.SelectedIndex = this.methodconfig.detector[index].epctAutoBridgeCurrentActual;
            if (this.methodconfig.detector[index].epcHeater == 1)
                cbHeater.CheckState = CheckState.Checked;
            else
                cbHeater.CheckState = CheckState.Unchecked;

            if (this.methodconfig.detector[index].epctElectricSignal == 1)
                cbElectricSignal.CheckState = CheckState.Checked;
            else
                cbElectricSignal.CheckState = CheckState.Unchecked;

            if (this.methodconfig.detector[index].epcPolarity == 1)
                cbPolarity.CheckState = CheckState.Checked;
            else
                cbPolarity.CheckState = CheckState.Unchecked;

            if (this.methodconfig.detector[index].epctRGFR == 1)
                cbRGFR.CheckState = CheckState.Checked;
            else
                cbRGFR.CheckState = CheckState.Unchecked;

            if (this.methodconfig.detector[index].epctBridgeCurrent == 1)
                cbBridgeCurrent.CheckState = CheckState.Checked;
            else
                cbBridgeCurrent.CheckState = CheckState.Unchecked;

            if (this.methodconfig.detector[index].epctAutoBridgeCurrent == 1)
                cbAutoBridgeCurrent.CheckState = CheckState.Checked;
            else
                cbAutoBridgeCurrent.CheckState = CheckState.Unchecked;
        }

        public void recordCurData(int index)
        {
            this.methodconfig.detector[index].epctAutoBridgeCurrentSet = (ushort) combAutoBridgeCurrent.SelectedIndex;
            this.methodconfig.detector[index].epctBridgeCurrentSet = string2float(txtBridgeCurrent.Text);
            this.methodconfig.detector[index].epctElectricSignalSet = string2float(txtElectricSignal.Text);
            this.methodconfig.detector[index].epcHeaterTmpSet = string2float(txtHeater.Text);
            this.methodconfig.detector[index].epctRGFRSet = string2float(txtRGFR.Text);
            this.methodconfig.detector[index].epcPolaritySet = (ushort) combPolarity.SelectedIndex;

            if (cbAutoBridgeCurrent.CheckState == CheckState.Checked)
                this.methodconfig.detector[index].epcHeater = 1;
            else
                this.methodconfig.detector[index].epcHeater = 0;

            if (cbBridgeCurrent.CheckState == CheckState.Checked)
                this.methodconfig.detector[index].epctBridgeCurrent = 1;
            else
                this.methodconfig.detector[index].epctBridgeCurrent = 0;

            if (cbElectricSignal.CheckState == CheckState.Checked)
                this.methodconfig.detector[index].epctElectricSignal = 1;
            else
                this.methodconfig.detector[index].epctElectricSignal = 0;

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

            if (cbPolarity.CheckState == CheckState.Checked)
                this.methodconfig.detector[index].epcPolarity = 1;
            else
                this.methodconfig.detector[index].epcPolarity = 0;

            if (cbRGFR.CheckState == CheckState.Checked)
                this.methodconfig.detector[index].epctRGFR = 1;
            else
                this.methodconfig.detector[index].epctRGFR = 0;



            #region 旧配置tcd文件

            //if (tb桥电流.Text == "")
            //    this.methodconfig.tcd[index].u16Electric = 0;
            //else
            //    this.methodconfig.tcd[index].u16Electric = System.UInt16.Parse(tb桥电流.Text);

            //this.methodconfig.tcd[index].fHeaterTempSet = string2float(tcdsettemp.Text);

            //this.methodconfig.tcd[index].fTaiGasSet = string2float(tb设定尾吹气温压.Text);

            //this.methodconfig.tcd[index].u16Polarity = cb极性.SelectedIndex == 0 ? (ushort)0 : (ushort)1;
            //this.methodconfig.tcd[index].u16AutoZeroSet = cBox调零.CheckState == CheckState.Checked ? (ushort)1 : (ushort)0;
            //this.methodconfig.tcd[index].u16HeaterSel = cBox加热器.CheckState == CheckState.Checked ? (ushort)1 : (ushort)0;
            //this.methodconfig.tcd[index].u16TaiGasSel = cBox尾吹气.CheckState == CheckState.Checked ? (ushort)1 : (ushort)0;
            //this.methodconfig.tcd[index].u16AutoZeroSet = cBox调零.CheckState == CheckState.Checked ? (ushort)1 : (ushort)0;

            //if (cListBoxSign.GetItemChecked(0) == true)
            //    this.methodconfig.tcd[index].u16SigDeduct = 1;
            //else if (cListBoxSign.GetItemChecked(1) == true)
            //    this.methodconfig.tcd[index].u16SigDeduct = 2;
            //else if (cListBoxSign.GetItemChecked(2) == true)
            //    this.methodconfig.tcd[index].u16SigDeduct = 3;

            #endregion
        }

        //private void tcdsettemp_TextChanged(object sender, EventArgs e)
        //{
        //    float tmpval = string2float(tcdsettemp.Text);
        //    if (tmpval > 400.0f)
        //    {
        //        MessageBox.Show("设置温度需要小于400");
        //        tcdsettemp.Text = "0";
        //    }
        //}
    }
}
