using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using WeifenLuo.WinFormsUI.Docking;

namespace GcDevicePc
{
    public partial class Form4 : DockContent
    {
        public Form4()
        {
            InitializeComponent();

            this.DoubleBuffered = true;

            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);  
        }

        double time = 0;
        double value = 0;

        static int start = 10;
        int iii = start;
        int step = 40;

        float dtime;
        float dvalue;

        public bool stop = false;

        bool in_hdmode = false;

    //    DataTable TEST_TB = new DataTable();
        private void timer1_Tick(object sender, EventArgs e)
        {
        //    chart_FW1.label4.Text = DateTime.Now.ToString();
        //     chart_FW1.label5.Text = DateTime.Now.ToString();

            if (!in_hdmode)
            {
                if (stop)
                {
                    button2.Text = "开始";
                    return;
                }

                if (globaldata.Draw_Port.Count > 1)
                {
                    if (globaldata.startclear || globaldata.stopflag || globaldata.cancelflag)
                    {
                        time = 0;
                        algorithm_W.algorithm.datavalue_clear();
                        algorithm_W.algorithm.result_clear();
                        globaldata.startclear = false;
                    }

                    if (globaldata.stopflag)
                    {
                        algorithm_W.algorithm.work_pause = false;
                        algorithm_W.algorithm.datavalue_clear();
                        algorithm_W.algorithm.result_clear();
                        time = 0;
                    }
                    else
                    {
                        if (globaldata.starttimer == 0)
                        {
                            globaldata.starttimer = ((globaldata.Channel_XY)globaldata.Draw_Port[0]).ctimer;
                        }

                      //  Console.WriteLine("数据个数" + globaldata.Draw_Port.Count.ToString());

                        for (int i = 0; i < globaldata.Draw_Port.Count; i++)
                        {
                            if (globaldata.starttimer <= ((globaldata.Channel_XY)globaldata.Draw_Port[i]).ctimer)
                            {

                                time += ((((globaldata.Channel_XY)globaldata.Draw_Port[i]).ctimer - globaldata.starttimer) / 1000.0f / 60.0f);
                                globaldata.starttimer = ((globaldata.Channel_XY)globaldata.Draw_Port[i]).ctimer;

                                value = (((globaldata.Channel_XY)globaldata.Draw_Port[i]).cdata * 1000.0f);


                                dtime = float.Parse(time.ToString("0.0000"));
                                dvalue = float.Parse(value.ToString("0.0000"));


                                //wjq
                                if (globaldata.startflag && globaldata.cur_DataName != null)
                                {
                                    AESfile savefile = new AESfile(globaldata.cur_DataName);
                                    savefile.Write_filetw(dtime, dvalue);
                                }


                                ////现在只有绘图没有计算？
                                //Console.Write("时间" + dtime.ToString());
                                //Console.WriteLine("值" + dvalue.ToString());
                                //TEST_TB.Rows.Add(dtime, dvalue);

                               
                                chart_FW1.new_data_in(dtime, dvalue);

                            }
                        }
                    }
                }
                //chart1.DataSource = TEST_TB;
                //chart1.DataBind();

                globaldata.Draw_Port.Clear();

                if (globaldata.cancelflag)
                {
                    if (globaldata.cur_DataName != null)
                    {
                        if (File.Exists(globaldata.cur_DataName))
                        {
                            File.Delete(globaldata.cur_DataName);
                        }
                    }

                    globaldata.cancelflag = true;
                }
            }
           
        }
   
        public void enable_for_HD(string filename)
        {
            //algorithm_W.algorithm.datavalue_clear();
            //algorithm_W.algorithm.result_clear();

            this.comboBox1.Enabled = true;
            this.textBox1.Enabled = true;
            this.textBox2.Enabled = true;
          //  this.button1.Enabled = true;
            this.button2.Enabled = true;
            this.button3.Enabled = true;

            this.comboBox1.Text = filename;

            this.in_hdmode = true;
     

        }

