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

namespace GcDevicePc.ConfigDlg
{
    public partial class Det : Form
    {
        public TCDDlg[] m_TcdDlg = new TCDDlg[3];
        public FIDDlg[] m_FidDlg = new FIDDlg[3];

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

        TabPage Page;
  

        public Det()
        {
            InitializeComponent();

            m_TcdDlg[0] = new TCDDlg();
            m_TcdDlg[1] = new TCDDlg();
            m_TcdDlg[2] = new TCDDlg();
            m_FidDlg[0] = new FIDDlg();
            m_FidDlg[1] = new FIDDlg();
            m_FidDlg[2] = new FIDDlg();            
        }

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

                    }
                    break;
                case 1:
                    if (CProfileDevice.m_DevParam.syspara.u16DetInterType == 0)
                    {
                        m_TcdDlg[1].recordCurData(1);
                    }
                    else if (CProfileDevice.m_DevParam.syspara.u16DetInterType == 1)
                    {
                        m_FidDlg[1].recordCurData(1);
                    }
                    else
                    {

                    }
                    break;
                case 2:
                    if (CProfileDevice.m_DevParam.syspara.u16DetBehindType == 0)
                    {
                        m_TcdDlg[2].recordCurData(2);
                    }
                    else if (CProfileDevice.m_DevParam.syspara.u16DetBehindType == 1)
                    {
                        m_FidDlg[2].recordCurData(2);
                    }
                    else
                    {

                    }
                    break;
            }          
        }

        public void recordCurData()
        {                                                      
            globaldata.m_profileMethod.GetDevState();
            ushort pagenum = CProfileDevice.m_DevParam.syspara.u16DetNum;

            if (pagenum > 0)
            {                    
                if (CProfileDevice.m_DevParam.syspara.u16DetFront == 1)
                {
                    DetRec_Dlg(0);
                    if (CProfileDevice.m_DevParam.syspara.u16DetInter == 1)
                    {
                        DetRec_Dlg(1);

                        if (CProfileDevice.m_DevParam.syspara.u16DetBehind == 1)
                        {
                            DetRec_Dlg(2);
                     
                        }
                    }
                    else if (CProfileDevice.m_DevParam.syspara.u16DetBehind == 1)
                    {
                        DetRec_Dlg(2);
                    }
                }
                else if (CProfileDevice.m_DevParam.syspara.u16DetInter == 1)
                {
                    DetRec_Dlg(1);             
                    if (CProfileDevice.m_DevParam.syspara.u16DetBehind == 1)
                    {
                        DetRec_Dlg(2);               
                    }
                }
                else if (CProfileDevice.m_DevParam.syspara.u16DetBehind == 1)
                {
                    DetRec_Dlg(2);        
                }    
            }
        }


        private void Detadd_Dlg(int position, int index)
        {
            switch (position)
            {
                case 0:
                    if (CProfileDevice.m_DevParam.syspara.u16DetFrontType == 0)
                    {
                        m_TcdDlg[position].TopLevel = false;
                        m_TcdDlg[position].Show();
                        tabControlDet.TabPages[index].Controls.Add(m_TcdDlg[position]);
                        m_TcdDlg[position].reflashContent(position);
                       // tcdnum++;
                    }
                    else if (CProfileDevice.m_DevParam.syspara.u16DetFrontType == 1)
                    {
                        m_FidDlg[position].TopLevel = false;
                        m_FidDlg[position].Show();
                        tabControlDet.TabPages[index].Controls.Add(m_FidDlg[position]);
                        m_FidDlg[position].reflashContent(position);
                      //  fidnum++;
                    }
                    else
                    {

                    }
                    break;
                case 1:
                    if (CProfileDevice.m_DevParam.syspara.u16DetInterType == 0)
                    {
                        m_TcdDlg[position].TopLevel = false;
                        m_TcdDlg[position].Show();
                        tabControlDet.TabPages[index].Controls.Add(m_TcdDlg[position]);
                        m_TcdDlg[position].reflashContent(position);
                     //   tcdnum++;
                    }
                    else if (CProfileDevice.m_DevParam.syspara.u16DetInterType == 1)
                    {
                        m_FidDlg[position].TopLevel = false;
                        m_FidDlg[position].Show();
                        tabControlDet.TabPages[index].Controls.Add(m_FidDlg[position]);
                        m_FidDlg[position].reflashContent(position);
                      //  fidnum++;
                    }
                    else
                    {

                    }
                    break;
                case 2:
                    if (CProfileDevice.m_DevParam.syspara.u16DetBehindType == 0)
                    {
                        m_TcdDlg[position].TopLevel = false;
                        m_TcdDlg[position].Show();
                        tabControlDet.TabPages[index].Controls.Add(m_TcdDlg[position]);
                        m_TcdDlg[position].reflashContent(position);
                     //   tcdnum++;
                    }
                    else if (CProfileDevice.m_DevParam.syspara.u16DetBehindType == 1)
                    {
                        m_FidDlg[position].TopLevel = false;
                        m_FidDlg[position].Show();
                        tabControlDet.TabPages[index].Controls.Add(m_FidDlg[position]);
                        m_FidDlg[position].reflashContent(position);
                     //   fidnum++;
                    }
                    else
                    {

                    }
                    break;
            }          
        }

        public void reflashContent()
        {

            globaldata.m_profileMethod.GetDevState();
            ushort pagenum = CProfileDevice.m_DevParam.syspara.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" + i.ToString();
                    Page.Name = "tabPage" + i.ToString();

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

                }

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

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

        }
    }
}
