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 HistoryAtls : Form
    {
        public HistoryAtls()
        {
            InitializeComponent();
        }
        double time = 0;
        Random AAA = new Random();

        string[] value_lt = File.ReadAllLines("demo(白酒).txt");
        double value = 0;
        static int start = 10;
        int iii = start;        
        int step = 40;
        public bool stop = false;       
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (stop)
            {
                button2.Text = "开始";
                return;
            }
            time += 0.01;
            value = Convert.ToDouble(value_lt[iii].Split('\t')[1]);
            //////value = AAA.NextDouble()*100;
            //////chart_FW1.new_data_in(time, AAA.NextDouble());
            chart_FW1.new_data_in(time, value);            
            iii += step;

            if (iii >= value_lt.Length)
            {
                iii = start;                
            }
        }

        private void Form4_Load(object sender, EventArgs e)
        {

        }

        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("请输入有效数值!");
            }           
        }

        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)
        {
            try
            {
                algorithm_W.algorithm.datavalue_clear();
                algorithm_W.algorithm.result_clear();
                start = Convert.ToInt16(textBox1.Text);
                step = Convert.ToInt16(textBox2.Text);
                time = 0;
                value_lt = File.ReadAllLines(comboBox1.Text);
                for (int i = start; i < value_lt.Length; i += step)
                {
                    time += 0.01;
                    value = Convert.ToDouble(value_lt[i].Split('\t')[1]);
                    chart_FW1.new_data_in(time, value);
                }
            }
            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();
        }
    }
}
