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

namespace GcDevicePc.ConfigMethod
{
    public partial class MethodMain : Form
    {

        public delegate void SendUpdateCMD(bool flag);
        public delegate void Up_MethodMain();

        private Point dwPoint;
        private bool moveFlag;

        HWConfig gchwconfig;
        GCMethodConfig gcmethodconfig;

        private ValveTab m_ValveTab;
        //private AuxTab m_AuxTab;
        //private PortTab m_PortTab;
        private DetTab m_DetTab;
        private SampleTab m_SampleTab;
        private RealColTab m_ReadColTab;
        //private ChromeTab m_ChromeTab;
        private SignalTab m_SignalTab;

        private ChromTabs m_chromTabs;
        //private FID fID;
        //private TCD tCD;
        private SPortTabs sportTabs;

        private AuxTabs auxTabs;

        ////////////////////////////////////////////////////////////////////////////
        //private IPPIP m_iPPIP;
        //private IPSPL m_iPSPL;
        //private IPVAL m_iPVAL;
        //private EPCFID m_ePCFID;
        //private EPCTCD m_ePCTCD;

        //private 
        public MethodMain()
        {
            InitializeComponent();
        }

        private bool updateflag = false;
        public MethodMain(bool flag)
        {
            InitializeComponent();

            this.updateflag = flag;

            if(this.updateflag)
            {
                this.savebth.Text = "保存并提交";
            }
        }

        public MethodMain(bool flag, bool enable)
        {
            InitializeComponent();

            this.updateflag = flag;

            if (this.updateflag)
            {
                this.savebth.Text = "保存并提交";
            }

            if (!enable)
            {
                this.resetbth.Enabled = false;
                this.saveasbth.Enabled = false;
                this.savebth.Enabled = false;
            }
        }

        public void SetGCConfig(HWConfig hwconfig, GCMethodConfig methodconfig)
        {
            this.gchwconfig = hwconfig;
            this.gcmethodconfig = methodconfig;

            this.m_ValveTab = new ValveTab(this.gchwconfig, ref this.gcmethodconfig);
            //this.m_AuxTab = new AuxTab(this.gchwconfig, ref this.gcmethodconfig);
            //this.m_PortTab = new PortTab(this.gchwconfig, ref this.gcmethodconfig);
            this.sportTabs = new SPortTabs(this.gchwconfig, ref this.gcmethodconfig);

            this.m_DetTab = new DetTab(this.gchwconfig, ref this.gcmethodconfig);
            this.m_SampleTab = new SampleTab(this.gchwconfig, ref this.gcmethodconfig);
            this.m_ReadColTab = new RealColTab(this.gchwconfig, ref this.gcmethodconfig);
            this.m_chromTabs = new ChromTabs(this.gchwconfig, ref this.gcmethodconfig);
            this.m_SignalTab = new SignalTab(this.gchwconfig, ref this.gcmethodconfig);


            this.auxTabs = new AuxTabs(this.gchwconfig, ref this.gcmethodconfig);

        }

        #region
        /// <summary>
        /// 最小化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mixbth_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }


