#include <gccore.h>
#include <ogcsys.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ogc/ios.h>

#include "gfx/hand.h"
#include "gfx/bg.h"
#include "drawmenu.h"
#include "../screen.h"
#include "../pad.h"
void wait(char *msg)
{
	u16 colortitle = screen_color_from_rgb(0x00, 0xFF, 0xFF);
	screen_clear();
	screen_printf(20, 22, colortitle, msg);
	screen_render();
	while(!(pad_menu_buttons() & GB_CHANGE_SIZE));
}
/*** Draw menu primitives ***/
void menu_draw_cursor(int x, int y, float roll)
{
	//u16 colorline = screen_color_from_rgb(0x00, 0xFF, 0x00);
	//screen_draw_line(x, y, x+10, y+10, colorline);
	//screen_draw_line(x-2, y+12, x+10, y+10, colorline);
	//screen_draw_line(x, y, x-2, y+12, colorline);
	screen_draw_image(x-5, y, hand_width, hand_height, hand_data, roll, 1.0);
}
/*** MENU procedures ***/
void menu_draw_item(const char *title, int isSelected, int menu_width, int x, int y, int item_height)
{
	char buffer[(menu_width-4)/9];
	
	if (strcmp(title, "")==0)
		isSelected = 0;

	u16 colortext = (isSelected)? screen_color_from_rgb(0xFF, 0x66, 0x00) : screen_color_from_rgb(0x00, 0x00, 0x00);
	u16 colorbg = (isSelected)? screen_color_from_rgb(0x33, 0x33, 0x33) : screen_color_from_rgb(0xFF, 0xFF, 0xFF);
	
	memcpy(buffer, title, ((menu_width-4)/9));
	if (isSelected)
		screen_draw_rectangle(x+1, y, menu_width-2, item_height, colorbg, 1);
	screen_printf(x+2, y+((item_height-16)/2), colortext, buffer);
}
int menu_draw(const char *title, char *items[], int n_max)
{
	//u16 colorline = screen_color_from_rgb(0xFF, 0x00, 0x00);
//	u16 colorbg = screen_color_from_rgb(0xFF, 0xFF, 0xFF);
//	u16 colorbgtitle = screen_color_from_rgb(0x00, 0xFF, 0xFF);
	u16 colortitle = screen_color_from_rgb(0xFF, 0x12, 0x24);
	
	int menu_width = 520, menu_height = 380, item_height=25;
	int exit=0, x, y, offsetx = (screen_w-menu_width)/2, offsety = (screen_h-menu_height)/2, selected, limit=0, items_max=((menu_height-20)/item_height);
	int max_limit = n_max-items_max, i;
	float roll;
	do
	{
		selected=-1;
		pad_position(&x, &y, &roll);
		/*Search selected*/
		if (x>=offsetx+2 && x<=offsetx+menu_width-4)
		{
			for ( i=0; i<items_max && i<n_max; i++)
				if (y>=offsety+22+(i*item_height) && y<=offsety+22+((i+1)*item_height))
				{
					selected=i;
					break;
				}
		}
		/*Draw panel*/
		screen_clear();
		screen_draw_image((screen_w-bg_width)/2, (screen_h-bg_height)/2, bg_width, bg_height, bg_data, 0.0, 1.0);//bg
		//screen_draw_rectangle(offsetx, offsety, menu_width, menu_height, colorline, 0);
		//screen_draw_line(offsetx, offsety+20, menu_width, offsety+20, colorline);
		
//		screen_draw_rectangle(offsetx+1, offsety+1, menu_width-2, 18, colorbgtitle, 1);
//		screen_draw_rectangle(offsetx+1, offsety+21, menu_width-2, menu_height-22, colorbg, 1);
		
		screen_printf(offsetx+4+(menu_width-strlen(title))/2, offsety+2, colortitle, title);
		/*Draw items*/
		for ( i=0; i<items_max && i<n_max; i++)
		{
			if(limit>0 && i==0)
				menu_draw_item(" -- move up --", (selected==i), menu_width, offsetx, offsety+22+(i*item_height), item_height);
			else if(limit<max_limit && i==items_max-1)
				menu_draw_item(" -- move down --", (selected==i), menu_width, offsetx, offsety+22+(i*item_height), item_height);
			else
				menu_draw_item(items[limit+i], (selected==i), menu_width, offsetx, offsety+22+(i*item_height), item_height);
		}
		//screen_printf(offsetx, offsety+menu_height, colorline, "press HOME for exit");
		/*Draw cursor*/
		menu_draw_cursor(x, y, roll);
		//screen_printf(offsetx, 30, colortitle, "pos x: %3d  y: %3d", x, y);
		screen_render();

		if (pad_menu_buttons() & GB_PAD_START && selected>=0)
		{
			if(limit>0 && selected==0)
					limit--;
			else if(limit<max_limit && selected==items_max-1)
					limit++;
			else
			{
				selected = limit+selected;
				exit = 1;
			}
		}
		if (pad_menu_buttons() & GB_MENU)
			return -1;
	}while(!exit);
	
	return selected;
}

