//Copyright (C) 2007 SiegeLord
//See ReadMe.txt and License.txt for copying information

#include "../include/engine/text.h"
#include "../include/engine/config.h"

int GetTextLength(const char* text, FONT* fnt)
{
	FONT* old_font = font;
	font = fnt;
	int ret = gui_strlen(text);
	font = old_font;
	return ret;
}

void DrawText(const char* text, float x, float y, zColor color, FONT* fnt)
{
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_BLEND);

	//maybe I should make this be switcheable

	//the following fixes the allegrogl's retarded way of drawing text
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();

	glLoadIdentity();
	glOrtho(0, SCREEN_W, 0, SCREEN_H, 1, 10000);

	int height = text_height(fnt);

	allegro_gl_printf(fnt, (int)x, SCREEN_H + height - (int)y, -10, color,
					"%s", text);

	glPopMatrix();


	//allegro_gl_printf(fnt, (int)x, (int)y, -10, color,
	//				"%s", text);

	glDisable(GL_BLEND);
	glBlendFunc(GL_ONE, GL_ZERO);

	glColor4ub(255, 255, 255, 255);
}

void DrawTextAlign(const char* text, float x, float y, int v_align, int h_align, zColor color, FONT* fnt)
{
	float width = GetTextLength(text, fnt);
	float height = text_height(fnt);
	float X,Y;

	switch(v_align)//down, mid, up
	{
	case -1:
		Y = y + height;
		break;
	case 0:
		Y = y + height / 2;
		break;
	default:
		Y = y;
		break;
	}

	switch(h_align)//left, mid, right
	{
	default:
		X = x;
		break;
	case 0:
		X = x - width / 2;
		break;
	case 1:
		X = x - width;
		break;
	}

	DrawText(text, X, Y, color, fnt);
}

//afaik this draws a line of text limited by the max_width
//returns the position in the string(pass this as start)
int DrawTextLim(string* str, float x, float y, int start, float max_width, zColor color, FONT* fnt, int* out_width,
				   int* out_color, bool clip_words, bool draw)
{
	int width = 0;
	int a,b,old_a;
	a = start;
	old_a = start;

	string token;
	int col;

	if(out_color)
		col = *out_color;
	else
		col = color;

	bool first = true;//used to kill leading spaces
	bool ignore_color_determinator = false;//we use it to treat double ##
	while(true)
	{
		b = str->find_first_of(" \t\n#",a);//find a separator starting from a
		if(b == (int)str->npos)//we found nothing
		{
			if(a == start)//we just started
				token = str->substr(a);//get the rest of the word
			else if(a == -1)//we drew the last word on the previous iteration
				return -1;//we are done
			else
				token = str->substr(a - 1);//we cut out the token
			old_a = a;
			a = -1;
		}
		else
		{
			if(a == 0)
				token = str->substr(a, b - a);//get the word
			else
				token = str->substr(a - 1, b - a + 1);//get the word before the thing we found
			old_a = a;
			a = b + 1;
		}

		switch(token[0])//clean up the token
		{
		case '\t':
		case ' ':
			if(first)
			{
				token.erase(0,1);//kill it
				old_a++;//?
			}
			break;
		case '\n'://we are done, the next line is to be drawn
			if(first)
			{
				token.erase(0,1);	//kill it
				old_a++;//?
			}
			else
				return old_a;//so that we skip over it next time
			break;
		case '#':
			if(!ignore_color_determinator)
			{
				token.erase(0,1);	//kill it
				if(token.size() > 0)//if it is 0 then it means that we have a double ##
				{
					string col_str = token.substr(0, 8);//cut out the color part
					col = GetColor(col_str.c_str());
					token.erase(0, 8);

					if(out_color)
						*out_color = col;
				}
				else
					ignore_color_determinator = true;
			}
			else
				ignore_color_determinator = false;
		}

		int len = GetTextLength(token.c_str(), fnt);
		if(width + len < max_width)
		{
			if(draw)
				DrawText(token.c_str(), width + x, y, col, fnt);
			width += len;
			if(out_width)
				*out_width = width;
		}
		else if(clip_words || first)	//we draw what fits, and return the end of the word
		{								//note that if this is the first word, we clip it regardless
			len = 0;
			int char_len;
			char buf[2];
			buf[1] = 0;
			FOR1((int)token.size())
			{
				buf[0] = token[ii];
				char_len = gui_strlen(buf);

				if(char_len + len + width < max_width)
				{
					len += char_len;
				}
				else
				{
					if(ii == 0 && first)//well... that means the width is way too small
						return -1;
					else if(ii == 0)//we can't draw it at all, return its start
						return old_a;
					token = token.substr(0, ii);
					//draw it
					if(draw)
						DrawText(token.c_str(), width + x, y, col, fnt);
					return old_a + ii;
				}
			}
		}
		else//we return the last num
		{
			return old_a;
		}
		first = false;
	}
}
