#pragma once
#include <stdio.h>
#include <time.h>
#include <string>
#include <iostream>

namespace Stopky
	{

	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 System::Runtime::InteropServices;
	using namespace std;
	
	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Label^  Text_nbtn;
	protected: 
	private: System::Windows::Forms::Label^  Text_nbtn2;
	private: System::Windows::Forms::TextBox^  TextBox1;
	private: System::Windows::Forms::CheckBox^  checkBox1;
	private: System::Windows::Forms::Button^  Start_btn;
	private: System::Windows::Forms::Button^  Zapsat_btn;
	private: System::Windows::Forms::Button^  Reset_btn;

	private: System::ComponentModel::IContainer^  components;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>


#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
		System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
		this->Text_nbtn = (gcnew System::Windows::Forms::Label());
		this->Text_nbtn2 = (gcnew System::Windows::Forms::Label());
		this->TextBox1 = (gcnew System::Windows::Forms::TextBox());
		this->checkBox1 = (gcnew System::Windows::Forms::CheckBox());
		this->Start_btn = (gcnew System::Windows::Forms::Button());
		this->Zapsat_btn = (gcnew System::Windows::Forms::Button());
		this->Reset_btn = (gcnew System::Windows::Forms::Button());
		this->SuspendLayout();
		// 
		// Text_nbtn
		// 
		this->Text_nbtn->AutoSize = true;
		this->Text_nbtn->Location = System::Drawing::Point(6, 9);
		this->Text_nbtn->Name = L"Text_nbtn";
		this->Text_nbtn->Size = System::Drawing::Size(151, 13);
		this->Text_nbtn->TabIndex = 0;
		this->Text_nbtn->Text = L"Zadejte jméno a stiskněte start";
		// 
		// Text_nbtn2
		// 
		this->Text_nbtn2->AutoSize = true;
		this->Text_nbtn2->Location = System::Drawing::Point(151, 9);
		this->Text_nbtn2->Name = L"Text_nbtn2";
		this->Text_nbtn2->Size = System::Drawing::Size(0, 13);
		this->Text_nbtn2->TabIndex = 1;
		// 
		// TextBox1
		// 
		this->TextBox1->Location = System::Drawing::Point(9, 34);
		this->TextBox1->Name = L"TextBox1";
		this->TextBox1->Size = System::Drawing::Size(100, 20);
		this->TextBox1->TabIndex = 2;
		// 
		// checkBox1
		// 
		this->checkBox1->AutoSize = true;
		this->checkBox1->Checked = true;
		this->checkBox1->CheckState = System::Windows::Forms::CheckState::Checked;
		this->checkBox1->Location = System::Drawing::Point(9, 60);
		this->checkBox1->Name = L"checkBox1";
		this->checkBox1->Size = System::Drawing::Size(148, 17);
		this->checkBox1->TabIndex = 3;
		this->checkBox1->Text = L"Spustit testovaný program";
		this->checkBox1->UseVisualStyleBackColor = true;
		// 
		// Start_btn
		// 
		this->Start_btn->Location = System::Drawing::Point(9, 83);
		this->Start_btn->Name = L"Start_btn";
		this->Start_btn->Size = System::Drawing::Size(75, 23);
		this->Start_btn->TabIndex = 4;
		this->Start_btn->Text = L"Start";
		this->Start_btn->UseVisualStyleBackColor = true;
		this->Start_btn->Click += gcnew System::EventHandler(this, &Form1::Start_btn_Click);
		// 
		// Zapsat_btn
		// 
		this->Zapsat_btn->Location = System::Drawing::Point(90, 83);
		this->Zapsat_btn->Name = L"Zapsat_btn";
		this->Zapsat_btn->Size = System::Drawing::Size(75, 23);
		this->Zapsat_btn->TabIndex = 5;
		this->Zapsat_btn->Text = L"Zapsat";
		this->Zapsat_btn->UseVisualStyleBackColor = true;
		this->Zapsat_btn->Click += gcnew System::EventHandler(this, &Form1::Zapsat_btn_Click);
		// 
		// Reset_btn
		// 
		this->Reset_btn->Location = System::Drawing::Point(171, 83);
		this->Reset_btn->Name = L"Reset_btn";
		this->Reset_btn->Size = System::Drawing::Size(75, 23);
		this->Reset_btn->TabIndex = 6;
		this->Reset_btn->Text = L"Reset";
		this->Reset_btn->UseVisualStyleBackColor = true;
		this->Reset_btn->Click += gcnew System::EventHandler(this, &Form1::Reset_btn_Click);
		// 
		// Form1
		// 
		this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
		this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
		this->ClientSize = System::Drawing::Size(254, 122);
		this->Controls->Add(this->Reset_btn);
		this->Controls->Add(this->Zapsat_btn);
		this->Controls->Add(this->Start_btn);
		this->Controls->Add(this->checkBox1);
		this->Controls->Add(this->TextBox1);
		this->Controls->Add(this->Text_nbtn2);
		this->Controls->Add(this->Text_nbtn);
		this->Icon = (cli::safe_cast<System::Drawing::Icon^  >(resources->GetObject(L"$this.Icon")));
		this->Name = L"Form1";
		this->Text = L"Stopky";
		this->ResumeLayout(false);
		this->PerformLayout();

			}

		time_t start, stop;
		char *jmeno;
		int poradi;
		int on;
		
