#pragma once
#include "Uhr.h"
#include <thread>
namespace CppCLRWinformsProjekt {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace std;

	public delegate void AnzeigeDelegate(int minuten, int sekunden);
	/// <summary>
	/// Zusammenfassung für Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{

	public: static Label^ uhrLabel;
	public: static AnzeigeDelegate^ anzeigeDelegate;
	private: System::Windows::Forms::Panel^ panel1;
	public:
	private: Uhr* meineUhr;
	public:
		Form1(void)
		{
			InitializeComponent();

			meineUhr = new Uhr();
			uhrLabel = lblTimer;
			
			anzeigeDelegate += gcnew AnzeigeDelegate(this, &Form1::SetText);
		}

	protected:
		/// <summary>
		/// Verwendete Ressourcen bereinigen.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Label^ lblTimer;
	protected:

	private: System::Windows::Forms::Button^ btnStart;
	private: System::Windows::Forms::Button^ btnStopp;
	private: System::Windows::Forms::Button^ btnPause;
	private: System::Windows::Forms::NumericUpDown^ nudMinuten;
	private: System::Windows::Forms::NumericUpDown^ nudSekunden;
	private: System::Windows::Forms::GroupBox^ gbZeiteinstellen;
	private: System::Windows::Forms::Label^ lblSekunden;
	private: System::Windows::Forms::Label^ lblMinuten;
	protected:

	private:
		/// <summary>
		/// Erforderliche Designervariable.
		/// </summary>
		System::ComponentModel::Container^ components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Erforderliche Methode für die Designerunterstützung.
		/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
		/// </summary>
		void InitializeComponent(void)
		{
			this->lblTimer = (gcnew System::Windows::Forms::Label());
			this->btnStart = (gcnew System::Windows::Forms::Button());
			this->btnStopp = (gcnew System::Windows::Forms::Button());
			this->btnPause = (gcnew System::Windows::Forms::Button());
			this->nudMinuten = (gcnew System::Windows::Forms::NumericUpDown());
			this->nudSekunden = (gcnew System::Windows::Forms::NumericUpDown());
			this->gbZeiteinstellen = (gcnew System::Windows::Forms::GroupBox());
			this->lblSekunden = (gcnew System::Windows::Forms::Label());
			this->lblMinuten = (gcnew System::Windows::Forms::Label());
			this->panel1 = (gcnew System::Windows::Forms::Panel());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->nudMinuten))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->nudSekunden))->BeginInit();
			this->gbZeiteinstellen->SuspendLayout();
			this->panel1->SuspendLayout();
			this->SuspendLayout();
			// 
			// lblTimer
			// 
			this->lblTimer->Dock = System::Windows::Forms::DockStyle::Top;
			this->lblTimer->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 48, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
				static_cast<System::Byte>(0)));
			this->lblTimer->ForeColor = System::Drawing::Color::Black;
			this->lblTimer->Location = System::Drawing::Point(0, 0);
			this->lblTimer->Name = L"lblTimer";
			this->lblTimer->Size = System::Drawing::Size(284, 73);
			this->lblTimer->TabIndex = 0;
			this->lblTimer->Text = L"00:00";
			this->lblTimer->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
			// 
			// btnStart
			// 
			this->btnStart->Location = System::Drawing::Point(180, 40);
			this->btnStart->Name = L"btnStart";
			this->btnStart->Size = System::Drawing::Size(75, 23);
			this->btnStart->TabIndex = 1;
			this->btnStart->Text = L"Start";
			this->btnStart->UseVisualStyleBackColor = true;
			this->btnStart->Click += gcnew System::EventHandler(this, &Form1::btnStart_Click);
			// 
			// btnStopp
			// 
			this->btnStopp->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Right));
			this->btnStopp->Enabled = false;
			this->btnStopp->Location = System::Drawing::Point(197, 91);
			this->btnStopp->Name = L"btnStopp";
			this->btnStopp->Size = System::Drawing::Size(75, 23);
			this->btnStopp->TabIndex = 2;
			this->btnStopp->Text = L"Stopp";
			this->btnStopp->UseVisualStyleBackColor = true;
			this->btnStopp->Click += gcnew System::EventHandler(this, &Form1::btnStopp_Click);
			// 
			// btnPause
			// 
			this->btnPause->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Right));
			this->btnPause->Enabled = false;
			this->btnPause->Location = System::Drawing::Point(116, 91);
			this->btnPause->Name = L"btnPause";
			this->btnPause->Size = System::Drawing::Size(75, 23);
			this->btnPause->TabIndex = 3;
			this->btnPause->Text = L"Pause";
			this->btnPause->UseVisualStyleBackColor = true;
			this->btnPause->Click += gcnew System::EventHandler(this, &Form1::btnPause_Click);
			// 
			// nudMinuten
			// 
			this->nudMinuten->Location = System::Drawing::Point(21, 43);
			this->nudMinuten->Maximum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 59, 0, 0, 0 });
			this->nudMinuten->Name = L"nudMinuten";
			this->nudMinuten->Size = System::Drawing::Size(50, 20);
			this->nudMinuten->TabIndex = 4;
			this->nudMinuten->ValueChanged += gcnew System::EventHandler(this, &Form1::nudMinuten_ValueChanged);
			// 
			// nudSekunden
			// 
			this->nudSekunden->Location = System::Drawing::Point(77, 43);
			this->nudSekunden->Maximum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 59, 0, 0, 0 });
			this->nudSekunden->Name = L"nudSekunden";
			this->nudSekunden->Size = System::Drawing::Size(44, 20);
			this->nudSekunden->TabIndex = 5;
			this->nudSekunden->ValueChanged += gcnew System::EventHandler(this, &Form1::nudSekunden_ValueChanged);
			// 
			// gbZeiteinstellen
			// 
			this->gbZeiteinstellen->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Right));
			this->gbZeiteinstellen->Controls->Add(this->lblSekunden);
			this->gbZeiteinstellen->Controls->Add(this->lblMinuten);
			this->gbZeiteinstellen->Controls->Add(this->btnStart);
			this->gbZeiteinstellen->Controls->Add(this->nudSekunden);
			this->gbZeiteinstellen->Controls->Add(this->nudMinuten);
			this->gbZeiteinstellen->Location = System::Drawing::Point(12, 3);
			this->gbZeiteinstellen->Name = L"gbZeiteinstellen";
			this->gbZeiteinstellen->Size = System::Drawing::Size(260, 82);
			this->gbZeiteinstellen->TabIndex = 6;
			this->gbZeiteinstellen->TabStop = false;
			this->gbZeiteinstellen->Text = L"Zeiteinstellen";
			// 
			// lblSekunden
			// 
			this->lblSekunden->AutoSize = true;
			this->lblSekunden->Location = System::Drawing::Point(74, 27);
			this->lblSekunden->Name = L"lblSekunden";
			this->lblSekunden->Size = System::Drawing::Size(56, 13);
			this->lblSekunden->TabIndex = 7;
			this->lblSekunden->Text = L"Sekunden";
			// 
			// lblMinuten
			// 
			this->lblMinuten->AutoSize = true;
			this->lblMinuten->Location = System::Drawing::Point(18, 27);
			this->lblMinuten->Name = L"lblMinuten";
			this->lblMinuten->Size = System::Drawing::Size(45, 13);
			this->lblMinuten->TabIndex = 6;
			this->lblMinuten->Text = L"Minuten";
			// 
			// panel1
			// 
			this->panel1->Controls->Add(this->gbZeiteinstellen);
			this->panel1->Controls->Add(this->btnPause);
			this->panel1->Controls->Add(this->btnStopp);
			this->panel1->Dock = System::Windows::Forms::DockStyle::Fill;
			this->panel1->Location = System::Drawing::Point(0, 73);
			this->panel1->Name = L"panel1";
			this->panel1->Size = System::Drawing::Size(284, 125);
			this->panel1->TabIndex = 7;
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(284, 198);
			this->Controls->Add(this->panel1);
			this->Controls->Add(this->lblTimer);
			this->Name = L"Form1";
			this->Text = L"Uhr";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->nudMinuten))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->nudSekunden))->EndInit();
			this->gbZeiteinstellen->ResumeLayout(false);
			this->gbZeiteinstellen->PerformLayout();
			this->panel1->ResumeLayout(false);
			this->ResumeLayout(false);

		}
