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

        private int indexOld = 0;

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

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

        public void reflashContent()
        {
            this.methodconfig.GetDevInjPortAllPara();

            int index;

            if (this.hwconfig.hwconfiginfo.u16InjPortNum > 0)
            {
                if (this.hwconfig.hwconfiginfo.u16StartType != 2 || this.hwconfig.hwconfiginfo.u16StartType != 1)
                {
                    this.cargasgroup.Enabled = false;
                }
                else
                {
                    this.cargasgroup.Enabled = true;
                }

                InjPortBox.Items.Clear();
                Shuntmode.Items.Clear();

                this.InjPortBox.Items.Add("前进样口");
                this.InjPortBox.Items.Add("后进样口");
                this.Shuntmode.Items.Add("分流");
                this.Shuntmode.Items.Add("不分流");
                Shuntmode.SelectedIndex = 0;
                InjPortBox.SelectedIndex = 0;

                if (this.hwconfig.hwconfiginfo.u16InjPortNum < 2)
                    InjPortBox.Enabled = false;

                if (this.hwconfig.hwconfiginfo.u16InjPortFront == 1)
                {
                    indexOld = 0;
                }
                else if (this.hwconfig.hwconfiginfo.u16InjPortBehind == 1)
                {
                    indexOld = 1;
                }
                else
                {
                    indexOld = 0;
                }

                index = indexOld;

                tb加热.Text = this.methodconfig.port[index].fHeaterSetTemp.ToString();
                tb压力设定.Text = this.methodconfig.port[index].fSetPre.ToString();
                tb总流量设定.Text = this.methodconfig.port[index].fSetSumFlow.ToString();
                tb柱流量.Text = this.methodconfig.port[index].fSetColumnFlow.ToString();
                tb吹扫流量.Text = this.methodconfig.port[index].fSetPurgeFlow.ToString();

                if (this.methodconfig.port[index].bHeater == true)
                    cBox加热器.CheckState = CheckState.Checked;
                else
                    cBox加热器.CheckState = CheckState.Unchecked;

                ShuntPer.Text = this.methodconfig.port[index].u16SplitRatio.ToString();
                ShuntFlow.Text = this.methodconfig.port[index].fSplitFlow.ToString();

                if (this.methodconfig.port[index].bCarrierGasSave == true)
                    cBox省EPC.CheckState = CheckState.Checked;
                else
                    cBox省EPC.CheckState = CheckState.Unchecked;

                cargasthrift.Text = this.methodconfig.port[index].u16CarrierGasSave.ToString();
                tb等待时间.Text = this.methodconfig.port[index].fSaveGasTime.ToString();

                this.InjPortBox.SelectedIndex = indexOld;

                this.groupBox1.Enabled = true;
                this.InjPortBox.Enabled = true;
                this.Shuntmode.Enabled = true;
                this.Shuntmode.Enabled = true;
                this.ShuntPer.Enabled = true;
                this.ShuntFlow.Enabled = true;
            }
            else
            {
                this.groupBox1.Enabled = false;
                this.InjPortBox.Enabled = false;
                this.Shuntmode.Enabled = false;
                this.Shuntmode.Enabled = false;
                this.ShuntPer.Enabled = false;
                this.ShuntFlow.Enabled = false;
                this.cargasgroup.Enabled = false;
            }                                                      
        }

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

        private void InjPortBox_SelectedIndexChanged(object sender, EventArgs e)
        {

            recordCurData();

            int index;
            indexOld = this.InjPortBox.SelectedIndex;
            index = indexOld;

            if (index < 0)
                return;

            tb加热.Text = this.methodconfig.port[index].fHeaterSetTemp.ToString();
            tb压力设定.Text = this.methodconfig.port[index].fSetPre.ToString();
            tb总流量设定.Text = this.methodconfig.port[index].fSetSumFlow.ToString();
            tb柱流量.Text = this.methodconfig.port[index].fSetColumnFlow.ToString();
            tb吹扫流量.Text = this.methodconfig.port[index].fSetPurgeFlow.ToString();
            if (this.methodconfig.port[index].bHeater == true)
                cBox加热器.CheckState = CheckState.Checked;
            else
                cBox加热器.CheckState = CheckState.Unchecked;
            ShuntPer.Text = this.methodconfig.port[index].u16SplitRatio.ToString();
            ShuntFlow.Text = this.methodconfig.port[index].fSplitFlow.ToString();

            if (this.methodconfig.port[index].bCarrierGasSave == true)
                cBox省EPC.CheckState = CheckState.Checked;
            else
                cBox省EPC.CheckState = CheckState.Unchecked;

            cargasthrift.Text = this.methodconfig.port[index].u16CarrierGasSave.ToString();
            tb等待时间.Text = this.methodconfig.port[index].fSaveGasTime.ToString();
        }

        public void recordCurData()
        {
            int index;
            index = indexOld;

            if (index < 0)
                return;

            if (this.hwconfig.hwconfiginfo.u16InjPortFront == 1 && index == 0)
            {

            }
            else if (this.hwconfig.hwconfiginfo.u16InjPortBehind == 1 && index == 1)
            {
 
            }
            else
            {
                return;
            }

            if (tb加热.Text != "")
            {
                this.methodconfig.port[index].fHeaterSetTemp = float.Parse(tb加热.Text);
                this.methodconfig.port[index].fHeaterSetTempm = float.Parse(tb加热.Text);
                this.methodconfig.port[index].fSetPre = float.Parse(tb压力设定.Text);
                this.methodconfig.port[index].fSetSumFlow = float.Parse(tb总流量设定.Text);
                this.methodconfig.port[index].fSetColumnFlow = float.Parse(tb柱流量.Text);
                this.methodconfig.port[index].fSetPurgeFlow = float.Parse(tb吹扫流量.Text);

                if (cBox加热器.CheckState == CheckState.Checked)
                    this.methodconfig.port[index].bHeater = true;
                else
                    this.methodconfig.port[index].bHeater = false;

                this.methodconfig.port[index].u16SplitRatio = System.UInt16.Parse(ShuntPer.Text);
                this.methodconfig.port[index].fSplitFlow = float.Parse(ShuntFlow.Text);

                if (cBox省EPC.CheckState == CheckState.Checked)
                {
                    this.methodconfig.port[index].bCarrierGasSave = true;
                    this.methodconfig.port[index].u16CarrierGasSave = System.UInt16.Parse(cargasthrift.Text);
                    this.methodconfig.port[index].fSaveGasTime = float.Parse(tb等待时间.Text);
                }
                else
                {
                    this.methodconfig.port[index].bCarrierGasSave = false;
                    this.methodconfig.port[index].u16CarrierGasSave = System.UInt16.Parse(cargasthrift.Text);
                    this.methodconfig.port[index].fSaveGasTime = float.Parse(tb等待时间.Text);
                }
            }
        }

        private void tb加热_TextChanged(object sender, EventArgs e)
        {
            if (tb加热.Text != "")
            {
                float tmpval = float.Parse(tb加热.Text);
                if(tmpval > 400.0f)
                {
                    MessageBox.Show("设置温度需要小于400");
                    tb加热.Text = "0";
                }
            }
        }
    }
}