#pragma endregion
	private: System::Void Start_btn_Click(System::Object^  sender, System::EventArgs^  e) {
				
	 if (on != 1)
			{
			start = time(NULL);
			poradi = 1;
			on = 1;

			this->Text_nbtn->Text=gcnew System::String("Stopky byly spuštěny");												//Zapis aktualniho deni
			this->Text_nbtn2->Text=gcnew System::String("");																//misto cisla zapisu nebude nic
			
			jmeno = (char*)Marshal::StringToHGlobalAnsi( System::Convert::ToString(this->TextBox1->Text)).ToPointer();		//ziskani jmena s textboxu
   
			if ((this->checkBox1->Checked) == true)
				{
				system("start \"C:\\Program Files\\Internet Explorer\\iexplore.exe\" \"https://oee.comes.eu/Logon3/Login.aspx\"");
				}
			}
		else
			{
			this->Text_nbtn->Text=gcnew System::String("Nelze spustit znovu bez resetu");
			this->Text_nbtn2->Text=gcnew System::String("");
			}
	 }

private: System::Void Zapsat_btn_Click(System::Object^  sender, System::EventArgs^  e) {
 try
	 {
	 if (on == 1)
			{
			int hod = 0;												//hodiny
			int min = 0;												//minuty
			int sec = 0;												//sekundy
			double rozdil = 0;											//namereny cas os startu do zapisu
			char name[30] = "";
			char format[] = ".txt";
			FILE *zapis;

			stop = time(NULL);
			rozdil = difftime(stop, start);							// rozdil mezi startem a stopem a nasledny vypocet hod. min. sec.
			hod = (int)rozdil/3600;										
			min = ((int)rozdil % 3600)/60;
			sec = (int)rozdil % 60;
			
			if (strlen(jmeno)>= 30)
				{
				throw invalid_argument("Moc dlouhe jmeno");
				}
			
			strcpy(name, jmeno);
			strcat(name, format);

			zapis = fopen(name,"a");									//zapis poradi a casu do souboru
			if (zapis!=NULL)
				{
				fprintf(zapis, "%u\t%u:%u:%u\n", poradi, hod, min, sec);
				}
	
			fclose(zapis);
	
			this->Text_nbtn->Text=gcnew System::String("Byl zaznamenán čas číslo:");
			this->Text_nbtn2->Text=gcnew System::String(Convert::ToString(poradi));			//vypis poradi zapisu do souboru
		
			poradi++;
			}
		else
			{
			this->Text_nbtn->Text=gcnew System::String("Nelze zapsat to co nebylo spuštěno");
			this->Text_nbtn2->Text=gcnew System::String("");
			}
	 }
 catch(invalid_argument &e)
	 {
		cerr << "std::invalid_argument: " << e.what() << endl;
	 }
	 }

private: System::Void Reset_btn_Click(System::Object^  sender, System::EventArgs^  e) {

			 if (on == 1)
				 {
				 on = 0;																	//povolení ke startu
				 this->Text_nbtn->Text=gcnew System::String("Proběhl Reset");
				 this->Text_nbtn2->Text=gcnew System::String("");
				 }
			 else
				 {
				 this->Text_nbtn->Text=gcnew System::String("Reset byl již proveden");
				 this->Text_nbtn2->Text=gcnew System::String("");
				 }
		 }
};
}

