#include <allegro.h>
#include "CDisplay.hpp"
#include "CMain.hpp"
#include "Interaction.hpp"

// TODO: Cleanup remove replace numbers with defined constants
// TODO: Assumes a monospaced font! Could get messy if I ever use something else
// TODO: I need to erase the area if row >= 5 -- it needs to erase the msg buffer
// TODO: need to add the red arrow pointer for more info!
void Talk(std::string from, std::string msg)
{
	int ystart = screen->h-text_box->h;
	masked_blit(text_box, screen, 0, 0, 0, ystart, text_box->w, text_box->h);

	textprintf_ex(screen, font, 30, ystart+5, makecol(25,25,25), -1, from.c_str());

	//textprintf_ex(screen, font, 30+1, ystart+15+1, 0, -1, "1234567891          1234567891          1234567891          1234567891");

	if(msg.length() < 72)
         textprintf_ex(screen, font, 30, ystart+15, makecol(0,0,0), -1, msg.c_str());

	else if(msg.length() <= 360) // allows 5 lines of text
	{
		int font_width = text_length(font, " ");
		int row = 0;
		int col = 0;

		for(int i = 0; i<msg.length(); ++i)
		{
			col++;

			textprintf_ex(screen, font, 30+col*font_width, ystart+15+row*10, makecol(0,0,0), -1,"%c", msg[i]);
			if( (i+1) % 72 == 0 )
			{
				row++;
				col = 0;
			}


		}
	}


	else if( msg.length() > 360)
	{
		int font_width = text_length(font, " ");
		int row = 0;
		int col = 0;

		for(int i = 0; i<360; ++i)
		{
			col++;

			textprintf_ex(screen, font, 30+col*font_width, ystart+15+row*10, makecol(0,0,0), -1,"%c", msg[i]);
			if( (i+1) % 72 == 0 )
			{
				row++;
				col = 0;
			}

			//if(

		}

		if(row >= 5)
			{
		bool done = false;
		clear_keybuf();
		key_space = 1;
	     do{
			update_key(KEY_SPACE, &key_space);
		    if(key_space == 1) done = true;
			}while(!done);
			row = 0; col = 0;
//			allegro_message("HI");
		for(int i = 360; i<msg.length(); ++i)
		{
			col++;

			textprintf_ex(screen, font, 30+col*font_width, ystart+15+row*10, makecol(255,255,255), 0,"%c", msg[i]);
			if( (i+1) % 72 == 0 )
			{
				row++;
				col = 0;
			}

			//if(

		}

		}

		//blit game_objects

	}

//	clear_keybuf();
	bool done = false;
	key_space = 1;
//	remove_keyboard();
//	install_keyboard();
	do{

	 update_key(KEY_SPACE, &key_space);
	 if(key_space == 1) done = true;
	}while(!done);


    /*Reset the keyboard */
	// TODO: *Cough* REALLY shouldn't do this! --- The reason I do is because that's seemingly the only way to completely
	// remove all input from the key and stop possible collision skips near walls!
	remove_keyboard();
	install_keyboard();
	/*
	textprintf_ex(screen, font, 30, ystart+25, makecol(255,255,255), -1, "TETS TTEST ESTSETESTSETSETE TESTSETSET");
	textprintf_ex(screen, font, 30, ystart+35, makecol(255,255,255), -1, "TETS TTEST ESTSETESTSETSETE TESTSETSET");
	textprintf_ex(screen, font, 30, ystart+45, makecol(255,255,255), -1, "TETS TTEST ESTSETESTSETSETE TESTSETSET");
	textprintf_ex(screen, font, 30, ystart+55, makecol(255,255,255), -1, "TETS TTEST ESTSETESTSETSETE TESTSETSET");
	*/

}

