/*---------------------------------------------------------------------------------
 libBAG  Copyright (C) 2010 - 2012		
  BassAceGold - <BassAceGold@gmail.com>

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any
  damages arising from the use of this software.

  Permission is granted to anyone to use this software for any
  purpose, including commercial applications, and to alter it and
  redistribute it freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you
     must not claim that you wrote the original software. If you use
     this software in a product, an acknowledgment in the product
     documentation would be appreciated but is not required.
	 
  2. Altered source versions must be plainly marked as such, and
     must not be misrepresented as being the original software.
	 
  3. Restricting the usage of or portions of code in this library
     from other software developers and or content creators
     is strictly prohibited in applications with software creation capabilities
     and or with the intent to profit from said restrictions.
	 
  4. This notice may not be removed or altered from any source
     distribution.
---------------------------------------------------------------------------------*/

#include "text.h"
#include "BitTextFunctions.h"
#include "FTTextFunctions.h"
#include "../Draw.h"
#include "../Effects.h"
#include "filesystem.h"


char *BAG_Font_GetType(FNTObj_t *font)
{
	return (char*)&font->type;
}

char *BAG_Font_GetRotMode(FNTObj_t *font)
{
	return (char*)&font->rotMode;
}

char *BAG_Font_GetUnicode(FNTObj_t *font)
{
	return (char*)&font->utf;
}

int _BAG_Font_LoadFont(const char *filename, FNTObj_t *font)
{
	if(font->loadFont != NULL)
		return font->loadFont(filename, font);
	return 0;
}

void BAG_Font_UnloadFont(FNTObj_t *font)
{
	if(font->unloadFont != NULL)
	{
		//unload the font
		font->unloadFont(font);
		
		//clean functions
		font->unloadFont = NULL;
		font->loadFont = NULL;
		font->drawChar = NULL;
	}
	memset(font, 0, sizeof(FNTObj_t));
}


int BAG_Font_LoadFontEx(const char *filename, FNTObj_t *font, char type, int size)
{
	BAG_Font_UnloadFont(font);
	VAL(BAG_Font_GetType(font)) = type;
	VAL(BAG_Font_GetUnicode(font)) = DEFAULT_UNICODE;
	switch((*BAG_Font_GetType(font)))
	{
		case 0://bitmap font
			BAG_BitText_FontInit(font);
			BAG_BitText_SetFontDrawSize(font, size);
		break;
		case 1://freetype font
			BAG_Freetype_FontInit(font);
			BAG_Freetype_SetFontDrawSize(font, size);
		break;
	}
	
	return _BAG_Font_LoadFont(filename, font);
}


int BAG_Font_LoadFont(const char *filename, FNTObj_t *font)
{
	u8 magic_number[4];
	memset(magic_number, 0, sizeof(magic_number));
	
	if(BAG_Filesystem_GetMagicNumbers(filename, magic_number, 4) == -1){
		BAG_DBG_LibMsg(OPEN_FAIL_MSG, filename);
		return ERR_END;
	}
		
	if(!strcmp((char*)magic_number, "ttcf"))
		return BAG_Font_LoadFontEx(filename, font, TYPE_FREETYPE, DEFAULT_FREETYPE_SIZE);
	else
		return BAG_Font_LoadFontEx(filename, font, TYPE_BITMAP, DEFAULT_BITTEXT_SIZE);

	BAG_DBG_LibMsg(OPEN_FAIL_MSG, filename);
	return ERR_END;
}


int BAG_Font_LoadFontExt(const char *filename, FNTObj_t *font)
{
	char newFile[MAX_PATH];

	#define FONTFILES 4
	char exts[FONTFILES][5] = {
		".bmp\0",
		".jpg\0",
		".ttf\0",//ttf takes top priority
		".png\0",
	};
	
	char *extlist[FONTFILES];
	int i = 0;
	for(; i < FONTFILES; i++)
		extlist[i] = (char*)&exts[i];

	
	if(BAG_Filesystem_FindFileExt(filename, extlist, FONTFILES, newFile, MAX_PATH) > 0)
		return BAG_Font_LoadFont(newFile, font);
	return ERR_END;
}


