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

namespace WindowsFormsApp_ZaawProgLab05 {
    public partial class Form1 : Form {
        volatile bool Active = true;
        volatile bool Stop = false;
        public Form1() {
            InitializeComponent();
        }

        private void ProgressBar(object sender, EventArgs e) {
            progressBar1.Maximum = 200;
            progressBar1.Minimum = 0;
            for (int i = 0; i < 200; i++) {
                Thread.Sleep(20);
                progressBar1.Value = i + 1;
            }
        }

        private void CiezkieObliczenia(object sender, EventArgs e) {
            BackgroundWorker bw = new BackgroundWorker();
            bw.WorkerReportsProgress = true; //umożliwia powiadamianie 
            bw.DoWork += new DoWorkEventHandler(             // what to do in the background thread
                delegate (object o, DoWorkEventArgs args) {
                    BackgroundWorker b = o as BackgroundWorker;
                    int limit = 20;
                    for (int i = 1; i <= limit; i++) {
                        if (Active) {
                            // 1:
                            textBox1.AppendText(string.Format("{0}% Completed", (int)(i * (float)(100.0 / limit))) + Environment.NewLine);
                            // 2:
                            //b.ReportProgress((int)(i * (float)(100.0 / limit)));
                            // Thread.Sleep(500);
                            // 3:
                            //b.ReportProgress((int)(i * (float)(100.0 / limit)), "I want to say something");
                            //Thread.Sleep(500);
                        } else {
                            i--;
                            Thread.Sleep(500);
                        }
                        if (Stop) {
                            Stop = false;
                            break;
                        }
                    }
                }
            );

            // co zrobić gdy zmienił się wynik
            bw.ProgressChanged += new ProgressChangedEventHandler(
            delegate (object o, ProgressChangedEventArgs args) {
                label1.Text = string.Format("{0}% Completed", args.ProgressPercentage);
                textBox1.AppendText(string.Format("{0}% Completed", args.ProgressPercentage) + Environment.NewLine);

                //3:
                //textBox1.AppendText((args.UserState as String) + Environment.NewLine);
            });

            // zdarzenie uruchamiane po zakończeniu obliczeń
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
            delegate (object o, RunWorkerCompletedEventArgs args) {
                label1.Text = "Finished!";
                textBox1.AppendText("Finished!" + Environment.NewLine);
            });

            bw.RunWorkerAsync();
        }


        private void pause_resume_click(object sender, EventArgs e) {
            if (button1.Text == "Pause") {
                Active = false;
                button1.Text = "Resume";
                button1.BackColor = Color.Green;
            } else {
                Active = true;
                button1.Text = "Pause";
                button1.BackColor = Color.Red;
            }

            //Application.DoEvents();
        }

        private void button4_Click(object sender, EventArgs e) {
            Stop = true;
        }
    }
}
