#include "cText.hpp"

/*** SDL Header Files ***/
#include <SDL2_gfxPrimitives.h>
#include <SDL2_gfxPrimitives_font.h>
#include <SDL2_rotozoom.h>

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

using TextTypeEngine::cText;
using VideoEngine::cRenderer;
using FXEngine::cGFX;
using UtilityEngine::cUtility;

cText::cText( const cString& text, const unsigned long int size /*= 8*/ )
	: mpp_font(nullptr), m_text(text), m_size(size), m_render(eRender::Solid)
{
	White();
}

cText::cText( SDL_Colour& colour, TextTypeEngine::cFont** font /*= nullptr*/, const cString& text /*= ""*/, const unsigned long int size /*= 8*/, const eRender& render /*= eRender::Solid*/ )
	: mpp_font(font), m_text(text), m_size(size), m_render(render)
{
	setColour(colour);
}

cText::cText( TextTypeEngine::cFont** font /*= nullptr*/, const cString& text /*= ""*/, const unsigned long int size /*= 8*/,
			const unsigned long int red /*= 0*/, const unsigned long int green /*= 0*/, const unsigned long int blue /*= 0*/, const eRender& render /*= eRender::Solid*/ )
			: mpp_font(font), m_text(text), m_size(size), m_render(render)
{
	setColour(red, green, blue);
}

cText::cText( const cText& copy )
	: mpp_font(nullptr), m_text(copy.getText()), m_render(copy.getRender())
{
	setColour(copy.getColour());
}

cText::~cText()
{}

///Function
void cText::White()
{
	setColour(255, 255, 255);
}

void cText::Black()
{
	setColour();
}

void cText::Blue()
{
	setColour(0, 0, 255);
}

void cText::Green()
{
	setColour(0, 255);
}

void cText::Red()
{
	setColour(255);
}

///Sets
void cText::setSize(const unsigned long int size /*= 8*/)
{
	m_size = size;
	if ((mpp_font != nullptr) && ((*mpp_font) != nullptr))
		(*mpp_font)->setSize(size);
}

void cText::setFont( TextTypeEngine::cFont** font )
{
	mpp_font = font;
	Text();
}

void cText::setText( const cString& text )
{
	if (m_text != text) {
		m_text = text;
		Text();
	}
}

void cText::setColour( const SDL_Colour& colour )
{
	m_colour = colour;
	Text();
}

void cText::setColour( const unsigned long int red /*= 0*/, const unsigned long int green /*= 0*/, const unsigned long int blue /*= 0*/, unsigned long int alpha /*= 255*/ )
{
	SDL_Colour colour = {Uint8(red), Uint8(green), Uint8(blue), Uint8(alpha)};
	setColour(colour);
}

void cText::setRender( const eRender& render /*= eRender::Solid*/ )
{
	m_render = render;
	Text();
}

///Gets
const unsigned long int cText::getSize() const
{
	unsigned long int rtn = m_size;
	if ((mpp_font != nullptr) && ((*mpp_font) != nullptr))
		return (*mpp_font)->getSize();
	return rtn;
}

TextTypeEngine::cFont* cText::getFont() const
{
	return (*mpp_font);
}

const cString& cText::getText() const
{
	return m_text;
}

const SDL_Colour& cText::getColour() const
{
	return m_colour;
}

void cText::getColour( unsigned long int& red, unsigned long int& green, unsigned long int& blue, unsigned long int& alpha ) const
{
	red = m_colour.r;
	green = m_colour.g;
	blue = m_colour.b;
	alpha = m_colour.a;
}

const cText::eRender& cText::getRender() const
{
	return m_render;
}

//Private
void cText::Text()
{
	if((mpp_font != nullptr) && (*mpp_font != nullptr)) {
		SDL_Surface* sur = nullptr;
		switch (m_render)
		{
		case Solid:
			if ((sur = TTF_RenderText_Solid((*mpp_font)->getFont(m_size), m_text.c_str(), m_colour) ) == nullptr)
				cUtility::Inst().Message("Unable to render text. TTF_RenderText_Solid():", __AT__, cUtility::eTypeSDL::TTF);
			break;
		case Shaded:
			//sur = TTF_RenderText_Shaded(mp_font, text.c_str(), m_colour, 
			break;
		case Blended:
			if ((sur = TTF_RenderText_Blended((*mpp_font)->getFont(m_size), m_text.c_str(), m_colour) ) == nullptr)
				cUtility::Inst().Message("Unable to render text. TTF_RenderText_Blended():", __AT__, cUtility::eTypeSDL::TTF);
			break;
		}
		setImage(sur);
	} else {
		SDL_Rect pos = { 0, 0, 0, 0 };
		//m_size == 25;
		SDL_Texture* texture = cRenderer::Inst().NewTexture(8 * m_text.length(), 8, SDL_TEXTUREACCESS_TARGET);
		SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
		
		//int t = SDL_SetTextureColorMod(texture, 0, 255, 0);

		// 376840196

		
		
		//t = SDL_SetTextureAlphaMod(texture, 0);
		//SDL_fill
		//cRenderer::Inst().SetRenderTarget(texture);
		//SDL_Colour col = { 0, 255, 255, 255};
		//SDL_Rect re= { 0, 0, 100, 100};
		//cGFX::Inst().RoundedRectangle(pos, 4, col, texture);
		//cRenderer::Inst().RenderDrawColor(col);
		//cRenderer::Inst().RenderDrawRect(re, texture);

		//cGFX::Inst().RoundedRectangle(pos, 4, col, texture);

		//cRenderer::Inst().Render( texture, nullptr, nullptr);
		//SDL_RenderCopy(cRenderer::Inst().getRenderer(), texture, nullptr, nullptr);

		//cRenderer::Inst().Render(texture, nullptr, nullptr);
		//cRenderer::Inst().ResetRenderTarget();

		if (texture != nullptr) {
			
			//cGFX::Inst().StringDefault(m_text, pos, m_colour);
			cGFX::Inst().StringDefault(m_text, pos, m_colour, texture);
			//cGFX::Inst().ZoomIn(texture, m_size * m_text.length(), m_size, SMOOTHING_ON);
			setImage(texture);
		}
	}
}