#pragma endregion
	private: System::Void setTimer()
	{
		this->lblTimer->Text = "";
		if (nudMinuten->Value < 10)
			this->lblTimer->Text = "0";

		this->lblTimer->Text += nudMinuten->Value;
		this->lblTimer->Text += ":";

		if (nudSekunden->Value < 10)
			this->lblTimer->Text += "0";

		this->lblTimer->Text += nudSekunden->Value;

		this->lblTimer->ForeColor = Color::Black;
	}

	private: System::Void nudMinuten_ValueChanged(System::Object^ sender, System::EventArgs^ e) {
		setTimer();
	}

	private: void SetText(int minuten, int sekunden)
	{
		this->lblTimer->Text = "";
		if (minuten < 10)
			this->lblTimer->Text = "0";

		this->lblTimer->Text += minuten;
		this->lblTimer->Text += ":";

		if (sekunden < 10)
			this->lblTimer->Text += "0";

		this->lblTimer->Text += sekunden;

		if (minuten == 0 && sekunden == 0 || minuten == 59 && sekunden == 59)
		{
			this->lblTimer->ForeColor = Color::DarkRed;
			this->btnStopp->PerformClick();
		}
		
	}


	private: System::Void nudSekunden_ValueChanged(System::Object^ sender, System::EventArgs^ e) {
		setTimer();
	}
	private: System::Void btnStart_Click(System::Object^ sender, System::EventArgs^ e) {
		meineUhr->minuten = (int)nudMinuten->Value;
		meineUhr->sekunden = (int)nudSekunden->Value;
		meineUhr->Start();
		this->gbZeiteinstellen->Visible = false;
		this->btnStart->Enabled = false;
		this->btnStopp->Enabled = true;
		this->btnPause->Enabled = true;
		this->lblTimer->ForeColor = Color::Black;
	}
	private: System::Void btnStopp_Click(System::Object^ sender, System::EventArgs^ e) {
		meineUhr->uhrLaeuft = false;
		meineUhr->pausiert = false;
		meineUhr->modus = Stellmodus;
		meineUhr->minuten = 0;
		meineUhr->sekunden = 0;
		this->lblTimer->Text = "00:00";
		this->btnPause->Enabled = false;
		this->btnPause->Text = "Pause";
		this->btnStopp->Enabled = false;
		this->btnStart->Enabled = true;
		this->gbZeiteinstellen->Visible = true;
		this->nudMinuten->Value = 0;
		this->nudSekunden->Value = 0;
		

	}
private: System::Void btnPause_Click(System::Object^ sender, System::EventArgs^ e) {
	if (!meineUhr->pausiert)
	{
		meineUhr->Pause();
		this->btnPause->Text = "Weiter";
	}
	else
	{
		meineUhr->Start();
		this->btnPause->Text = "Pause";
	}
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
};
}
