using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using GcDevicePc.GCBuffer;

namespace GcDevicePc.ConfigMethod
{
    public partial class ChromeTab : Form
    {
        //private int indexOld = 0;

        HWConfig hwconfig;
        GCMethodConfig methodconfig;

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

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

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

            int FrontNo = 0;
            int BehindNo = 1;
            int cFront = 0;
            int cBehind = 1;

            if (this.hwconfig.hwconfiginfo.u16ChromNum >= 1)
            {
                ChromeSelect.Items.Clear();
                ChromeType.Items.Clear();
                ChromeType.Items.Insert(0, "毛细孔柱");
                ChromeType.Items.Insert(1, "填充柱");

                if (this.hwconfig.hwconfiginfo.u16ChromFront == 1)
                {
                    ChromeSelect.Items.Insert(FrontNo, "前色谱柱");
                    if (this.hwconfig.hwconfiginfo.u16ChromFrontType == 0)
                    {
                        ChromeType.Items.Insert(FrontNo, "毛细孔柱");
                    }
                    else if (this.hwconfig.hwconfiginfo.u16ChromFrontType == 1)
                    {
                        ChromeType.Items.Insert(FrontNo, "填充柱");
                    }
                    else
                    {
                        ChromeType.Items.Insert(FrontNo, "未知");
                    }
                }
                else
                {
                    BehindNo = 0;
                }

                if (this.hwconfig.hwconfiginfo.u16ChromBehind == 1)
                {
                    ChromeSelect.Items.Insert(BehindNo, "后色谱柱");
                    if (this.hwconfig.hwconfiginfo.u16ChromBehindType == 0)
                    {
                        ChromeType.Items.Insert(BehindNo, "毛细孔柱");
                    }
                    else if (this.hwconfig.hwconfiginfo.u16ChromBehindType == 1)
                    {
                        ChromeType.Items.Insert(BehindNo, "填充柱");
                    }
                    else
                    {
                        ChromeType.Items.Insert(BehindNo, "未知");
                    }
                    ChromeType.Enabled = false;
                }
                if (ChromeSelect.Items.Count >= 1)
                    ChromeSelect.SelectedIndex = 0;
            }
            else
            {
                ChromeSelect.Enabled = false;
                ChromeType.Enabled = false;

            }

            if (this.hwconfig.hwconfiginfo.u16ChromFront == 1)
            {
                ChromeMaxTemp.Text = this.methodconfig.chrom[cFront].fHigestTempSet.ToString();
                ChromeLen.Text = this.methodconfig.chrom[cFront].fLengthSet.ToString();
                ChromeCal.Text = this.methodconfig.chrom[cFront].fCalSet.ToString();
                ChromeMem.Text = this.methodconfig.chrom[cFront].fFilmThicknessSet.ToString();
                tb柱前压.Text = this.methodconfig.chrom[cFront].fPreSetValue.ToString();
                tb柱流量.Text = this.methodconfig.chrom[cFront].fFlowSetValue.ToString();
                tb线速度.Text = this.methodconfig.chrom[cFront].fActualAverageLineSpeed.ToString();
                description.Text = this.methodconfig.chrom[cFront].sDescribe;
            }

            if (this.hwconfig.hwconfiginfo.u16ChromBehind == 1)
            {
                ChromeMaxTemp.Text = this.methodconfig.chrom[cBehind].fHigestTempSet.ToString();
                ChromeLen.Text = this.methodconfig.chrom[cBehind].fLengthSet.ToString();
                ChromeCal.Text = this.methodconfig.chrom[cBehind].fCalSet.ToString();
                ChromeMem.Text = this.methodconfig.chrom[cBehind].fFilmThicknessSet.ToString();
                tb柱前压.Text = this.methodconfig.chrom[cBehind].fPreSetValue.ToString();
                tb柱流量.Text = this.methodconfig.chrom[cBehind].fFlowSetValue.ToString();
                tb线速度.Text = this.methodconfig.chrom[cBehind].fActualAverageLineSpeed.ToString();
                description.Text = this.methodconfig.chrom[cBehind].sDescribe;
            }

