#ifdef WIN32

#include <Windows.h>
#include <GdiPlus.h>
#pragma comment( lib, "gdiplus.lib" )
#include "Fenster.h"
#include "Maus.h"

#endif
#define Global
#include "Model3DList.h"
#include "TexturList.h"
#include "Globals.h"
#include "Thread.h"
#include "Datei.h"
#include "Zeit.h"
#include "DLLRegister.h"

void Framework::initFramework(HINSTANCE__* hInst)
{
	if (istInitialisiert)
		return;
	thRegister = new ThreadRegister();
#ifdef WIN32
	Gdiplus::GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR gdiplusToken;
	Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, 0);
	msgExit = 0;
	MausTrack = 1;
#endif
	for (int i = 0; i < 255; ++i)
		TastenStand[i] = 0;
	for (int i = 0; i < 3; ++i)
		MausStand[i] = 0;
	TexturList::init();
	dlls = new DLLRegister();
	logEnabled = 0;
	logFile = 0;
	_hinst = hInst;
	istInitialisiert = 1;
	debugDX = 0;
}

void Framework::releaseFramework()
{
	if (!istInitialisiert)
		return;
	thRegister->cleanUpClosedThreads();
	dlls->release();
	TexturList::destroy();
	if (logFile)
		logFile->release();
	delete thRegister;
	istInitialisiert = 0;
}

bool Framework::istThreadOk(Thread* t)
{
	return thRegister->isThread(t);
}

// Gibt das Thread Register des Frameworks zur�ck
Framework::ThreadRegister* Framework::getThreadRegister()
{
	return thRegister;
}

#ifdef WIN32

Framework::Punkt Framework::getMausPos()
{
	POINT point;
	GetCursorPos(&point);
	return { point.x, point.y };
}

//! Setzt die Position der Maus auf dem Bildschirm
void Framework::setMausPos(const Punkt& pos)
{
	SetCursorPos(pos.x, pos.y);
}

#endif

bool Framework::getMausStand(int taste)
{
	return MausStand[taste];
}

void Framework::setTastenStand(unsigned char taste, bool st)
{
	TastenStand[taste] = st;
}

bool Framework::getTastenStand(unsigned char taste)
{
	return TastenStand[taste];
}

// Legt fest ob Log Nachrichten gespeichert werden sollen
void Framework::setLogEnabled(bool le)
{
	logEnabled = le;
}

// Speichert eine Zeile in die Logdatei
//  txt: die zu Speichernde Nachricht
void Framework::logLine(char* txt)
{
	if (logEnabled)
	{
		logC.lock();
		if (!logFile)
		{
			Zeit* z = getZeit();
			logFile = new Datei();
			logFile->setDatei(z->getZeit("y-m-d h-i-s.log"));
			logFile->erstellen();
			z->release();
		}
		logFile->open(Datei::Style::schreiben | Datei::Style::lesen | Datei::Style::ende);
		Uhrzeit* uz = getUhrzeit();
		Text* time = uz->getUhrzeit("h:i:s");
		time->append("_");
		time->append((int)GetThreadId(GetCurrentThread()));
		time->append(": ");
		logFile->schreibe(time->getText(), time->getLength());
		time->release();
		logFile->schreibe(txt, textLength(txt));
		logFile->schreibe((char*)"\n", 1);
		logFile->close();
		logC.unlock();
	}
}

// Gibt das DLL Register zur�ck, in dem alle zurzeit dynamisch geladenen DLL Dateien hinterlegt sind
Framework::DLLRegister* Framework::getDLLRegister()
{
	return Framework::dlls;
}

//! Versetzt DirectX in den Debug modus
void Framework::setDebugDX(bool debug)
{
	debugDX = debug;
}

#ifdef WIN32
// gibt eine Referenz auf die Maus zur�ck
Framework::Maus& Framework::getMaus()
{
	return Framework::MausZeiger;
}

//! setzt den Zustand der Maus auf sichtbar oder unsichtbar
void Framework::setShowCursor(bool visible)
{
	CURSORINFO info = { 0 };
	info.cbSize = sizeof(CURSORINFO);
	GetCursorInfo(&info);
	if ((info.flags != 0) == (visible == 0))
		ShowCursor(visible);
}

#endif