#include "cTexture.hpp"

/*** Custom Header Files ***/
#include "../../VideoEngine/cRenderer.hpp"
#include "../../FXEngine/cGFX.hpp"
#include "GUIUtility.hpp"

using GUIHelpers::cTexture;

cTexture::cTexture()
{}

cTexture::~cTexture()
{}

///Functions

//private
SDL_Texture* cTexture::GenerateTexture( const SDL_Rect& dimensions, TextTypeEngine::cText* text /*= nullptr*/ )
{
	SDL_Rect t = text->getWH();
	SDL_Rect centerText = { 0, 0, 0, 0 };
	SDL_Rect centerButton = { 0, 0, 0, 0 };
	SDL_Rect dim = dimensions;

	///Find the min padding around the text.
// 	if (dim.w < (t.w + GUIHelpers::default::text::PADDING.x + GUIHelpers::default::text::PADDING.w) )
// 		dim.w += (t.w + GUIHelpers::default::text::PADDING.x + GUIHelpers::default::text::PADDING.w);
// 
// 	if (dim.h < (t.h + GUIHelpers::default::text::PADDING.y + GUIHelpers::default::text::PADDING.z) )
// 		dim.h += (t.h + GUIHelpers::default::text::PADDING.y + GUIHelpers::default::text::PADDING.z);

	///Find the center of the button	
	centerButton.x = dim.w / 2;
	centerButton.y = dim.h / 2;

	///Find the center of the text
	centerText.x = t.w / 2;
	centerText.y = t.h / 2;

	if (centerText.x < centerButton.x)
		t.x = centerButton.x - centerText.x;
	
	if (centerText.y < centerButton.y)
		t.y = centerButton.y - centerText.y;
	
	SDL_Texture* temptext = VideoEngine::cRenderer::Inst().NewTexture(dim.w, dim.h, SDL_TEXTUREACCESS_TARGET );

	GUIHelpers::RGBA colour = { 100, 100, 100, 255 };

	FXEngine::cGFX::Inst().RoundedBox(dim, 0, colour, temptext);

	
	VideoEngine::cRenderer::Inst().RenderToTexture(text->getImage(), nullptr, temptext, &t);

	
	//cRenderer::Inst().Render(temptext, nullptr, nullptr);

	//cRenderer::Inst().RenderToTexture(temptext, nullptr, text->getImage(), nullptr);

	/*SDL_Surface* tempsurface = SDL_CreateRGBSurface(cVideo::Inst().getVideoSettings(), dimensions.w, dimensions.h, cVideo::Inst().getColour(),
		0, 0, 0, 0);

	SDL_FillRect(tempsurface, nullptr, SDL_MapRGB(tempsurface->format, 0, 255, 0));

	/*if (roundedBoxRGBA(tempsurface, dimensions.x, dimensions.y, dimensions.w, dimensions.h, 10, 255, 255, 0, 255) != 0)
		cerr << "Unable to generate GUI Texture." << endl << SDL_GetError() << endl;*/

	
	/*if (text != nullptr) {
		SDL_Rect srcrect, dstrect = dimensions;
		cVideo::Inst().Render(text->getImage(), &srcrect, temptext, &dstrect);
	}

	this->setImage(temptext);
	this->setTransparent();*/

	setImage(temptext);

	return temptext;
}