static void BAG_Font_SetChar(FNTObj_t *font, struct FNTChar_s *character, int x, int y, int x2, int y2, char *code, u16 color)
{
	character->color = color;
	switch((*BAG_Font_GetType(font))){
		default:
			character->bitmap_font = (BitFnt_t*)font->bitmap_font;
		break;
		case TYPE_FREETYPE:
			character->freetype_font = (FTFnt_t*)BAG_Freetype_FTStruct(font);
		break;
	}
	
	character->utf = (*BAG_Font_GetUnicode(font));
	switch(character->utf)
	{
		default:
			character->character = code[0];
		break;
		case 16:
			character->character_uni16[0] = code[0];
			character->character_uni16[1] = code[1];
		break;
		case 32:
			character->character_uni32[0] = code[0];
			character->character_uni32[1] = code[1];
			character->character_uni32[2] = code[2];
			character->character_uni32[3] = code[3];
		break;
	}
	character->drawx = x;
	character->drawy = y;
	character->x2 = x2;
	character->y2 = y2;
}


struct FNTChar_s BAG_Font_DrawCharacter(u16 *dest, int destWd, int destHt, FNTObj_t *font, char *output, int x, int y, int x2, int y2, u16 color)
{
	struct FNTChar_s character; 
	memset(&character, 0, sizeof(struct FNTChar_s));
	BAG_Font_SetChar(font, &character, x, y, x2, y2, output, color);
	
	if(font->setRot != NULL)
		font->setRot(&character, (*BAG_Font_GetRotMode(font)));

	if(font->drawChar != NULL)
		font->drawChar(dest, destWd, destHt, &character);

	return character;
}

static int fillCode(int pos, int size, char *input, char *rtrn)
{
	int i = 0, len = strlen(input);
	for(i = 0; i < size; i++)
	{
		if(pos + i >= len)
			break;
	
		rtrn[i] = input[pos + i];
	}
	return i;
}

char BAG_Character_Advance(struct FNTChar_s *curLetter, int *newX, int *newY, int x1, int y1, int destWd, int destHt, int mode)
{
	if(curLetter->rot == TEXTROT_NORM)
	{
		if((*newX) != curLetter->drawx)
			(*newX) = curLetter->drawx;
		if((*newX) >= destWd){//hit edge of screen
			(*newX) = x1;
			curLetter->drawy = curLetter->newlinePos;
			if(mode == -2)
				return 1;
		}
		
		if((*newY) != curLetter->drawy)
		{
			(*newY) = curLetter->drawy;
			(*newX) = x1;//newline
		}
		if((*newY) > destHt)
			return 1;
	}
	else if(curLetter->rot == TEXTROT_RIGHT)
	{
		if((*newX) != curLetter->drawx)
		{
			(*newY) = y1;
			(*newX) = curLetter->drawx;
		}
		if((*newX) < 0)
			return 1;
		
		if((*newY) != curLetter->drawy)
			(*newY) = curLetter->drawy;
		if((*newY) < destHt){
			(*newY) = y1;
			if(mode == -2)
				return 1;
		}
	}
	return 0;
}

int BAG_Font_DrawStringEx(u16 *dest, int destWd, int destHt, FNTObj_t *font, char *string, int x1, int y1, int x2, int y2, int length, u16 color)
{
	struct FNTChar_s curLetter;
	memset(&curLetter, 0, sizeof(struct FNTChar_s));
	int tempy = y1, tempx = x1;
	char clip = 0;
	if(length < 0){
		if(length == -2)
			clip = -2;
		length = strlen(string);
	}
		
	int i = 0;
	char temp[4];
	do
	{
		switch((*BAG_Font_GetUnicode(font)))
		{
			case 8:
				curLetter = BAG_Font_DrawCharacter(dest, destWd, destHt, font, (char*)&string[i], tempx, tempy, x2, y2, color);	
				i+=1;
			break;
			case 16:
				fillCode(i, 2, string, temp);
				curLetter = BAG_Font_DrawCharacter(dest, destWd, destHt, font, temp, tempx, tempy, x2, y2, color);
				i+=2;
			break;
			case 32:
				fillCode(i, 4, string, temp);
				curLetter = BAG_Font_DrawCharacter(dest, destWd, destHt, font, temp, tempx, tempy, x2, y2, color);
				i+=4;			
			break;
		}		
		if(BAG_Character_Advance(&curLetter, &tempx, &tempy, x1, y1, x2, y2, clip))
			break;
	}while(i < length);
	return i;
}

int BAG_Print(u16 *dest, FNTObj_t *font, char *string, int x, int y, int x2, int y2, u16 color)
{
	return BAG_Font_DrawStringEx(dest, SCREEN_WIDTH, SCREEN_HEIGHT, font, string, x, y, x2, y2,-1, color);
}
