#include "osk.h"
#include <string.h>

int osk_x = 0;
int osk_y = 0;

int initialized = 0; /*initialization flag*/
int sel_x 	= 1; /*where the cursor points*/
int sel_y   = 1;
int drawSelX;
int drawSelY;
int keyset 		= 0;
Image * img_keyset[4];
Image * img_keyset_s[4];

char modeChar[4][3][3][5] =
{
	{	//standard letters
		{ ",abc",  ".def","!ghi" },
		{ "-jkl","\010m n", "?opq" },
		{ "(rst",  ":uvw",")xyz" }
	},

	{	//capital letters
		{ "^ABC",  "@DEF","*GHI" },
		{ "_JKL","\010M N", "\"OPQ" },
		{ "=RST",  ";UVW","/XYZ" }
	},

	{	//numbers
		{ "\0\0\0001","\0\0\0002","\0\0\0003" },
		{ "\0\0\0004",  "\010\0 5","\0\0\0006" },
		{ "\0\0\0007","\0\0\0008", "\0\00009" }
	},

	{	//special characters
		{ "'(.)",  "\"<'>","-[_]" },
		{ "!{?}","\010\0 \0", "+\\=/" },
		{ ":@;#",  "~$`%","*^|&" }
	}
};


void osk_load(void){

	
	/*load images*/
	img_keyset[0]	= loadImageFromMemory(image_osk_keys, size_image_osk_keys);
	img_keyset[1] 	= loadImageFromMemory(image_osk_keys_c, size_image_osk_keys_c);
	img_keyset_s[0] = loadImageFromMemory(image_osk_keys_s, size_image_osk_keys_s);
	img_keyset_s[1] = loadImageFromMemory(image_osk_keys_s_c, size_image_osk_keys_s_c);

	img_keyset[2] 	= loadImageFromMemory(image_osk_nums, size_image_osk_nums);
	img_keyset[3]	= loadImageFromMemory(image_osk_nums_c, size_image_osk_nums_c);
	img_keyset_s[2]	= loadImageFromMemory(image_osk_nums_s, size_image_osk_nums_s);	
	img_keyset_s[3]	= loadImageFromMemory(image_osk_nums_s_c, size_image_osk_nums_s_c);	

	initialized 	= 1;
	drawSelX = osk_x + (150/2 - 64/2);
	drawSelY = osk_y + (150/2 - 64/2);

	
}


void osk_render(void){
	
	blitImageToScreen(0,0,150,150,img_keyset[keyset],osk_x,osk_y);
	blitAlphaImageToScreen(sel_x * 64,sel_y * 64,64,64,img_keyset_s[keyset],drawSelX,drawSelY);
	
}

int osk_readInput(SceCtrlData pspctrl){
	int tmpLx;
	int tmpLy;
	int keyed = -1;
	char strtmp[5];
	char chartmp;
	static unsigned int lastbutton = 0;

	tmpLx = pspctrl.Lx;
	tmpLy = pspctrl.Ly;
	
	if(tmpLx < 75)
		sel_x = 0;
	else if(tmpLx > 180)
		sel_x = 2;
	else
		sel_x = 1;

	if(tmpLy < 75)
		sel_y = 0;
	else if(tmpLy > 180)
		sel_y = 2;
	else
		sel_y = 1;


	switch(sel_x){
		case 0:
			drawSelX = osk_x;
			break;
		case 1:
			drawSelX = osk_x + (150/2 - 64/2);
			break;
		case 2:
			drawSelX = osk_x + (150 - 64);
			break;
		}

	switch(sel_y){
		case 0:
			drawSelY = osk_y;
			break;
		case 1:
			drawSelY = osk_y + (150/2 - 64/2);
			break;
		case 2:
			drawSelY = osk_y + (150 - 64);
			break;
		}	

	if(lastbutton != pspctrl.Buttons){
		if(pspctrl.Buttons & PSP_CTRL_TRIANGLE)
			keyed = 0;
		else if(pspctrl.Buttons & PSP_CTRL_SQUARE)
			keyed = 1;
		else if(pspctrl.Buttons & PSP_CTRL_CROSS)
			keyed = 2;
		else if(pspctrl.Buttons & PSP_CTRL_CIRCLE)
			keyed = 3;
		else if(pspctrl.Buttons & PSP_CTRL_RTRIGGER){
			keyset++;
			if(keyset == 4)
				keyset = 0;
			}
		else if(pspctrl.Buttons & PSP_CTRL_LTRIGGER){
			keyset--;
			if(keyset == -1)
				keyset = 3;
			}
		}

	lastbutton = pspctrl.Buttons;


	

	if(keyed != -1)
		return(modeChar[keyset][sel_y][sel_x][keyed]);
	else
		return(-1);
}

void osk_setXY(int x, int y){
	osk_x = x;
	osk_y = y;
}


/*EOF*/
