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
{
    public partial class FID : Form
    {

        HWConfig hwconfig;
        GCMethodConfig methodconfig;

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

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

        public void reflashContent(int index)
        {

            this.methodconfig.GetFidByID(index);

            fidsettemp.Text = this.methodconfig.fid[index].fHeaterTempSet.ToString();
            tb氢气压力设定.Text = this.methodconfig.fid[index].fSetH2Flow.ToString();
            tb空气压力设定.Text = this.methodconfig.fid[index].fAirFlowSet.ToString();
            tb尾吹气压力设定.Text = this.methodconfig.fid[index].fSetTailGas.ToString();

            cb灵敏度.SelectedIndex = this.methodconfig.fid[index].u16Sensitive;
            cb极性.SelectedIndex = this.methodconfig.fid[index].u16Polarity;

            if (this.methodconfig.fid[index].u16KeyFire == 1)
                cBox自动点火.CheckState = CheckState.Checked;
            else
                cBox自动点火.CheckState = CheckState.Unchecked;

            if (this.methodconfig.fid[index].u16Zero == 1)
                cBox自动调零.CheckState = CheckState.Checked;
            else
                cBox自动调零.CheckState = CheckState.Unchecked;

            if (this.methodconfig.fid[index].u16KeyHeater == 1)
                cBox加热器.CheckState = CheckState.Checked;
            else
                cBox加热器.CheckState = CheckState.Unchecked;

            if (this.methodconfig.fid[index].u16KeyH2Flow == 1)
                cBox氢气.CheckState = CheckState.Checked;
            else
                cBox氢气.CheckState = CheckState.Unchecked;

            if (this.methodconfig.fid[index].u16KeyAirFlow == 1)
                cBox空气.CheckState = CheckState.Checked;
            else
                cBox空气.CheckState = CheckState.Unchecked;

            if (this.methodconfig.fid[index].u16KeyTailGas == 1)
                cBox尾吹气.CheckState = CheckState.Checked;
            else
                cBox尾吹气.CheckState = CheckState.Unchecked;

            if (this.methodconfig.fid[index].u16SigDeduct < 3 && this.methodconfig.fid[index].u16SigDeduct >= 0)
                cListBoxSign.SetItemChecked(this.methodconfig.fid[index].u16SigDeduct, true);
        }

        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(s == "" || s == null)
            {
                ret = 0;
            }
            else
            {
                ret = float.Parse(s);
            }

            return ret;
        }

        public void recordCurData(int index)
        {
            this.methodconfig.fid[index].fHeaterTempSet = string2float(fidsettemp.Text);
            this.methodconfig.fid[index].fSetH2Flow = string2float(tb氢气压力设定.Text);
            this.methodconfig.fid[index].fAirFlowSet = string2float(tb空气压力设定.Text);
            this.methodconfig.fid[index].fSetTailGas = string2float(tb尾吹气压力设定.Text);
            this.methodconfig.fid[index].u16Sensitive = (ushort)cb灵敏度.SelectedIndex;
            this.methodconfig.fid[index].u16Polarity = (ushort)cb极性.SelectedIndex;

            if (cBox自动点火.CheckState == CheckState.Checked)
                this.methodconfig.fid[index].u16KeyFire = 1;
            else
                this.methodconfig.fid[index].u16KeyFire = 0;

            if (cBox自动调零.CheckState == CheckState.Checked)
                this.methodconfig.fid[index].u16Zero = 1;
            else
                this.methodconfig.fid[index].u16Zero = 0;

            if (cBox加热器.CheckState == CheckState.Checked)
                this.methodconfig.fid[index].u16KeyHeater = 1;
            else
                this.methodconfig.fid[index].u16KeyHeater = 0;

            if (cBox氢气.CheckState == CheckState.Checked)
                this.methodconfig.fid[index].u16KeyH2Flow = 1;
            else
                this.methodconfig.fid[index].u16KeyH2Flow = 0;

            if (cBox空气.CheckState == CheckState.Checked)
                this.methodconfig.fid[index].u16KeyAirFlow = 1;
            else
                this.methodconfig.fid[index].u16KeyAirFlow = 0;

            if (cBox尾吹气.CheckState == CheckState.Checked)
                this.methodconfig.fid[index].u16KeyTailGas = 1;
            else
                this.methodconfig.fid[index].u16KeyTailGas = 0;

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

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