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.Common;
using GcDevicePc.GCBuffer;

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

        public delegate void SendUpdateCMD(bool flag);

        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;


        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_AuxTab = new AuxTab(this.gchwconfig, ref this.gcmethodconfig);
            this.m_ValveTab = new ValveTab(this.gchwconfig, ref this.gcmethodconfig);

            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.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_ChromeTab = new ChromeTab(this.gchwconfig, ref this.gcmethodconfig);
            this.m_SignalTab = new SignalTab(this.gchwconfig, ref this.gcmethodconfig);

        }

        /// <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);
        }

        private void InitAllTab()
        {
            m_SampleTab.reflashContent();
            m_ValveTab.reflashContent();
            m_PortTab.reflashContent();
            m_ChromeTab.reflashContent();
            m_ReadColTab.reflashContent();
            m_DetTab.reflashContent();
            m_SignalTab.reflashContent();
            m_AuxTab.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);

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

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

            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);

            InitAllTab();
        }

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

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

            m_PortTab.recordCurData();
            this.gcmethodconfig.SetDevInjPortAllPara();
            
            m_ChromeTab.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();
        }

        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, Encoding.Unicode);
                sw.WriteLine("[方法名称]");
                sw.WriteLine("名称=" + filename);
                sw.Close();
            }
            SaveMethod(this.gcmethodconfig.savefilename);

            if (this.updateflag)
            {
                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)
                            {
                                Log.Info(tmp +"提交成功");
                            }
                        }
                       
                        if (ret == 1)
                        {
                            
                            if (globaldata.m_pcbuffer.gcpcinfo.pcworkinfo.system_Statue)
                            {
                               // globaldata.m_pcbuffer.gcpcinfo.pcworkinfo.RunType = 1;
                                sendupdatecmd(true);
                            }
                          
                            globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName = gcmethodconfig.savefilename;

                            try
                            {
                                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();

                                string message = "保存并提交成功！";
                                MessageBox.Show(message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            catch
                            {
                                Log.Error(gcmethodconfig.savefilename + "解析失败");
                            }
                        }
                    }
                    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)
            {
                saveasmethod(saveDialog.filename);
            }
        }

        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);
        }
    }
}