            if (this.hwconfig.hwconfiginfo.u16StartType == 3)
            {
                this.epcgroup.Enabled = true;
            }
            else
            {
                this.epcgroup.Enabled = false;
            }

        }

        public void recordCurData()
        {
            int index;
            index = this.ChromeSelect.SelectedIndex;

            if (index < 0)
                return;

            if (this.hwconfig.hwconfiginfo.u16ChromNum == 2 && ChromeMaxTemp.Text != "")
            {
                if (index == 1)
                {
                    this.methodconfig.chrom[index - 1].fHigestTempSet = float.Parse(ChromeMaxTemp.Text);
                    this.methodconfig.chrom[index - 1].fLengthSet = float.Parse(ChromeLen.Text);
                    this.methodconfig.chrom[index - 1].fCalSet = float.Parse(ChromeCal.Text);
                    this.methodconfig.chrom[index - 1].fFilmThicknessSet = float.Parse(ChromeMem.Text);
                    this.methodconfig.chrom[index - 1].fPreSetValue = float.Parse(tb柱前压.Text);
                    this.methodconfig.chrom[index - 1].fFlowSetValue = float.Parse(tb柱流量.Text);
                    this.methodconfig.chrom[index - 1].fActualAverageLineSpeed = float.Parse(tb线速度.Text);
                    this.methodconfig.chrom[index - 1].sDescribe = description.Text;
                }
                if (index == 0)
                {
                    this.methodconfig.chrom[index + 1].fHigestTempSet = float.Parse(ChromeMaxTemp.Text);
                    this.methodconfig.chrom[index + 1].fLengthSet = float.Parse(ChromeLen.Text);
                    this.methodconfig.chrom[index + 1].fCalSet = float.Parse(ChromeCal.Text);
                    this.methodconfig.chrom[index + 1].fFilmThicknessSet = float.Parse(ChromeMem.Text);
                    this.methodconfig.chrom[index + 1].fPreSetValue = float.Parse(tb柱前压.Text);
                    this.methodconfig.chrom[index + 1].fFlowSetValue = float.Parse(tb柱流量.Text);
                    this.methodconfig.chrom[index + 1].fActualAverageLineSpeed = float.Parse(tb线速度.Text);
                    this.methodconfig.chrom[index + 1].sDescribe = description.Text;
                }
            }
            else if (this.hwconfig.hwconfiginfo.u16ChromNum == 1 && ChromeMaxTemp.Text != "")
            {
                if (this.hwconfig.hwconfiginfo.u16ChromFront == 1)
                {
                    this.methodconfig.chrom[index].fHigestTempSet = float.Parse(ChromeMaxTemp.Text);
                    this.methodconfig.chrom[index].fLengthSet = float.Parse(ChromeLen.Text);
                    this.methodconfig.chrom[index].fCalSet = float.Parse(ChromeCal.Text);
                    this.methodconfig.chrom[index].fFilmThicknessSet = float.Parse(ChromeMem.Text);
                    this.methodconfig.chrom[index].fPreSetValue = float.Parse(tb柱前压.Text);
                    this.methodconfig.chrom[index].fFlowSetValue = float.Parse(tb柱流量.Text);
                    this.methodconfig.chrom[index].fActualAverageLineSpeed = float.Parse(tb线速度.Text);
                    this.methodconfig.chrom[index].sDescribe = description.Text;
                }
                else
                {
                    this.methodconfig.chrom[index + 1].fHigestTempSet = float.Parse(ChromeMaxTemp.Text);
                    this.methodconfig.chrom[index + 1].fLengthSet = float.Parse(ChromeLen.Text);
                    this.methodconfig.chrom[index + 1].fCalSet = float.Parse(ChromeCal.Text);
                    this.methodconfig.chrom[index + 1].fFilmThicknessSet = float.Parse(ChromeMem.Text);
                    this.methodconfig.chrom[index + 1].fPreSetValue = float.Parse(tb柱前压.Text);
                    this.methodconfig.chrom[index + 1].fFlowSetValue = float.Parse(tb柱流量.Text);
                    this.methodconfig.chrom[index + 1].fActualAverageLineSpeed = float.Parse(tb线速度.Text);
                    this.methodconfig.chrom[index + 1].sDescribe = description.Text;
                }
            }
            else
            {

            }      
        }

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