        /// <summary>
        /// 关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void closebth_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 鼠标移动之按下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void paneltitle_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                dwPoint = new Point(-e.X, -e.Y);
                moveFlag = true;
            }
        }

        /// <summary>
        /// 鼠标移动之抬起事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void paneltitle_MouseUp(object sender, MouseEventArgs e)
        {
            if (moveFlag)
            {
                moveFlag = false;
            }
        }

        /// <summary>
        /// 鼠标移动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void paneltitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (moveFlag)
            {
                Point mousePoint = Control.MousePosition;
                mousePoint.Offset(dwPoint.X, dwPoint.Y);
                Location = mousePoint;
            }
        }

        /// <summary>
        /// 底部边框绘制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panelbottom_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(Pens.Black, 0, 0, panelbottom.Width - 1, panelbottom.Height - 1);
        }

        /// <summary>
        /// 右边边框绘制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panelright_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(Pens.Black, 0, 0, panelright.Width - 1, panelright.Height - 1);
        }

        #endregion

        private void InitAllTab()
        {
            m_SampleTab.reflashContent();
            m_ValveTab.reflashContent();
            sportTabs.reflashContent();
            m_chromTabs.reflashContent();
            m_ReadColTab.reflashContent();
            m_DetTab.reflashContent();
            m_SignalTab.reflashContent();
            auxTabs.reflashContent();
            //m_AuxTab.reflashContent();
            ////////////////////////////////////////////////////////////////////////////
            //fID.reflashContent(0);
            //m_ePCFID.reflashContent(0);
            //tCD.reflashContent(1);
            //m_ePCTCD.reflashContent(0);
            //m_iPPIP.reflashContent();
            //m_iPSPL.reflashContent();
            //m_iPVAL.reflashContent();
        }

        private void MethodMain_Load(object sender, EventArgs e)
        {

            m_SampleTab.TopLevel = false;
            m_SampleTab.Show();
            tabmain.TabPages[0].Controls.Add(m_SampleTab);

            m_ValveTab.TopLevel = false;
            m_ValveTab.Show();
            tabmain.TabPages[1].Controls.Add(m_ValveTab);

            sportTabs.TopLevel = false;
            sportTabs.Show();
            tabmain.TabPages[2].Controls.Add(sportTabs);

            m_chromTabs.TopLevel = false;
            m_chromTabs.Show();
            tabmain.TabPages[3].Controls.Add(m_chromTabs);

            m_ReadColTab.TopLevel = false;
            m_ReadColTab.Show();
            tabmain.TabPages[4].Controls.Add(m_ReadColTab);

            m_DetTab.TopLevel = false;
            m_DetTab.Show();
            tabmain.TabPages[5].Controls.Add(m_DetTab);

            m_SignalTab.TopLevel = false;
            m_SignalTab.Show();
            tabmain.TabPages[6].Controls.Add(m_SignalTab);

            //m_AuxTab.TopLevel = false;
            //m_AuxTab.Show();
            //tabmain.TabPages[7].Controls.Add(m_AuxTab);

            auxTabs.TopLevel = false;
            auxTabs.Show();
            tabmain.TabPages[7].Controls.Add(auxTabs);

            InitAllTab();
        }

        private void SaveMethod(string filename)
        {
            this.gcmethodconfig.SetMethodSaveName(filename);

            m_SampleTab.recordCurData();
            this.gcmethodconfig.SetSampleAllPara();
            
            m_ValveTab.recordCurData();
            this.gcmethodconfig.SetInjValveAllPara();

            sportTabs.recordCurData();
            //this.gcmethodconfig.SetDevInjPortAllPara();
            if (this.gchwconfig.hwconfiginfo.u16InjPortFront==1)
            {
                if (this.gchwconfig.hwconfiginfo.u16InjPortType_Front == 0)
                {
                    this.gcmethodconfig.SetDevInjPortAllPara();
                }
                if (this.gchwconfig.hwconfiginfo.u16InjPortType_Front == 1)
                {
                    this.gcmethodconfig.SetDevInjPortAllPara();
                }
            }

            if (this.gchwconfig.hwconfiginfo.u16InjPortBehind == 1)
            {
                if (this.gchwconfig.hwconfiginfo.u16InjPortType_Behind == 0)
                {
                    this.gcmethodconfig.SetDevInjPortAllPara();
                }
                if (this.gchwconfig.hwconfiginfo.u16InjPortType_Behind == 1)
                {
                    this.gcmethodconfig.SetDevInjPortAllPara();
                }
            }

            //if (this.gchwconfig.hwconfiginfo.u16InjValveVA1 == 1)
            //{
            //    this.gcmethodconfig.SetValInjPortAllPara();
            //}
            //if (this.gchwconfig.hwconfiginfo.u16InjValveVA2 == 1)
            //{
            //    this.gcmethodconfig.SetValInjPortAllPara();
            //}
            
            m_chromTabs.recordCurData();
            this.gcmethodconfig.SetChromAllPara();
            
            m_ReadColTab.recordCurData();
            this.gcmethodconfig.SetColTableElseAllPara();

            m_DetTab.recordCurData();
            if (this.gchwconfig.hwconfiginfo.u16DetFront == 1)
            {
                if(this.gchwconfig.hwconfiginfo.u16DetFrontType == 0)
                {
                    this.gcmethodconfig.SetTcdByID(0);
                }
                else if (this.gchwconfig.hwconfiginfo.u16DetFrontType == 1)
                {
                    this.gcmethodconfig.SetFidByID(0);
                }
            }
            
            if (this.gchwconfig.hwconfiginfo.u16DetInter == 1)
            {
                if (this.gchwconfig.hwconfiginfo.u16DetInterType == 0)
                {
                    this.gcmethodconfig.SetTcdByID(1);
                }
                else if (this.gchwconfig.hwconfiginfo.u16DetInterType == 1)
                {
                    this.gcmethodconfig.SetFidByID(1);
                }
               
            }
            
            if(this.gchwconfig.hwconfiginfo.u16DetBehind == 1)
            {
                if (this.gchwconfig.hwconfiginfo.u16DetBehindType == 0)
                {
                    this.gcmethodconfig.SetTcdByID(2);
                }
                else if (this.gchwconfig.hwconfiginfo.u16DetBehindType == 1)
                {
                    this.gcmethodconfig.SetFidByID(2);
                }  
            }
           
            m_SignalTab.recordCurData();
            this.gcmethodconfig.SetDevSignalAllPara();
            
            //m_AuxTab.recordCurData();
            //this.gcmethodconfig.SetDevAux();

            auxTabs.recordCurData();
            if (this.gchwconfig.hwconfiginfo.u16AuxHeaterNum >= 1)
            {
                //if (this.gchwconfig.hwconfiginfo.u16AuxHeater1 == 1)
                {
                    this.gcmethodconfig.SetDevAuxs();
                }
                //if (this.gchwconfig.hwconfiginfo.u16AuxHeater2 == 1)
                //{
                //    this.gcmethodconfig.SetDevAuxs();
                //}

            }


        }
        public event Up_MethodMain upmethod;

        public event SendUpdateCMD sendupdatecmd;
        private void savebth_Click(object sender, EventArgs e)
        {
            string filename = this.gcmethodconfig.savefilename.Substring(this.gcmethodconfig.savefilename.LastIndexOf("\\") + 1);
            if (File.Exists(this.gcmethodconfig.savefilename) == false)
            {
                StreamWriter sw = new StreamWriter(this.gcmethodconfig.savefilename, false, System.Text.Encoding.Unicode);
                sw.WriteLine("[方法名称]");
                sw.WriteLine("名称=" + filename);
                sw.Close();
            }
            SaveMethod(this.gcmethodconfig.savefilename);

            if (this.updateflag)
            {
                upmethod();
                if (globaldata.m_pcbuffer.gcpcinfo.pcworkinfo.work_Statue == 1)
                {
                    string message = "方法运行中，请先停止后再提交";
                    MessageBox.Show(message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    try
                    {
                        int ret = 0;
                        int initret = 0;
                        FileServerClient mainclient = new FileServerClient();
                        initret = mainclient.WtClientInit(globaldata.connection_ip, globaldata.fileserverport);
                        if (initret == 1)
                        {
                            string tmp = globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_OpenMethodName;
                            //if (ret == 1)
                            {
                                ret = mainclient.WtClientCopytoServer("SingleAnal.ini", tmp, globaldata.remoteFolder + "仪器方法\\");
                                tmp = globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_OpenMethodName.Substring(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_OpenMethodName.LastIndexOf("\\") + 1);
                                ret = mainclient.WtClientCopytoServer(tmp, globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_OpenMethodName, globaldata.remoteFolder + "仪器方法\\");
                            }
                            mainclient.WtClientClose();
                        }

                        if (ret == 1)
                        {
                            if (globaldata.m_pcbuffer.gcpcinfo.pcworkinfo.system_Statue)
                            {
                                sendupdatecmd(true);
                            }

                            globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName = this.gcmethodconfig.savefilename;

                            /////////同步当前运行方法/////////
                            {
                                globaldata.m_profileMethod.SetMethodFullName(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName);
                                globaldata.m_profileMethod.GetDevState();
                                if (globaldata.m_configDlg.IsDisposed)
                                    globaldata.m_configDlg = new MethodConfig();

                                globaldata.m_configDlg.m_InjPortDlg.reflashContent();
                                globaldata.m_configDlg.m_InjSampleDlg.reflashContent();
                                globaldata.m_configDlg.m_InjValveDlg.reflashContent();
                                globaldata.m_configDlg.m_chromeDlg.reflashContent();
                                globaldata.m_configDlg.m_RealColDlg.reflashContent();
                                globaldata.m_configDlg.m_det.reflashContent();
                                globaldata.m_configDlg.m_AuxDlg.reflashContent();
                                globaldata.m_configDlg.m_SignalDlg.reflashContent();
                            }
                            globaldata.m_pcbuffer.gcpcinfo.pcworkinfo.RunType = 0;
                            SignalDataToHw.methodconfig = this.gcmethodconfig;
                            SendDataToZB.methodconfig = this.gcmethodconfig;


                            //string message = "提交成功！";
                            //MessageBox.Show(message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    catch
                    {

                    }


                }
            }
            else
            {
                //string message = "保存成功！";
                //MessageBox.Show(message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            this.Close();
            
        }
        private void saveasbth_Click(object sender, EventArgs e)
        {

            SaveDialog saveDialog = new SaveDialog(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder, 1);
            DialogResult dr = saveDialog.ShowDialog();
            if (dr == DialogResult.OK)
            {
                //String mfilename = Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder, saveDialog.filename);
                saveasmethod(saveDialog.filename);
            }

            //SaveAsWin methodfile = new SaveAsWin(this.gchwconfig.hwconfiginfo.GCNum);
            //methodfile.SaveAsFile += new SaveAsMethodFile(saveasmethod);
            //methodfile.Show(); 

        }
        private void saveasmethod(string filename)
        {
            string newfilename = Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder, filename);
            if (File.Exists(newfilename) == false)
            {
                StreamWriter sw = new StreamWriter(newfilename, false, System.Text.Encoding.Unicode);
                sw.WriteLine("[方法名称]");
                sw.WriteLine("名称=" + filename);
                sw.Close();
            }
            SaveMethod(newfilename);

            //string message = "另存为成功！";
            //MessageBox.Show(message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
        }
    }
}
