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

        int configTemp;
        public void reflashContent(int index)
        {

            this.methodconfig.GetTcdByID(index);


            switch (index)
            {
                case 0:
                    configTemp = globaldata.m_hwconfig.hwconfiginfo.u16DetFrontPTemp;
                    break;
                case 1:
                    configTemp = globaldata.m_hwconfig.hwconfiginfo.u16DetInterPTemp;
                    break;
                case 2:
                    configTemp = globaldata.m_hwconfig.hwconfiginfo.u16DetBehindPTemp;
                    break;
                default:
                    break;
            }

            //tb桥电流.Text = this.methodconfig.tcd[index].u16Electric.ToString();
            tcdsettemp.Text = this.methodconfig.tcd[index].fHeaterTempSet.ToString();

            if (this.hwconfig.hwconfiginfo.u16StartType == 1 || this.hwconfig.hwconfiginfo.u16StartType == 2)
            {
                tb参比气流量.Text = this.methodconfig.tcd[index].fTaiGasSet.ToString();

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

            }
            else
            {
                tb参比气流量.Enabled = false;
                cBox参比气流.Enabled = false;
            }

            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].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 (string.IsNullOrEmpty(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;
        }
        bool isone = false;
        private void tcdsettemp_TextChanged(object sender, EventArgs e)
        {
            float tmpval = string2float(tcdsettemp.Text);
            if(tmpval > 400.0f)
            {
                MessageBox.Show("设置温度需要小于400");
                tcdsettemp.Text = "0";
            }
            else
            {
                if (isone)
                {
                    if (tmpval > configTemp)
                    {
                        MessageBox.Show("设置温度不能大于保护温度");
                        (sender as TextBox).Text = "0";
                    }
                }
                else
                {
                    isone = true;
                }
            }
        }
    }
}
