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;
using GcDevicePc.DevParam;
using GcDevicePc.IniParam;

namespace GcDevicePc.ConfigDlg
{
    public partial class InjPortDlg : Form
    {
        private int indexOld = 0;

        public InjPortDlg()
        {
            InitializeComponent();         
        }

        public void reflashContent()
        {
            globaldata.m_profileMethod.GetDevState();
            globaldata.m_profileMethod.GetDevInjPortAllPara();

            int index;

            if (CProfileDevice.m_DevParam.syspara.u16InjPortNum > 0)
            {
                if (CProfileDevice.m_DevParam.syspara.u16StartType != 2 || CProfileDevice.m_DevParam.syspara.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 (CProfileDevice.m_DevParam.syspara.u16InjPortNum < 2)
                    InjPortBox.Enabled = false;

                if (CProfileDevice.m_DevParam.syspara.u16InjPortFront == 1)
                {
                  //  InjPortBox.SelectedIndex = 0;
                    indexOld = 0;                   
                }
                else if (CProfileDevice.m_DevParam.syspara.u16InjPortBehind == 1)
                {
                  //  InjPortBox.SelectedIndex = 1;
                    indexOld = 1;
                }
                else
                {
                 //   InjPortBox.SelectedIndex = 0;
                    indexOld = 0;
                }

                index = indexOld;

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

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

                ShuntPer.Text = CProfileDevice.m_DevParam.port[index].u16SplitRatio.ToString(); 
                ShuntFlow.Text = CProfileDevice.m_DevParam.port[index].fSplitFlow.ToString();

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

                cargasthrift.Text = CProfileDevice.m_DevParam.port[index].u16CarrierGasSave.ToString();
                tb等待时间.Text = CProfileDevice.m_DevParam.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 = CProfileDevice.m_DevParam.port[index].fHeaterSetTemp.ToString();
            tb压力设定.Text = CProfileDevice.m_DevParam.port[index].fSetPre.ToString();
            tb总流量设定.Text = CProfileDevice.m_DevParam.port[index].fSetSumFlow.ToString();
            tb柱流量.Text = CProfileDevice.m_DevParam.port[index].fSetColumnFlow.ToString();
            tb吹扫流量.Text = CProfileDevice.m_DevParam.port[index].fSetPurgeFlow.ToString();
            if (CProfileDevice.m_DevParam.port[index].bHeater == true)
                cBox加热器.CheckState = CheckState.Checked;
            else
                cBox加热器.CheckState = CheckState.Unchecked;
            ShuntPer.Text = CProfileDevice.m_DevParam.port[index].u16SplitRatio.ToString(); 
            ShuntFlow.Text = CProfileDevice.m_DevParam.port[index].fSplitFlow.ToString();

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

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

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

            if (index < 0)
                return;

            if (CProfileDevice.m_DevParam.syspara.u16InjPortFront == 1 && index == 0)
            {
              //  this.cb进样口.SelectedIndex = 0;
            }
            else if (CProfileDevice.m_DevParam.syspara.u16InjPortBehind == 1 && index == 1)
            {
             //   this.cb进样口.SelectedIndex = 1;
            }
            else
            {
                //this.cb进样口.SelectedIndex = -1;
                return;
            }

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

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

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

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

                }
                    
           }
           
        }
    }
}
