#include <windows.h>
#include "cs_holder.h"
#include "convert.h"
#include "computing.h"
#include "robotFunctions.h"
#include "connecting.h"
#include "writeLog.h"

CRITICAL_SECTION IOSection;

int main()
{
	InitializeCriticalSection(&GlobalCriticalSection);
	InitializeCriticalSection(&clientDataSection);
	InitializeCriticalSection(&IOSection);

	DWORD dwThreadId;
	LPVOID argument = 0;

	HANDLE vypocet = CreateThread(NULL,0,computeSimulation,argument,0,&dwThreadId);
	HANDLE writeOutput = CreateThread(NULL,0,writeLog,argument,0,&dwThreadId);
	HANDLE naslouchani = CreateThread(NULL,0,poslouchamClienta,argument,0,&dwThreadId);
	HANDLE naslouchaniViewer = CreateThread(NULL,0,poslouchamViewer,argument,0,&dwThreadId);

	std::string klicKonec = "a";
	std::string textKonec;
	while(textKonec != klicKonec)
	{
		EnterCriticalSection(&IOSection);
		std::cout << "Stiskem klaves '" << klicKonec << "' se pocka na pripojeni posledniho clienta a potom se vypene server...\n" << std::endl;
		LeaveCriticalSection(&IOSection);
		std::cin >> textKonec;
	}
	konec = true;

	WaitForSingleObject(vypocet,INFINITE);
	WaitForSingleObject(naslouchani,1000);
	CloseHandle(naslouchani);
	CloseHandle(vypocet);

	return 0;
}