using System;
using System.IO;
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 System.Runtime.InteropServices;
using GcDevicePc.IniParam;

namespace GcDevicePc.ConfigDlg
{
   // public delegate void ChangeMNameForUp(string name);
    public delegate void SendUpdateCMD(bool flag);
    public partial class MethodBase : Form
    {

        Point dwPoint;
        bool moveFlag;

        private bool winflag = false;

        public MethodBase()
        {
            InitializeComponent();
        }

        //标题栏 最小化，全屏切换，关闭事件
        private void mmixbtn_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void mchangbtn_Click(object sender, EventArgs e)
        {
            //if (this.mchangbtn.Text == "口")
            //    this.windowState = FormWindowState.Maximized;
            //else
            //    this.windowState = FormWindowState.Normal;
        }

        private void mclose_Click(object sender, EventArgs e)
        {
            globaldata.createfile = false;
           // this.Hide();
            this.Close();

            if (globaldata.m_configDlg == null)
            {
                globaldata.m_configDlg = new MethodConfig();
               
            }
            else
            {
                if (globaldata.m_configDlg.IsDisposed)
                {
                    globaldata.m_configDlg = new MethodConfig();
                }
            }
        }

        public FormWindowState windowState
        {
            get
            {
                return this.WindowState;
            }
            set
            {
                if (value == FormWindowState.Normal)
                {
                    this.mchangbtn.Text = "口";
                    this.WindowState = FormWindowState.Normal;
                }
                else if (value == FormWindowState.Maximized)
                {
                    this.mchangbtn.Text = "吕";
                    this.WindowState = FormWindowState.Maximized;
                }
            }
        }

        private void paneltitle_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //this.WindowState = this.WindowState == FormWindowState.Normal ? FormWindowState.Maximized : FormWindowState.Normal;
            //if (this.WindowState == FormWindowState.Normal)
            //{
            //    this.mchangbtn.Text = "口";
            //}
            //else
            //{
            //    this.mchangbtn.Text = "吕";
            //}
        }

        //窗口变化自适应
        private void MainBase_SizeChanged(object sender, EventArgs e)
        {
            ////改变标题栏
            //this.paneltitle.Width = this.Width;
            //Point closep = new Point(this.Width - 30, 0);
            //this.closebtn.Location = closep;
            //Point changep = new Point(this.Width - 60, 0);
            //this.changebtn.Location = changep;
            //Point mixp = new Point(this.Width - 90, 0);
            //this.mixbtn.Location = mixp;


            //this.panelMenu.Width = this.Width;

            //this.showPanel.Width = this.Width;
            //this.showPanel.Height = this.Height - this.paneltitle.Height - this.panelMenu.Height;
        }

