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

namespace GcDevicePc.ConfigMethod
{
    public partial class DetTab : Form
    {
        public TCD[] m_TcdDlg = new TCD[3];
        public FID[] m_FidDlg = new FID[3];

        const int TCD = 0;
        const int FID = 1;

        TabPage Page;

        HWConfig hwconfig;
        GCMethodConfig methodconfig;

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

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

            m_TcdDlg[0] = new TCD(this.hwconfig, ref this.methodconfig);
            m_TcdDlg[1] = new TCD(this.hwconfig, ref this.methodconfig);
            m_TcdDlg[2] = new TCD(this.hwconfig, ref this.methodconfig);
            m_FidDlg[0] = new FID(this.hwconfig, ref this.methodconfig);
            m_FidDlg[1] = new FID(this.hwconfig, ref this.methodconfig);
            m_FidDlg[2] = new FID(this.hwconfig, ref this.methodconfig);

            
        }

        private void  DetRec_Dlg(int position)
        {
            switch (position)
            {
                case 0:
                    if (this.hwconfig.hwconfiginfo.u16DetFrontType == 0)
                    {
                        m_TcdDlg[0].recordCurData(0);
                    }
                    else if (this.hwconfig.hwconfiginfo.u16DetFrontType == 1)
                    {
                        m_FidDlg[0].recordCurData(0);
                    }
                    else
                    {

                    }
                    break;
                case 1:
                    if (this.hwconfig.hwconfiginfo.u16DetInterType == 0)
                    {
                        m_TcdDlg[1].recordCurData(1);
                    }
                    else if (this.hwconfig.hwconfiginfo.u16DetInterType == 1)
                    {
                        m_FidDlg[1].recordCurData(1);
                    }
                    else
                    {

                    }
                    break;
                case 2:
                    if (this.hwconfig.hwconfiginfo.u16DetBehindType == 0)
                    {
                        m_TcdDlg[2].recordCurData(2);
                    }
                    else if (this.hwconfig.hwconfiginfo.u16DetBehindType == 1)
                    {
                        m_FidDlg[2].recordCurData(2);
                    }
                    else
                    {

                    }
                    break;
            }          
        }

        public void recordCurData()
        {
     
            ushort pagenum = (ushort)this.hwconfig.hwconfiginfo.u16DetNum;

            if (pagenum > 0)
            {
                if (this.hwconfig.hwconfiginfo.u16DetFront == 1)
                {
                    DetRec_Dlg(0);
                    if (this.hwconfig.hwconfiginfo.u16DetInter == 1)
                    {
                        DetRec_Dlg(1);

                        if (this.hwconfig.hwconfiginfo.u16DetBehind == 1)
                        {
                            DetRec_Dlg(2);

                        }
                    }
                    else if (this.hwconfig.hwconfiginfo.u16DetBehind == 1)
                    {
                        DetRec_Dlg(2);
                    }
                }
                else if (this.hwconfig.hwconfiginfo.u16DetInter == 1)
                {
                    DetRec_Dlg(1);
                    if (this.hwconfig.hwconfiginfo.u16DetBehind == 1)
                    {
                        DetRec_Dlg(2);
                    }
                }
                else if (this.hwconfig.hwconfiginfo.u16DetBehind == 1)
                {
                    DetRec_Dlg(2);
                }
            }
        }


