#include "const.h"
#include "text.h"
#include <iostream>
#include <fstream>

// Constructor
Text::Text(std::string bitmapFile, std::string textFile, SDL_Renderer *renderer)
{
	texture = new LTexture();
	loadTextureFromFile(texture, bitmapFile, renderer);

	SDL_Rect rect = {0,0,0,0};
	mSprite = new Sprite(rect, texture, renderer);
	mSprite->setTexture(texture);
	mSprite->setRenderer(renderer);
	mFile = textFile;

	init();
}

// Destructor
Text::~Text()
{
	texture->unload();
	delete texture;
	texture = nullptr;

	delete mSprite;
	mSprite = nullptr;
}

// Inicializador
void Text::init()
{
	// Inicializa a cero el vector con las coordenadas
	for (int i = 0; i < 128; i++)
	{
		mOffset[i].x = 0;
		mOffset[i].y = 0;
		mOffset[i].w = 0;
	}

	// Carga los offsets desde el fichero
	initOffsetFromFile();

	// Inicia los valores del sprite que dibuja las letras
	mSprite->setWidth(mBoxWidth);
	mSprite->setHeight(mBoxHeight);
	mSprite->setPosX(0);
	mSprite->setPosY(0);
	mSprite->setSpriteClip(0, 0, mSprite->getWidth(), mSprite->getHeight());

	// Establece las coordenadas para cada caracter ascii de la cadena y su ancho
	for (int i = 32; i < 128; i++)
	{
		mOffset[i].x = ((i - 32) % 15) * mBoxWidth;
		mOffset[i].y = ((i - 32) / 15) * mBoxHeight;
	}
}

// Escribe texto en pantalla
void Text::write(int x, int y, std::string text, int kerning, int lenght)
{
	Uint16 shift = 0;

	if (lenght == -1)
		lenght = text.length();

	for (int i = 0; i < lenght; ++i)
	{
		mSprite->setSpriteClip(mOffset[int(text[i])].x, mOffset[int(text[i])].y, mSprite->getWidth(), mSprite->getHeight());
		mSprite->setPosX(x + shift);
		mSprite->setPosY(y);
		mSprite->render();
		shift += (mOffset[int(text[i])].w + kerning);
	}
}

// Escribe el texto con colores
void Text::writeColored(int x, int y, std::string text, color_t color, int kerning, int lenght)
{
	mSprite->getTexture()->setColor(color.r, color.g, color.b);
	write(x, y, text, kerning, lenght);
	mSprite->getTexture()->setColor(255, 255, 255);
}

// Escribe el texto con sombra
void Text::writeShadowed(int x, int y, std::string text, color_t color, Uint8 shadowDistance, int kerning, int lenght)
{
	mSprite->getTexture()->setColor(color.r, color.g, color.b);
	write(x + shadowDistance, y + shadowDistance, text, kerning, lenght);
	mSprite->getTexture()->setColor(255, 255, 255);
	write(x, y, text, kerning, lenght);
}

// Escribe el texto centrado en un punto x
void Text::writeCentered(int x, int y, std::string text, int kerning, int lenght)
{
	x -= (Text::lenght(text, kerning) / 2);
	write(x, y, text, kerning, lenght);
}

// Escribe texto con extras
void Text::writeDX(Uint8 flags, int x, int y, std::string text, int kerning, color_t textColor, Uint8 shadowDistance, color_t shadowColor, int lenght)
{
	const bool centered = ((flags & TXT_CENTER) == TXT_CENTER);
	const bool shadowed = ((flags & TXT_SHADOW) == TXT_SHADOW);
	const bool colored = ((flags & TXT_COLOR) == TXT_COLOR);
	const bool stroked = ((flags & TXT_STROKE) == TXT_STROKE);

	if (centered)
		x -= (Text::lenght(text, kerning) / 2);

	if (shadowed)
		writeColored(x + shadowDistance, y + shadowDistance, text, shadowColor, kerning, lenght);

	if (stroked)
	{
		writeColored(x + shadowDistance, y + shadowDistance, text, shadowColor, kerning, lenght);
		writeColored(x - shadowDistance, y + shadowDistance, text, shadowColor, kerning, lenght);
		writeColored(x + shadowDistance, y - shadowDistance, text, shadowColor, kerning, lenght);
		writeColored(x - shadowDistance, y - shadowDistance, text, shadowColor, kerning, lenght);
		
		writeColored(x, y + shadowDistance, text, shadowColor, kerning, lenght);
		writeColored(x, y - shadowDistance, text, shadowColor, kerning, lenght);
		writeColored(x + shadowDistance, y, text, shadowColor, kerning, lenght);
		writeColored(x - shadowDistance, y, text, shadowColor, kerning, lenght);
	}

	if (colored)
		writeColored(x, y, text, textColor, kerning, lenght);
	else
		write(x, y, text, kerning, lenght);
}

// Obtiene la longitud en pixels de una cadena
Uint16 Text::lenght(std::string text, int kerning)
{
	Uint16 shift = 0;

	for (int i = 0; i < (int)text.length(); ++i)
		shift += (mOffset[int(text[i])].w + kerning);

	return shift;
}

// Inicializa el vector de offsets desde un fichero
void Text::initOffsetFromFile()
{
	std::ifstream rfile(mFile);
	if (rfile.is_open() && rfile.good())
	{
		std::string buffer;

		// Lee los dos primeros valores del fichero
		std::getline(rfile, buffer);
		std::getline(rfile, buffer);
		mBoxWidth = std::stoi(buffer);

		std::getline(rfile, buffer);
		std::getline(rfile, buffer);
		mBoxHeight = std::stoi(buffer);

		// lee el resto de datos del fichero
		int index = 32;
		int line_read = 0;
		while (std::getline(rfile, buffer))
		{
			// Almacena solo las lineas impares
			if (line_read % 2 == 1)
				mOffset[index++].w = std::stoi(buffer);
			
			// Limpia el buffer
			buffer.clear();
			line_read++;
		};
	}
}

// Devuelve el valor de la variable
Uint8 Text::getCharacterWidth()
{
	return mBoxWidth;
}