using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

namespace WindowsFormsApp_ZaawProgLab06 {
    public partial class Form1 : Form {
        private Thread cpuThread;
        private double[] cpuArray = new double[60];
        private bool token = false;
        System.Threading.Timer t = null; // obiekt timera z trzeciego wykresu, jeżeli t nie będzie polem Form1
            //tylko np. zmienną lokalną metody button3, po paru sekundach przestanie działać, bo GarbageCollector
            //usunie ten obiekt
        public Form1() {
            InitializeComponent();
        }

        private void getPerformanceCounters() {
            var cpuPerfCounter = new PerformanceCounter("Processor Information", "% Processor Time", "_Total");
            while (true) {
                cpuArray[cpuArray.Length - 1] = Math.Round(cpuPerfCounter.NextValue(), 0);
                Array.Copy(cpuArray, 1, cpuArray, 0, cpuArray.Length - 1);
                if (cpuChart.IsHandleCreated) {
                    this.Invoke((MethodInvoker)delegate { UpdateCpuChart(); });
                    Application.DoEvents();
                } else {
                    //......
                }
                Thread.Sleep(500);
            }
        }

        private void UpdateCpuChart() {
            cpuChart.Series["Series1"].Points.Clear();
            chartWspolny.Series["Series3"].Points.Clear();
            for (int i = 0; i < cpuArray.Length - 1; ++i) {
                cpuChart.Series["Series1"].Points.AddY(cpuArray[i]);
                if (checkBox1.Checked)
                    chartWspolny.Series["Series3"].Points.AddY(cpuArray[i]);
            }
        }

        private void button1_Click(object sender, EventArgs e) {
            cpuThread = new Thread(new ThreadStart(this.getPerformanceCounters));
            cpuThread.IsBackground = true;
            cpuThread.Start();
        }


        private void button2_Click(object sender, EventArgs e) {
            Random gen = new Random();
            for (int i = 0; i < 100; i++) {
                int value = gen.Next(100);
                Thread.Sleep(50);
                chartRandom.Series["Series1"].Points.AddY(value);
                chartRandom.Update();
                if(checkBox1.Checked) {
                    chartWspolny.Series["Series2"].Points.AddY(value);
                    chartWspolny.Update();
                }
                Application.DoEvents();
            }
        }

        private void button3_Click(object sender, EventArgs e) {
            if (!token) {
                token = true;
                System.Windows.Forms.DataVisualization.Charting.Series s = chartSeconds.Series.Add("SeriaTimer");
                s.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;

                TimerCallback timeCB = new TimerCallback(doSmth);
                // Establish timer settings.
                t = new System.Threading.Timer(
                    timeCB, // The TimerCallback delegate object.
                    "SeriaTimer", // Any info to pass into the called method (null for no info).
                    0, // Amount of time to wait before starting (in milliseconds).
                    500); // Interval of time between calls (in milliseconds).
            }
        }

        private void doSmth(object state) {
            int v = DateTime.Now.Second;
            string name = (string)state; //rozpakowanie "MojaSeria" z obiektu state

            chartSeconds.BeginInvoke((Action)(() => {
                chartSeconds.Series[name].Points.AddY(v);
                textBox1.Text = "" + v;
                if(checkBox1.Checked)
                    chartWspolny.Series["Series1"].Points.AddY(v);
                //Application.DoEvents();
            }));

        }
    }
}