        private void ChromeSel_SelectedIndexChanged(object sender, EventArgs e)
        {

            recordCurData();
            const int cFront = 0;
            const int cBehind = 1;
            int index;
            index = this.ChromeSelect.SelectedIndex;

            if (this.hwconfig.hwconfiginfo.u16ChromNum >= 1)
            {
                if (this.hwconfig.hwconfiginfo.u16ChromFront == 1)
                {
                    if (index == 0)
                    {
                        ChromeType.SelectedIndex = Convert.ToInt32(this.methodconfig.chrom[cFront].u16ChromType);
                        ChromeMaxTemp.Text = this.methodconfig.chrom[cFront].fHigestTempSet.ToString();
                        ChromeLen.Text = this.methodconfig.chrom[cFront].fLengthSet.ToString();
                        ChromeCal.Text = this.methodconfig.chrom[cFront].fCalSet.ToString();
                        ChromeMem.Text = this.methodconfig.chrom[cFront].fFilmThicknessSet.ToString();
                        tb柱前压.Text = this.methodconfig.chrom[cFront].fPreSetValue.ToString();
                        tb柱流量.Text = this.methodconfig.chrom[cFront].fFlowSetValue.ToString();
                        tb线速度.Text = this.methodconfig.chrom[cFront].fActualAverageLineSpeed.ToString();
                        description.Text = this.methodconfig.chrom[cFront].sDescribe;
                    }
                    else
                    {
                        ChromeType.SelectedIndex = Convert.ToInt32(this.methodconfig.chrom[cBehind].u16ChromType);
                        ChromeMaxTemp.Text = this.methodconfig.chrom[cBehind].fHigestTempSet.ToString();
                        ChromeLen.Text = this.methodconfig.chrom[cBehind].fLengthSet.ToString();
                        ChromeCal.Text = this.methodconfig.chrom[cBehind].fCalSet.ToString();
                        ChromeMem.Text = this.methodconfig.chrom[cBehind].fFilmThicknessSet.ToString();
                        tb柱前压.Text = this.methodconfig.chrom[cBehind].fPreSetValue.ToString();
                        tb柱流量.Text = this.methodconfig.chrom[cBehind].fFlowSetValue.ToString();
                        tb线速度.Text = this.methodconfig.chrom[cBehind].fActualAverageLineSpeed.ToString();
                        description.Text = this.methodconfig.chrom[cBehind].sDescribe;
                    }
                }
                else
                {
                    if (this.hwconfig.hwconfiginfo.u16ChromBehind == 1)
                    {
                        if (index == 0)
                        {
                            ChromeType.SelectedIndex = Convert.ToInt32(this.methodconfig.chrom[cBehind].u16ChromType);
                            ChromeMaxTemp.Text = this.methodconfig.chrom[cBehind].fHigestTempSet.ToString();
                            ChromeLen.Text = this.methodconfig.chrom[cBehind].fLengthSet.ToString();
                            ChromeCal.Text = this.methodconfig.chrom[cBehind].fCalSet.ToString();
                            ChromeMem.Text = this.methodconfig.chrom[cBehind].fFilmThicknessSet.ToString();
                            tb柱前压.Text = this.methodconfig.chrom[cBehind].fPreSetValue.ToString();
                            tb柱流量.Text = this.methodconfig.chrom[cBehind].fFlowSetValue.ToString();
                            tb线速度.Text = this.methodconfig.chrom[cBehind].fActualAverageLineSpeed.ToString();
                            description.Text = this.methodconfig.chrom[cBehind].sDescribe;
                        }
                        else
                        {

                        }
                    }
                }
            }                  
        }
    }
}
