#include "cUtility.hpp"

/*** SDL Header Files ***/
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <SDL_net.h>

using UtilityEngine::cUtility;

/*static*/ cUtility* cUtility::sp_inst = nullptr;

//private:
cUtility::cUtility()
{}

cUtility::~cUtility()
{}

//public:
// cUtility& cUtility::operator=( const cUtility& copy )
// {
// 	if (this != &copy)
// 	{
// 	}
// 
// 	return *this;
// }

///Functions
/*static*/ cUtility& cUtility::Inst()
{
	if (sp_inst == nullptr)
		sp_inst = new cUtility();
	return *sp_inst;
}

/*static*/ void cUtility::Delete()
{
	delete sp_inst;
	sp_inst = nullptr;
}

void cUtility::Message( const cString& msg, const cString& debugInfo /*= ""*/, const eTypeSDL typeSDL /*= eTypeSDL::None*/ ) const
{
	cString sdlerror;
	switch (typeSDL)
	{
	case NONE:
		break;
	case SDL:
		sdlerror = SDL_GetError();
		break;
	case TTF:
		sdlerror = TTF_GetError();
		break;
	case IMAGE:
		sdlerror = IMG_GetError();
		break;
	case MIXER:
		sdlerror = Mix_GetError();
		break;
	case NET:
		sdlerror = SDLNet_GetError();
		break;
	case GFX:
		sdlerror = SDL_GetError();
		break;
	default:
		break;
	}
	SDL_ClearError();
	if (debugInfo.size() > 0)
		fprintf(stderr, "Error in %s %s %s \n", debugInfo.substr((1 + debugInfo.char_find('\\')), debugInfo.length()).c_str(), msg.c_str(), sdlerror.c_str());
	else
		fprintf(stderr, "%s\n", msg.c_str());
}

void cUtility::PrintVersion( const eTypeSDL typeSDL /*= eTypeSDL::ALL*/ ) const
{
	SDL_version compiled_version;
	SDL_version running_version;

	cString type = "";

	switch (typeSDL)
	{
	case ALL:
		for ( int num = SDL; num <= NET; num++)
			PrintVersion((eTypeSDL)num);
		break;
	case SDL:
		SDL_VERSION(&compiled_version);
		SDL_GetVersion(&running_version);
		type = "SDL";
		break;
	case TTF:
		SDL_TTF_VERSION(&compiled_version);
		running_version = *TTF_Linked_Version();
		type = "SDL_TTF";
		break;
	case IMAGE:
		SDL_IMAGE_VERSION(&compiled_version);
		running_version = *IMG_Linked_Version();
		type = "SDL_IMAGE";
		break;
	case MIXER:
		SDL_MIXER_VERSION(&compiled_version);
		running_version = *Mix_Linked_Version();
		type = "SDL_MIXER";
		break;
	case NET:
		SDL_NET_VERSION(&compiled_version);
		running_version = *SDLNet_Linked_Version();
		type = "SDL_NET";
		break;
	default:
		break;
	}
	if (typeSDL != ALL) {
		VersionParser("Compiled", compiled_version, type);
		VersionParser("Running", running_version, type);
	}
}

///Private:
///Functions
void cUtility::VersionParser( const cString& runCompiled, const SDL_version& version, const cString& type ) const
{
	fprintf(stderr, "%s with %s version: %u.%u.%u \n", runCompiled.c_str(), type.c_str(), version.major, version.minor, version.patch);
}