#include "cImageFile.hpp"

/*** Custom Header Files ***/
#include "cRenderer.hpp"
#include "../UtilityEngine/cUtility.hpp"

using VideoEngine::cImageFile;
using UtilityEngine::cUtility;

cImageFile::cImageFile( const cString& filename, const cString& dir /*= ""*/, const bool transparent /*= false*/,
	const unsigned char red /*= 0*/, const unsigned char blue /*= 0*/, const unsigned char green /*= 255*/,
	const unsigned char translevel /*= 255*/, const bool isSurface /*= false*/ )
	: cImage(transparent, red, blue, green, translevel, isSurface), m_dir(dir), m_fileName(filename)
{
	if (m_fileName != (char*)"")
		LoadImage();
}

cImageFile::cImageFile( const cImageFile& copy, const bool surfaceCopy /*= true*/ )
	: cImage(copy)
{}

cImageFile::~cImageFile()
{}

/// Sets
void cImageFile::setDir( const cString& dir )
{
	m_dir = dir;
}

void cImageFile::setFileName( const cString& filename )
{
	m_fileName = filename;
	LoadImage();
}

void cImageFile::setFileNameandDir( const cString& filename, const cString& dir /*= ""*/ )
{
	setDir(dir);
	setFileName(filename);
}

const cString& cImageFile::getDir() const
{
	return m_dir;
}

const cString& cImageFile::getFileName() const
{
	return m_fileName;
}

/// private
/// Functions
void cImageFile::LoadImage()
{
	SDL_Texture* tempText = nullptr;
	SDL_Surface* tempSurface = nullptr;

	if (m_fileName != "")
	{
		cString temp = m_dir + m_fileName;

		if (this->getIsSurface() == false) {
			if ((tempText = IMG_LoadTexture(cRenderer::Inst().getRenderer(), temp.c_str())) == nullptr)
				cUtility::Inst().Message("Unable to load necessary image file. " + temp + " IMG_LoadTexture():", __AT__, cUtility::eTypeSDL::IMAGE);
			else
				this->setImage(tempText);
		}
		else {
			if ((tempSurface = IMG_Load(temp.c_str())) == nullptr)
				cUtility::Inst().Message("Unable to load necessary image file. " + temp + " IMG_Load():", __AT__, cUtility::eTypeSDL::IMAGE);
			else
				this->setImage(tempSurface);
		}
	}
}