#include "cRenderer.hpp"

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

using VideoEngine::cRenderer;
using VideoEngine::cVideo;
using UtilityEngine::cUtility;

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

//private:
cRenderer::cRenderer()
{
	Setup();
}

cRenderer::~cRenderer()
{
	CleanUp();
}

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

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

const bool cRenderer::Setup()
{
	bool rtn = false;
	if (cVideo::Inst().IsInit() == false)
		cUtility::Inst().Message("Unable to setup until SDL_Video is initialized.");
	else {
		mp_renderer = cVideo::Inst().CreateRender();
		if (mp_renderer != nullptr)
			rtn = true;
	}
	return rtn;
}

void cRenderer::CleanUp()
{
	SDL_DestroyRenderer(mp_renderer);
	mp_renderer = nullptr;
}

void cRenderer::Render( SDL_Texture* texture, SDL_Rect* srcrect, SDL_Rect* dstrect )
{
	cRenderer::Render(texture, srcrect, mp_renderer, dstrect);
}

void cRenderer::Render( SDL_Surface* surface, SDL_Rect* srcrect, SDL_Rect* dstrect )
{
	SDL_Texture* texture = SurfaceToTexture(surface);
	Render( texture, srcrect, dstrect );
	SDL_DestroyTexture(texture);
}

void cRenderer::Render( SDL_Surface* src, const SDL_Rect* srcrect, SDL_Surface* dst, SDL_Rect* dstrect )
{
	if (SDL_BlitSurface(src, srcrect, dst, dstrect) < 0)
		cUtility::Inst().Message("Unable to BlitSurface. SDL_BlitSurface():", __AT__, cUtility::eTypeSDL::SDL);
}

void cRenderer::Render( SDL_Texture* src, const SDL_Rect* srcrect, SDL_Surface* dst, SDL_Rect* dstrect )
{
	src;
	srcrect;
	dst;
	dstrect;
	//???????
}

void cRenderer::Render( SDL_Texture* src, const SDL_Rect* srcrect, SDL_Renderer* dst, SDL_Rect* dstrect )
{
	if (SDL_RenderCopy(dst, src, srcrect, dstrect) < 0)
		cUtility::Inst().Message("Unable to copy Texture to Renderer. SDL_RenderCopy():", __AT__, cUtility::eTypeSDL::SDL);
}

void cRenderer::RenderToTexture( SDL_Texture* src, SDL_Rect* srcrect, SDL_Texture* dst, SDL_Rect* dstrect )
{
	SetRenderTarget(dst);
	Render(	src, srcrect, dstrect );
	ResetRenderTarget();
}

void cRenderer::RenderDrawColor( const SDL_Colour& colour )
{
	if (SDL_SetRenderDrawColor(mp_renderer, colour.r, colour.g, colour.b, colour.a) < 0)
		cUtility::Inst().Message("Unable to set render draw colour. SDL_SetRenderDrawColor():", __AT__, cUtility::eTypeSDL::SDL);
}

void cRenderer::RenderDrawRect( const SDL_Rect& rect, SDL_Texture* dst /*= nullptr */ )
{
	SetRenderTarget(dst);
	if (SDL_RenderFillRect(mp_renderer, &rect) < 0)
		cUtility::Inst().Message("Unable to draw rectangle. SDL_RenderFillRect():", __AT__, cUtility::eTypeSDL::SDL);
	ResetRenderTarget();
}

void cRenderer::ScreenShot( const cString& filename, const cString& dir /*= ""*/ )
{
	SDL_Surface* infoSurface = SDL_GetWindowSurface(VideoEngine::cVideo::Inst().getWindow());
	SDL_Surface* saveSurface = NewSurface();

	if (infoSurface == NULL) {
		cUtility::Inst().Message("Failed to create info surface from window.\n", __AT__, cUtility::eTypeSDL::SDL);
	} else {
		if (SDL_RenderReadPixels(mp_renderer, &infoSurface->clip_rect, infoSurface->format->format, saveSurface->pixels, infoSurface->w * infoSurface->format->BytesPerPixel) != 0)
			cUtility::Inst().Message("Failed to read pixel data from SDL_Renderer object.\n", __AT__, cUtility::eTypeSDL::SDL);
		else
			SaveSurface(saveSurface, filename, dir);
		SDL_FreeSurface(infoSurface);
		infoSurface = NULL;
	}
	SDL_FreeSurface(saveSurface);
	saveSurface = NULL;
}


void cRenderer::SaveSurface( SDL_Surface* surface, const cString& fileName, const cString& dir /*= ""*/ )
{
	cString temp = dir + fileName;
	if (SDL_SaveBMP(surface, temp.c_str()) < 0)
		cUtility::Inst().Message("Unable to save Surface. SDL_SaveBMP()", __AT__, cUtility::eTypeSDL::SDL);
}

void cRenderer::SaveTexture( SDL_Texture* texture, const cString& fileName, const cString& dir /*= ""*/ )
{
	SDL_Surface* surface = TextureToSurface(texture);

	if (surface != nullptr)
		SaveSurface(surface, fileName, dir);
	else
		cUtility::Inst().Message("Unable to create Surface from Texture. TextureToSurface():", __AT__);

	SDL_FreeSurface(surface);
}

