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
{
    public partial class TCD : Form
    {
        HWConfig hwconfig;
        GCMethodConfig methodconfig;

        public TCD(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.GetTcdByID(index);

            tb桥电流.Text = this.methodconfig.tcd[index].u16Electric.ToString();
            tcdsettemp.Text = this.methodconfig.tcd[index].fHeaterTempSet.ToString();
            tb设定尾吹气温压.Text = this.methodconfig.tcd[index].fTaiGasSet.ToString();

            if (this.methodconfig.tcd[index].u16Polarity == 0)
            {
                cb极性.SelectedIndex = 0;
            }
            else
            {
                cb极性.SelectedIndex = 1;
            }

            if (this.methodconfig.tcd[index].u16AutoZeroSet == 1)
            {
                cBox调零.CheckState = CheckState.Checked;
            }
            else
            {
                cBox调零.CheckState = CheckState.Unchecked;
            }

            if (this.methodconfig.tcd[index].u16HeaterSel == 1)
            {
                cBox加热器.CheckState = CheckState.Checked;
            }
            else
            {
                cBox加热器.CheckState = CheckState.Unchecked;
            }

            if (this.methodconfig.tcd[index].u16TaiGasSel == 1)
            {
                cBox尾吹气.CheckState = CheckState.Checked;
            }
            else
            {
                cBox尾吹气.CheckState = CheckState.Unchecked;
            }

            if (this.methodconfig.tcd[index].u16AutoZeroSet == 1)
            {
                cBox调零.CheckState = CheckState.Checked;
            }
            else
            {
                cBox调零.CheckState = CheckState.Unchecked;
            }

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

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

        public void recordCurData(int index)
        {

            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;
        }

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