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 AuxTab : Form
    {
        HWConfig hwconfig;
        GCMethodConfig methodconfig;

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

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

        /// <summary>
        /// 刷新数据
        /// </summary>
        public void reflashContent()
        {
            this.methodconfig.GetDevAux();

            if (this.hwconfig.hwconfiginfo.u16AuxHeaterNum >= 1)
            {
                this.aidheatgroup.Enabled = true;
                if (this.hwconfig.hwconfiginfo.u16AuxHeater1 == 1)
                {
                    aidheatcheck1.Enabled = true;
                    aidheatset1.Enabled = true;
                    aidheatcheck1.Text = this.hwconfig.hwconfiginfo.u16AuxHeaterName1;

                    aidheatcheck1.CheckState = this.methodconfig.aux.fTemp_Enable1 ? CheckState.Checked : CheckState.Unchecked;
                    aidheatset1.Text = this.methodconfig.aux.fSetTemp_HeatArea1.ToString();
                }
                else
                {
                    aidheatcheck1.Enabled = false;
                    aidheatset1.Enabled = false;
                    aidheatcheck1.Text = "未定义";
                }

                if (this.hwconfig.hwconfiginfo.u16AuxHeater2 == 1)
                {
                    aidheatcheck2.Enabled = true;
                    aidheatset2.Enabled = true;
                    aidheatcheck2.Text = this.hwconfig.hwconfiginfo.u16AuxHeaterName2;
                    aidheatcheck2.CheckState = this.methodconfig.aux.fTemp_Enable2 ? CheckState.Checked : CheckState.Unchecked;
                    aidheatset2.Text = this.methodconfig.aux.fSetTemp_HeatArea2.ToString();
                }
                else
                {
                    aidheatcheck2.Enabled = false;
                    aidheatset2.Enabled = false;
                    aidheatcheck2.Text = "未定义";
                }
            }
            else
            {
                this.aidheatgroup.Enabled = false;
            }
            //实际温度设定
            this.aidheatvalue1.Text = "N/A";
            this.aidheatvalue2.Text = "N/A";
        }

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

        /// <summary>
        /// 记录更新当前数据
        /// </summary>
        public void recordCurData()
        {
            if (!String.IsNullOrEmpty(aidheatset1.Text))
            {
                this.methodconfig.aux.fSetTemp_HeatArea1 = float.Parse(aidheatset1.Text);
                if(this.methodconfig.aux.fSetTemp_HeatArea1 > 400.0f)
                {
                    this.methodconfig.aux.fSetTemp_HeatArea1 = 0;
                }
            }
            else
            {
                this.methodconfig.aux.fSetTemp_HeatArea1 = 0.0f;
            }

            if (!String.IsNullOrEmpty(aidheatset2.Text))
            {
                this.methodconfig.aux.fSetTemp_HeatArea2 = float.Parse(aidheatset2.Text);
                if (this.methodconfig.aux.fSetTemp_HeatArea2 > 400.0f)
                {
                    this.methodconfig.aux.fSetTemp_HeatArea2 = 0;
                }
            }
            else
            {
                this.methodconfig.aux.fSetTemp_HeatArea2 = 0.0f;
            }

            this.methodconfig.aux.fTemp_Enable1 = (aidheatcheck1.CheckState == CheckState.Checked) ? true : false;
            this.methodconfig.aux.fTemp_Enable2 = (aidheatcheck2.CheckState == CheckState.Checked) ? true : false;         
        }

        /// <summary>
        /// 输入框数值检测
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void aidheatset1_TextChanged(object sender, EventArgs e)
        {
            float tmpval=0.0f;
            if (aidheatset1.Text != null && aidheatset1.Text != "")
            {
                tmpval = float.Parse(aidheatset1.Text);
            }
 
            if (tmpval > 400.0f)
            {
                MessageBox.Show("请输入小于400的数");
                aidheatset1.Text = "0";
            }
        }

        /// <summary>
        /// 输入框数值检测
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void aidheatset2_TextChanged(object sender, EventArgs e)
        {
            float tmpval = 0.0f;
            if (aidheatset2.Text != null && aidheatset2.Text != "")
            {
                tmpval = float.Parse(aidheatset2.Text);
            }
            if (tmpval > 400.0f)
            {
                MessageBox.Show("请输入小于400的数");
                aidheatset2.Text = "0";
            }
        }
    }
}
