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 Form5 : Form
    {
        public Form5()
        {
            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;


        algorithm.algorithm hdalgorithm = new algorithm.algorithm();

        //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 Form5_Load(object sender, EventArgs e)
        {
            DirectoryInfo TheFolder = new DirectoryInfo(globaldata.DataFolder);

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

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                value_lt = File.ReadAllLines(comboBox1.Text);
                hdalgorithm.datavalue_clear();
                hdalgorithm.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;
                hdalgorithm.work_pause = stop;
            }
            else
            {
                button2.Text = "开始";
                stop = true;
                hdalgorithm.work_pause = stop;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string filename = System.IO.Path.Combine(globaldata.DataFolder, comboBox1.Text);
            try
            {
                int len = 0;
                hdalgorithm.datavalue_clear();
                hdalgorithm.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 Form5_FormClosed(object sender, FormClosedEventArgs e)
        {          
           this.Close();        
        }

        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;
                hdalgorithm.write_data_file_infor(file_path);
            }
        }

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