#include "TextSprite.h"

#include "SDL.h"
#include "Visualisation.h"
#include "AlphabetData.h"
#include "GameConfig.h"

#include "GameException.h"

TextSprite::TextSprite(const SpriteID &id, const std::string &message, const ColourRGB &colour)
: Sprite(id)
{
	m_width = message.size() * CHARACTER_WIDTH;
	m_height = CHARACTER_HEIGHT;

	m_numFrames = 1; //Only one frame for procedural boxes so far, animation could be added later.
	SDL_Surface* pScreen = Visualisation::getSingleton()->GetScreen();
	m_pFrames = SDL_CreateRGBSurface(SDL_HWSURFACE, m_width, m_height, pScreen->format->BitsPerPixel, pScreen->format->Rmask, pScreen->format->Gmask, pScreen->format->Bmask, pScreen->format->Amask);

	if(!m_pFrames)
	{
		throw GameException(eMajorError, "TextSprite: Constructor failed due to failed SDL_CreateRGBSurface() call.");
		return;
	}

	ColourRGB transColour = Visualisation::getSingleton()->GetTransparencyKey();
	Uint32 key = SDL_MapRGB(m_pFrames->format, transColour.r, transColour.g, transColour.b);
	SDL_SetColorKey(m_pFrames, SDL_SRCCOLORKEY, key);

	int xOffset = 0;
	for(size_t i = 0; i < message.size(); i++)
	{
		for(size_t x = 0; x < CHARACTER_WIDTH; x++)
		{
			for(size_t y = 0; y < CHARACTER_HEIGHT; y++)
			{
				int charDataIndex = (y * CHARACTER_WIDTH) + x;
				int pixelDataIndex = (y * m_width) + (x + xOffset);

				const bool* pCharData = GetCharacterArray(message[i]);
				if(!pCharData)
				{
					throw GameException(eMinorError, "TextSprite: GetCharacterArray() failed on character: %c", message[i]);
					return;
				}

				if(pCharData[charDataIndex])
					WritePixel(m_pFrames, x + xOffset, y, colour);
				else
					WritePixel(m_pFrames, x + xOffset, y, transColour);
			}
		}

		xOffset += CHARACTER_WIDTH;
	}
}

const bool* TextSprite::GetCharacterArray(char letter)
{
	const bool* pCharacter = 0;
	switch(letter)
	{
		case ' ':
			pCharacter = gCharacterSpace;
			break;
		case 'A':
		case 'a':
			pCharacter = gCharacterA;
			break;
		case 'B':
		case 'b':
			pCharacter = gCharacterB;
			break;
		case 'C':
		case 'c':
			pCharacter = gCharacterC;
			break;
		case 'D':
		case 'd':
			pCharacter = gCharacterD;
			break;
		case 'E':
		case 'e':
			pCharacter = gCharacterE;
			break;
		case 'F':
		case 'f':
			pCharacter = gCharacterF;
			break;
		case 'G':
		case 'g':
			pCharacter = gCharacterG;
			break;
		case 'H':
		case 'h':
			pCharacter = gCharacterH;
			break;
		case 'I':
		case 'i':
			pCharacter = gCharacterI;
			break;
		case 'J':
		case 'j':
			pCharacter = gCharacterJ;
			break;
		case 'K':
		case 'k':
			pCharacter = gCharacterK;
			break;
		case 'L':
		case 'l':
			pCharacter = gCharacterL;
			break;
		case 'M':
		case 'm':
			pCharacter = gCharacterM;
			break;
		case 'N':
		case 'n':
			pCharacter = gCharacterN;
			break;
		case 'O':
		case 'o':
			pCharacter = gCharacterO;
			break;
		case 'P':
		case 'p':
			pCharacter = gCharacterP;
			break;
		case 'Q':
		case 'q':
			pCharacter = gCharacterQ;
			break;
		case 'R':
		case 'r':
			pCharacter = gCharacterR;
			break;
		case 'S':
		case 's':
			pCharacter = gCharacterS;
			break;
		case 'T':
		case 't':
			pCharacter = gCharacterT;
			break;
		case 'U':
		case 'u':
			pCharacter = gCharacterU;
			break;
		case 'V':
		case 'v':
			pCharacter = gCharacterV;
			break;
		case 'W':
		case 'w':
			pCharacter = gCharacterW;
			break;
		case 'X':
		case 'x':
			pCharacter = gCharacterX;
			break;
		case 'Y':
		case 'y':
			pCharacter = gCharacterY;
			break;
		case 'Z':
		case 'z':
			pCharacter = gCharacterZ;
			break;
		case '0':
			pCharacter = gCharacter0;
			break;
		case '1':
			pCharacter = gCharacter1;
			break;
		case '2':
			pCharacter = gCharacter2;
			break;
		case '3':
			pCharacter = gCharacter3;
			break;
		case '4':
			pCharacter = gCharacter4;
			break;
		case '5':
			pCharacter = gCharacter5;
			break;
		case '6':
			pCharacter = gCharacter6;
			break;
		case '7':
			pCharacter = gCharacter7;
			break;
		case '8':
			pCharacter = gCharacter8;
			break;
		case '9':
			pCharacter = gCharacter9;
			break;
		case '!':
			pCharacter = gCharacterExclamationMark;
			break;
		case '?':
			pCharacter = gCharacterQuestionMark;
			break;
		case '.':
			pCharacter = gCharacterFullstop;
			break;
		case ',':
			pCharacter = gCharacterComma;
			break;
		case ':':
			pCharacter = gCharacterColon;
			break;
	}

	return pCharacter;
}