SDL_Texture* cRenderer::SurfaceToTexture( SDL_Surface* surface )
{
	SDL_Texture* rtn = nullptr;
	rtn = SDL_CreateTextureFromSurface( mp_renderer, surface );
	if ( rtn == nullptr)
		cUtility::Inst().Message("Unable to create Texture. SDL_CreateTextureFromSurface():", __AT__, cUtility::eTypeSDL::SDL);

	return rtn;
}

SDL_Surface* cRenderer::TextureToSurface( SDL_Texture* texture )
{
	SDL_Surface* rtn = nullptr;

	int w = 0;
	int h = 0;

	if (SDL_QueryTexture(texture, nullptr, nullptr, &w, &h) < 0)
		cUtility::Inst().Message("Unable to Query Texture. SDL_QueryTexture():", __AT__, cUtility::eTypeSDL::SDL);

	rtn = NewSurface(w, h);

	void* srcPixels = nullptr;
	int srcPitch = 0;
	
	//Lock texture for manipulation
	if (SDL_LockTexture( texture, nullptr, &srcPixels, &srcPitch ) < 0)
		cUtility::Inst().Message("Unable to lock texture. SDL_LockTexture():", __AT__, cUtility::eTypeSDL::SDL);

	if (SDL_LockSurface(rtn))
		cUtility::Inst().Message("Unable to lock surface rtn. SDL_LockSurface():", __AT__, cUtility::eTypeSDL::SDL);

	//Copy loaded/formatted surface pixels
	memcpy( rtn->pixels, srcPixels, srcPitch * h );

	//Unlock texture to update
	SDL_UnlockSurface(rtn);
	SDL_UnlockTexture( texture );

	srcPixels = nullptr;

	return rtn;
}

void cRenderer::CopyTexture( SDL_Texture* src, SDL_Texture* dst )
{
	void* srcPixels = nullptr;
	int srcPitch = 0;

	void* dstPixels = nullptr;
	int dstPitch = 0;

	int height = 0;

	if (SDL_QueryTexture( src, nullptr, nullptr, nullptr, &height ) < 0)
		cUtility::Inst().Message("Unable to query texture. SDL_QueryTexture():", __AT__, cUtility::eTypeSDL::SDL);

	//Lock texture for manipulation
	if (SDL_LockTexture( src, nullptr, &srcPixels, &srcPitch ) < 0)
		cUtility::Inst().Message("Unable to lock texture src. SDL_LockTexture():", __AT__, cUtility::eTypeSDL::SDL);

	if (SDL_LockTexture( dst, nullptr, &dstPixels, &dstPitch ) < 0)
		cUtility::Inst().Message("Unable to lock texture dst. SDL_LockTexture():", __AT__, cUtility::eTypeSDL::SDL);

	//Copy loaded/formatted surface pixels
	memcpy( dstPixels, srcPixels, srcPitch * height );

	//Unlock texture to update
	SDL_UnlockTexture( dst );
	SDL_UnlockTexture( src );
	srcPixels = nullptr;
	dstPixels = nullptr;

}

SDL_Surface* cRenderer::CopySurface( SDL_Surface* src )
{
	return SDL_ConvertSurface( src, src->format, src->flags );
}

SDL_Surface* cRenderer::NewSurface( long int width /*= -1*/, long int height /*= -1*/ ) const
{
	if (width < 0)
		width = cVideo::Inst().getWidth();
	if (height < 0)
		height = cVideo::Inst().getHeight();

	SDL_Surface* rtn = SDL_CreateRGBSurface(cVideo::Inst().getVideoSettings(), width, height, cVideo::Inst().getColour(), cVideo::Inst().getRmask(), cVideo::Inst().getGmask(), cVideo::Inst().getBmask(), cVideo::Inst().getAmask());

	if (rtn == nullptr)
		cUtility::Inst().Message("Unable to create surface. SDL_CreateRGBSurface():", __AT__, cUtility::eTypeSDL::SDL);

	return rtn;
}

SDL_Texture* cRenderer::NewTexture( long int width, long int height, const unsigned long int access /*= SDL_TEXTUREACCESS_TARGET*/ ) const
{	
	if (width < 0)
		width = cVideo::Inst().getWidth();
	if (height < 0)
		height = cVideo::Inst().getHeight();

	SDL_Texture* rtn = SDL_CreateTexture(mp_renderer, SDL_PIXELFORMAT_ABGR8888, access, width, height);
	if (rtn == nullptr)
		cUtility::Inst().Message("Unable to create texture. SDL_CreateTexture:", __AT__, cUtility::eTypeSDL::SDL);
	return rtn;
}

void cRenderer::SetRenderTarget( SDL_Texture* target /*= nullptr*/ )
{
	if (SDL_SetRenderTarget(mp_renderer, target) < 0)
		cUtility::Inst().Message("Unable to set render target to dst. SDL_SetRenderTarget():", __AT__, cUtility::eTypeSDL::SDL);
}

void cRenderer::ResetRenderTarget()
{
	if (SDL_SetRenderTarget(mp_renderer, nullptr) < 0)
		cUtility::Inst().Message("Unable to set render target to mp_rend. SDL_SetRenderTarget():", __AT__, cUtility::eTypeSDL::SDL);
}

SDL_Renderer* cRenderer::getRenderer()
{
	return mp_renderer;
}