        private void Form4_Load(object sender, EventArgs e)
        {


            DirectoryInfo TheFolder = new DirectoryInfo(globaldata.DataFolder);

            foreach (FileInfo NextFile in TheFolder.GetFiles())
                this.comboBox1.Items.Add(NextFile.Name);


            //TEST_TB.Columns.Add("TIME");
            //TEST_TB.Columns.Add("VALUE");

            //chart1.Series[0].XValueMember = "TIME";
            //chart1.Series[0].YValueMembers = "VALUE";
            //chart1.DataSource = TEST_TB;
            //chart1.DataBind();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
              //  value_lt = File.ReadAllLines(comboBox1.Text);
                algorithm_W.algorithm.datavalue_clear();
                algorithm_W.algorithm.result_clear();
                start = Convert.ToInt16(textBox1.Text);
                step = Convert.ToInt16(textBox2.Text);
                iii = start;
                time = 0;
            }catch(Exception)
            {
                MessageBox.Show("请输入有效数值!");
            }           
        }


        public void draw_stop()
        {
            algorithm_W.algorithm.work_pause = false;
        }

        public void draw_clear()
        {
            algorithm_W.algorithm.datavalue_clear();
            algorithm_W.algorithm.result_clear();
            this.time = 0;
            // chart_FW1.new_data_in(time, 0);
        }

        public void save_data()
        {
            // algorithm.algorithm.datavalue_clear();
            // algorithm.algorithm.write_formatter_infor(setting_name_tBox.Text.ToString());
            DateTime sNowTime = DateTime.Now;

            string tmp = "曲线" + sNowTime.ToString("yyyyMMdd_hh_mm_ss");

            algorithm_W.algorithm.write_formatter_infor(tmp);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (stop)
            {
                button2.Text = "停止";
                stop = false;
                algorithm_W.algorithm.work_pause = stop;
            }
            else
            {
                button2.Text = "开始";
                stop = true;
                algorithm_W.algorithm.work_pause = stop;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string filename = System.IO.Path.Combine(globaldata.DataFolder, comboBox1.Text);
     //       algorithm_W.algorithm.work_pause = false;
            try
            {
                int len = 0;

                algorithm_W.algorithm.datavalue_clear();
                algorithm_W.algorithm.result_clear();               

                AESfile hfile = new AESfile(filename);

                using (StreamReader read = new StreamReader(filename, Encoding.Default))
                {
                    len = read.ReadToEnd().Split('\n').Length;
                }

                len = len / 2;
                double[] time = new double[len];
                double[] value = new double[len];

                hfile.Read_filetw(ref time, ref value);

                for (int i = 0; i < len; i++)
                {
                    chart_FW1.new_data_in(time[i], value[i]);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("请输入有效数值!");
            }    
        }

        private void Form4_FormClosed(object sender, FormClosedEventArgs e)
        {          

            ////this.Close();
            ////Environment.Exit(0);
        }

        private void save_file_bt_Click(object sender, EventArgs e) ////文件保存
        {
            saveFileDialog1.Filter = "历史文件|*.zgm";
            saveFileDialog1.DefaultExt = "zgm";
            saveFileDialog1.FileName = "data.zgm";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string file_path = saveFileDialog1.FileName;
                algorithm_W.algorithm.write_out_file_infor(file_path);
            }
        }
        private void save_data_bt_Click(object sender, EventArgs e) ////数据保存
        {
            saveFileDialog1.Filter = "历史数据|*.zgmd";
            saveFileDialog1.DefaultExt = "zgmd";
            saveFileDialog1.FileName = "data.zgmd";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string file_path = saveFileDialog1.FileName;
                algorithm_W.algorithm.write_data_file_infor(file_path);
            }
        }

        private void Form4_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            stop = true;
            ////button2.Text = "开始";
            this.Hide();
        }
    }
}