        private void Methodtitle_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                dwPoint = new Point(-e.X, -e.Y);
                moveFlag = true;
            }
        }

        private void Methodtitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (moveFlag)
            {
                Point mousePoint = Control.MousePosition;
                mousePoint.Offset(dwPoint.X, dwPoint.Y);
                Location = mousePoint;
            }
        }

        private void Methodtitle_MouseUp(object sender, MouseEventArgs e)
        {
            if (moveFlag)
            {
                moveFlag = false;
            }
        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(Pens.Black, 0, 0, panel3.Width - 1, panel3.Height - 1);
        }

        private void panel4_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(Pens.Black, 0, 0, panel4.Width - 1, panel4.Height - 1);
        }

        private void resetbtn_Click(object sender, EventArgs e)
        {
            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();
        }

        private void saveasmethod(string filename)
        {
            string tmpname = null;

            string tmpMethodFile = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder + "\\Interim";
          //  string destinationFile = @"c:\temp\test.txt";

            tmpname = globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName;
            FileInfo onefile = new FileInfo(tmpname);
            if (onefile.Exists)
            {
                onefile.CopyTo(tmpMethodFile, true);
            }

            if (globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName != null)
                globaldata.m_configDlg.Save_Method();
            
            if (onefile.Exists)
            {
                onefile.CopyTo(filename, true);
            }

            FileInfo twofile = new FileInfo(tmpMethodFile);
            if (twofile.Exists)
            {
                twofile.CopyTo(tmpname, true);
                File.Delete(tmpMethodFile);
            }
            string message = "保存成功";
            MessageBox.Show(message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void savebtn_Click(object sender, EventArgs e)
        {
            SaveAsWin methodfile = new SaveAsWin();
            methodfile.SaveInI += new SaveAsMethodFile(saveasmethod);
            methodfile.Show(); 
        }

        int maxtemp;
        int mixtemp;
        List<Point> oldline = new List<Point>();
        private void MethodBase_Load(object sender, EventArgs e)
        {
            ushort strt = CProfileDevice.m_DevParam.col.u16ColProNum;
           
            CProfileDevice.stRunTableVal sttmp = new CProfileDevice.stRunTableVal();

          

            if (strt > 0)
            {
                int speed;
                for (int i = 0; i < strt; i++ )
                {
                    globaldata.m_profileMethod.GetColTableStageVal(ref sttmp, (ushort)i);
                    if(i == 0)
                    {
                       // oldline.Add(new Point(0,(int)(CProfileDevice.m_DevParam.col.fCurTemp.fCurTemp)));
                        oldline.Add(new Point(0, 30));
                        oldline.Add(new Point(30, (int)sttmp.TargetTemp/10));
                        mixtemp = (int)(sttmp.TargetTemp / 10);
                    }
                    else
                    {
                        speed = (int)(sttmp.HeatSpeed);
                        if (speed == 0)
                        {
                            speed = 1;
                        }
                        if(i == 1)
                        {
                            oldline.Add(new Point((int)((float)( sttmp.TargetTemp / 10 - oldline[1].Y) / speed * 60 + 30), (int)(sttmp.TargetTemp/10)));
                            if((float)((sttmp.TargetTemp / 10 - oldline[1].Y) / speed) < 1.0f)
                            {
                                oldline.Add(new Point(90, (int)(sttmp.TargetTemp / 10)));
                            }

                        }else
                        {
                            oldline.Add(new Point((int)((float)(sttmp.TargetTemp / 10 - oldline[oldline.Count - 1].Y) / speed * 60 + oldline[oldline.Count - 1].X), (int)(sttmp.TargetTemp / 10)));
                            if ((float)((sttmp.TargetTemp / 10 - oldline[oldline.Count - 2].Y) / speed) < 1.0f)
                            {
                                oldline.Add(new Point(60*i + 30, (int)(sttmp.TargetTemp / 10)));
                            }
                        }                
                    }

                    if(i == strt -1 )
                    {
                        maxtemp = (int)(sttmp.TargetTemp / 10);
                    }
                }

                this.OvenTempCurve.InitPaint(oldline, CProfileDevice.m_DevParam.col.u16ColProNum, mixtemp, maxtemp);

                System.Windows.Forms.Timer T = new System.Windows.Forms.Timer();
                T.Interval = 1000;
                T.Tick += new EventHandler(T_Tick);
                T.Enabled = true;
                cx = 30;
                cy = 150;
            }
        }

        float cx;
        float cy;
        private void T_Tick(object sender, EventArgs e)
        {
            
            cy = (int)(CProfileDevice.m_DevParam.col.fCurTemp.fCurTemp);
            cx += 1;
            //cy += 0.3f;

            //if (cy > 190.0f)
            //    cy = 190.0f;
            
            this.OvenTempCurve.DrawPoint((float)cx,(float)cy);

        }

        private void oventempcheck_CheckedChanged(object sender, EventArgs e)
        {
            //if(oventempcheck.Checked)
            //{
               
            //}else
            //{
            //     T.Enabled = false;
            //}
           
        }

        private void oventempcheck_CheckStateChanged(object sender, EventArgs e)
        {
            //if (oventempcheck.Checked)
            //{
            //    T.Enabled = true;
            //}
            //else
            //{
            //    T.Enabled = false;
            //}
        }

       // public event ChangeMNameForUp changenameforup;
        public event SendUpdateCMD sendupdatecmd;
        private void updatebtn_Click(object sender, EventArgs e)
        {
            int ret=0;

            if (globaldata.m_pcbuffer.gcpcinfo.pcworkinfo.work_Statue == 1 && globaldata.createfile == false)
            {
                string message = "方法运行中，请先停止后再提交";
                MessageBox.Show(message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            else
            {
                if (globaldata.newfile)
                    globaldata.newfile = false;
                else
                    globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName = globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_OpenMethodName;

                if (globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName != null)
                    globaldata.m_configDlg.Save_Method();

                if (!String.IsNullOrEmpty(globaldata.connection_ip) && globaldata.createfile == false)
                {
                    try
                    {
                        FileServerClient mainclient = new FileServerClient();
                        mainclient.WtClientInit(globaldata.connection_ip, globaldata.fileserverport);
                        string tmp = globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName;
                        ret = mainclient.WtClientCopytoServer("SingleAnal.ini", globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName, globaldata.remoteFolder + "仪器方法\\");
                        tmp = globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName.Substring(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName.LastIndexOf("\\") + 1);
                        Thread.Sleep(1000);
                        ret = mainclient.WtClientCopytoServer(tmp, globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName, globaldata.remoteFolder + "仪器方法\\");
                        mainclient.WtClientClose();

                        if (ret == 1)
                        {
                           // changenameforup(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName.Substring(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName.LastIndexOf("\\") + 1));

                            if (globaldata.m_pcbuffer.gcpcinfo.pcworkinfo.system_Statue)
                            {
                                globaldata.m_pcbuffer.gcpcinfo.pcworkinfo.RunType = 0;
                                sendupdatecmd(true);
                            }

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

                        }
                    }
                    catch (Exception ex)
                    {                      
                        MessageBox.Show(ex.ToString(), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        //  mainclient.WtClientClose();
                        //mainclient.close();
                    }
                }

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

                }else if(ret == 1)
                {
                    string message = "保存并提交成功！";
                    MessageBox.Show(message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }else
                {
                    string message = "提交失败！";
                    MessageBox.Show(message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
           // this.Close();
        }

        private void MethodBase_Activated(object sender, EventArgs e)
        {
            this.winflag = true;
            if (this.winflag)
            {
                this.winflag = false;
                if (globaldata.createfile)
                {
                    this.downloadbtn.Text = "保存";
                    this.savebtn.Enabled = false;
                }
                else
                {
                    this.downloadbtn.Text = "保存并提交";
                    this.savebtn.Enabled = true;
                }
            }
        }
    }
}
