using GcDevicePc.GCBuffer;
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;

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

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

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

        public void reflashContent(int index)
        {
            this.methodconfig.GetDetectorByID(index,1);
            lblDetector.Text = "FID";
            txtHeater.Text = this.methodconfig.detector[index].epcHeaterTmpActual.ToString();
            txtHFlow.Text = this.methodconfig.detector[index].epcfHFlowActual.ToString();
            //txtPolarity.Text = this.methodconfig.detector[index].epcPolarityActual == 0 ? "+" : "-";
            txtTGFR.Text = this.methodconfig.detector[index].epcfTGFRActual.ToString();
            txtFlame.Text = this.methodconfig.detector[index].epcfFlameActual.ToString();
            txtAirFlow.Text = this.methodconfig.detector[index].epcfAirFlowActual.ToString();
            combPolarity.SelectedIndex = this.methodconfig.detector[index].epcPolaritySet;

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

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

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

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

            //if (this.methodconfig.detector[index].epcfFlame == 1)
            //    cbFlame.CheckState = CheckState.Checked;
            //else
            //    cbFlame.CheckState = CheckState.Unchecked;

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

        private void TextBoxDec_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }
        }

        float string2float(string s)
        {
            float ret;

            if (string.IsNullOrEmpty(s))
            {
                ret = 0;
            }
            else
            {
                ret = float.Parse(s);
            }

            return ret;
        }

        public void recordCurData(int index)
        {
            this.methodconfig.detector[index].epcHeaterTmpSet = string2float(txtHeater.Text);
            this.methodconfig.detector[index].epcfHFlowSet = string2float(txtHFlow.Text);
            this.methodconfig.detector[index].epcPolaritySet = (ushort)combPolarity.SelectedIndex;
            this.methodconfig.detector[index].epcfTGFRSet = string2float(txtTGFR.Text);
            this.methodconfig.detector[index].epcfFlameSet = string2float(txtFlame.Text);
            this.methodconfig.detector[index].epcfAirFlowSet = string2float(txtAirFlow.Text);

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

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

            //if (cbFlame.CheckState == CheckState.Checked)
            //    this.methodconfig.detector[index].epcfFlame = 1;
            //else
            //    this.methodconfig.detector[index].epcfFlame = 0;

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

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

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


            this.methodconfig.epc[0].fristFlow = string2float(txtTGFR.Text);
            this.methodconfig.epc[0].gasType1 = (ushort)0;
            this.methodconfig.epc[0].SecondFlow = string2float(txtHFlow.Text);
            this.methodconfig.epc[0].gasType2 = (ushort)0;
            this.methodconfig.epc[0].ThirdFlow = string2float(txtAirFlow.Text);
            this.methodconfig.epc[0].gasType3 = (ushort)0; 

        }

        private void txtHFlow_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtAirFlow_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtTGFR_TextChanged(object sender, EventArgs e)
        {

        }

        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 > 100)
                {
                    MessageBox.Show("流量设置不能大于100");
                    (sender as TextBox).Text = lastnum.ToString();
                }
            }
        }


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

        private void txtAirFlow_KeyUp(object sender, KeyEventArgs e)
        {
            if (!string.IsNullOrEmpty((sender as TextBox).Text))
            {
                double nextnum = Convert.ToDouble((sender as TextBox).Text);
                if (nextnum > 500)
                {
                    MessageBox.Show("流量设置不能大于500");
                    (sender as TextBox).Text = alastnum.ToString();
                }
            }
        }

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