void Talkf(std::string from, std::string msg, ...)
{
    char buf[255];
    va_list va_l;
	va_start(va_l, msg.c_str());
	 vsnprintf( buf, sizeof(buf), msg.c_str(), va_l);
	va_end(va_l);
	
    msg = buf;

	int ystart = screen->h-text_box->h;
	masked_blit(text_box, screen, 0, 0, 0, ystart, text_box->w, text_box->h);

	textprintf_ex(screen, font, 30, ystart+5, makecol(255,255,255), -1, from.c_str());

	//textprintf_ex(screen, font, 30+1, ystart+15+1, 0, -1, "1234567891          1234567891          1234567891          1234567891");

	if(msg.length() < 72)
         textprintf_ex(screen, font, 30, ystart+15, makecol(255,255,255), -1, msg.c_str());

	else if(msg.length() <= 360) // allows 5 lines of text
	{
		int font_width = text_length(font, " ");
		int row = 0;
		int col = 0;

		for(int i = 0; i<msg.length(); ++i)
		{
			col++;

			textprintf_ex(screen, font, 30+col*font_width, ystart+15+row*10, makecol(255,255,255), -1,"%c", msg[i]);
			if( (i+1) % 72 == 0 )
			{
				row++;
				col = 0;
			}


		}
	}


	else if( msg.length() > 360)
	{
		int font_width = text_length(font, " ");
		int row = 0;
		int col = 0;

		for(int i = 0; i<360; ++i)
		{
			col++;

			textprintf_ex(screen, font, 30+col*font_width, ystart+15+row*10, makecol(255,255,255), -1,"%c", msg[i]);
			if( (i+1) % 72 == 0 )
			{
				row++;
				col = 0;
			}

			//if(

		}

		if(row >= 5)
			{
		bool done = false;
		clear_keybuf();
	     do{
			update_key(KEY_SPACE, &key_space);
		    if(key_space == 1) done = true;
			}while(!done);
			row = 0; col = 0;
//			allegro_message("HI");
		for(int i = 360; i<msg.length(); ++i)
		{
			col++;

			textprintf_ex(screen, font, 30+col*font_width, ystart+15+row*10, makecol(255,255,255), 0,"%c", msg[i]);
			if( (i+1) % 72 == 0 )
			{
				row++;
				col = 0;
			}

			//if(

		}

		}

		//blit game_objects

	}

//	clear_keybuf();
	bool done = false;
	key_space = 1;
//	remove_keyboard();
//	install_keyboard();
	do{

	 update_key(KEY_SPACE, &key_space);
	 if(key_space == 1) done = true;
	}while(!done);


    /*Reset the keyboard */
	// TODO: *Cough* REALLY shouldn't do this! --- The reason I do is because that's seemingly the only way to completely
	// remove all input from the key and stop possible collision skips near walls!
	remove_keyboard();
	install_keyboard();
	/*
	textprintf_ex(screen, font, 30, ystart+25, makecol(255,255,255), -1, "TETS TTEST ESTSETESTSETSETE TESTSETSET");
	textprintf_ex(screen, font, 30, ystart+35, makecol(255,255,255), -1, "TETS TTEST ESTSETESTSETSETE TESTSETSET");
	textprintf_ex(screen, font, 30, ystart+45, makecol(255,255,255), -1, "TETS TTEST ESTSETESTSETSETE TESTSETSET");
	textprintf_ex(screen, font, 30, ystart+55, makecol(255,255,255), -1, "TETS TTEST ESTSETESTSETSETE TESTSETSET");
	*/

}


BITMAP *Talk(std::string from, std::string msg, bool yesno)
{
	int ystart = screen->h-text_box->h;
	masked_blit(text_box, screen, 0, 0, 0, ystart, text_box->w, text_box->h);

	textprintf_ex(screen, font, 30, ystart+5, makecol(0,0,0), -1, from.c_str());

	//textprintf_ex(screen, font, 30+1, ystart+15+1, 0, -1, "1234567891          1234567891          1234567891          1234567891");

	if(msg.length() < 72)
		textprintf_ex(screen, font, 30, ystart+15, makecol(0,0,0), -1, msg.c_str());

	else if(msg.length() <= 360) // allows 5 lines of text
	{
		int font_width = text_length(font, " ");
		int row = 0;
		int col = 0;

		for(int i = 0; i<msg.length(); ++i)
		{
			col++;

			textprintf_ex(screen, font, 30+col*font_width, ystart+15+row*10, makecol(0,0,0), -1,"%c", msg[i]);
			if( (i+1) % 72 == 0 )
			{
				row++;
				col = 0;
			}


		}
	}


	else if( msg.length() > 360)
	{
		int font_width = text_length(font, " ");
		int row = 0;
		int col = 0;

		for(int i = 0; i<360; ++i)
		{
			col++;

			textprintf_ex(screen, font, 30+col*font_width, ystart+15+row*10, makecol(0,0,0), -1,"%c", msg[i]);
			if( (i+1) % 72 == 0 )
			{
				row++;
				col = 0;
			}

			//if(

		}

		if(row >= 5)
			{
		bool done = false;
		clear_keybuf();
	     do{
			update_key(KEY_SPACE, &key_space);
		    if(key_space == 1) done = true;
			}while(!done);
			row = 0; col = 0;
	//		allegro_message("HI");
		for(int i = 360; i<msg.length(); ++i)
		{
			col++;

			textprintf_ex(screen, font, 30+col*font_width, ystart+15+row*10, makecol(255,255,255), 0,"%c", msg[i]);
			if( (i+1) % 72 == 0 )
			{
				row++;
				col = 0;
			}

			//if(

		}

		}

		//blit game_objects

	}

//BITMAP *savescreen = screen;
//	clear_keybuf();
	bool done = false;
	key_space = 1;
//	remove_keyboard();
//	install_keyboard();
	do{

	 update_key(KEY_SPACE, &key_space);
	 if(key_space == 1) done = true;
	}while(!done);


    /*Reset the keyboard */
	// TODO: *Cough* REALLY shouldn't do this! --- The reason I do is because that's seemingly the only way to completely
	// remove all input from the key and stop possible collision skips near walls!
	clear_keybuf();
//	remove_keyboard();
//	install_keyboard();
	/*
	textprintf_ex(screen, font, 30, ystart+25, makecol(255,255,255), -1, "TETS TTEST ESTSETESTSETSETE TESTSETSET");
	textprintf_ex(screen, font, 30, ystart+35, makecol(255,255,255), -1, "TETS TTEST ESTSETESTSETSETE TESTSETSET");
	textprintf_ex(screen, font, 30, ystart+45, makecol(255,255,255), -1, "TETS TTEST ESTSETESTSETSETE TESTSETSET");
	textprintf_ex(screen, font, 30, ystart+55, makecol(255,255,255), -1, "TETS TTEST ESTSETESTSETSETE TESTSETSET");
	*/
   return screen;
}


void update_key(int key_code, int *key_var)
{
    if(key[key_code])
        (*key_var)++;
    else
        (*key_var) = 0;
}