        private void Detadd_Dlg(int position, int index)
        {
            switch (position)
            {
                case 0:
                    if (hwconfig.hwconfiginfo.u16DetFrontType == 0)
                    {
                        m_TcdDlg[position].TopLevel = false;
                        m_TcdDlg[position].Show();
                        tabControlDet.TabPages[index].Controls.Add(m_TcdDlg[position]);
                        m_TcdDlg[position].reflashContent(position);
                    }
                    else if (hwconfig.hwconfiginfo.u16DetFrontType == 1)
                    {
                        m_FidDlg[position].TopLevel = false;
                        m_FidDlg[position].Show();
                        tabControlDet.TabPages[index].Controls.Add(m_FidDlg[position]);
                        m_FidDlg[position].reflashContent(position);
                    }
                    else
                    {

                    }
                    break;
                case 1:
                    if (hwconfig.hwconfiginfo.u16DetInterType == 0)
                    {
                        m_TcdDlg[position].TopLevel = false;
                        m_TcdDlg[position].Show();
                        tabControlDet.TabPages[index].Controls.Add(m_TcdDlg[position]);
                        m_TcdDlg[position].reflashContent(position);
                    }
                    else if (hwconfig.hwconfiginfo.u16DetInterType == 1)
                    {
                        m_FidDlg[position].TopLevel = false;
                        m_FidDlg[position].Show();
                        tabControlDet.TabPages[index].Controls.Add(m_FidDlg[position]);
                        m_FidDlg[position].reflashContent(position);
                    }
                    else
                    {

                    }
                    break;
                case 2:
                    if (hwconfig.hwconfiginfo.u16DetBehindType == 0)
                    {
                        m_TcdDlg[position].TopLevel = false;
                        m_TcdDlg[position].Show();
                        tabControlDet.TabPages[index].Controls.Add(m_TcdDlg[position]);
                        m_TcdDlg[position].reflashContent(position);
                    }
                    else if (hwconfig.hwconfiginfo.u16DetBehindType == 1)
                    {
                        m_FidDlg[position].TopLevel = false;
                        m_FidDlg[position].Show();
                        tabControlDet.TabPages[index].Controls.Add(m_FidDlg[position]);
                        m_FidDlg[position].reflashContent(position);
                    }
                    else
                    {

                    }
                    break;
            }          
        }

        public void reflashContent()
        {

            ushort pagenum = (ushort)hwconfig.hwconfiginfo.u16DetNum;
            int i;

            if (pagenum > 0)
            {

                foreach (TabPage tabPage in tabControlDet.TabPages)
                {
                    this.tabControlDet.TabPages.Remove(tabPage);
                }

                for (i = 0; i < pagenum; i++)
                {
                    Page = new TabPage();
                    Page.Location = new System.Drawing.Point(4, 22);
                    Page.Padding = new System.Windows.Forms.Padding(3);

                    Page.Size = new System.Drawing.Size(553, 288);
                    Page.TabIndex = i;

                    Page.Text = "";
                    Page.Name = "tabPage" + i.ToString();

                    Page.UseVisualStyleBackColor = true;
                    tabControlDet.Controls.Add(Page);

                }

                if ( hwconfig.hwconfiginfo.u16DetFront == 1)
                {
                    tabControlDet.TabPages[0].Text = "前检测器";
                    Detadd_Dlg(0, 0);
                    if (hwconfig.hwconfiginfo.u16DetInter == 1)
                    {
                        tabControlDet.TabPages[1].Text = "中检测器";
                        Detadd_Dlg(1, 1);

                        if (hwconfig.hwconfiginfo.u16DetBehind == 1)
                        {
                            tabControlDet.TabPages[2].Text = "后检测器";
                            Detadd_Dlg(2, 2);
                        }
                    }
                    else if (hwconfig.hwconfiginfo.u16DetBehind == 1)
                    {
                        tabControlDet.TabPages[1].Text = "后检测器";
                        Detadd_Dlg(2, 1);
                    }
                }
                else if (hwconfig.hwconfiginfo.u16DetInter == 1)
                {
                    tabControlDet.TabPages[0].Text = "中检测器";
                    Detadd_Dlg(1, 0);
                    if (hwconfig.hwconfiginfo.u16DetBehind == 1)
                    {
                        tabControlDet.TabPages[1].Text = "后检测器";
                        Detadd_Dlg(2, 1);
                    }
                }
                else if (hwconfig.hwconfiginfo.u16DetBehind == 1)
                {
                    tabControlDet.TabPages[0].Text = "后检测器";
                    Detadd_Dlg(2, 0);
                }
                tabControlDet.SelectedIndex = 0;

            }
        }
    
        private void Det_Load(object sender, EventArgs e)
        {
           
        }
    }
}
