#include <allegro.h>
#include "menu.h"
#include "data.h"

int current_profile = 0;
int soth_player_mode = 0;
char gmenu_mvol_text[64] = {0};
char gmenu_svol_text[64] = {0};
char gmenu_dtext[256] = {0};
char gmenu_mtext[256] = {0};
char gmenu_wtext[256] = {0};
char gmenu_mvtext[256] = {0};
char gmenu_svtext[256] = {0};
char gmenu_gptext[256] = {0};
char gmenu_wstext[256] = {0};
char gmenu_istext[256] = {0};
char gmenu_iptext[256] = {0};
char * weapon_text[64] = {"Standard", "Standard (No Mines)", "Classic", "Maximum Weapons", "Master Inventory", "Water Rifles Only", "Scarce Weapons", "Wabawar", "Minewar", "Short-Range Weapons", "Custom"};
char * cweapon_text[8] = {"Light", "Medium", "Heavy"};
char * citem_text[8] = {"None", "Light", "Medium", "Heavy"};
char gmenu_cwtext[100][256] = {0};
char gmenu_citext[100][256] = {0};
char gmenu_gmtext[256] = {0};
char gmenu_gset_text[16][256] = {0};
char * gmtext[16] = {"Splash Fest", "Team Splash Fest", "Tag Match", "Team Tag Match", "Runner", "Capture the Flag", "Tag", "Freeze Tag", "Cops and Robbers", "Hot Head", "Explore", NULL};
char * dmenu_mode_description[16] =
{
	"Each player has a set number of respawns. When a player runs out of respawns they are out of the game. The last player remaining is the winner. Discard players using the various weapons available to you.",
	"The same as Splashfest except you align yourself with one of the teams. When all players from one team are eiliminated the other team wins.",
	"Make a set number of discards to win. To discard someone means to deplete their raincoat completely so that they must respawn.",
	"Same as Tag Match except you have teams.",
	"When your reach the time limit you are the winner. Sound simple? You have to be what's called the runner in order for your timer to count down. If you are discarded the player that discarded you becomes the runner. The game continues until someone reaches the time limit.",
	"You must capture the other team's flag and take it back to your base. Once you have captured enough flags you are the winner.",
	"The object is to not be it and thus not have your time ticking away. If you are it long enough you are eliminated. To tag someone simply hit them with any weapon. Last player standing wins.",
	"If you are a runner, you must keep yourself from being shot. If you are shot you are frozen until another runner touches you. If all runners are frozen the taggers win, otherwise if time reaches 0 the runners win.",
	"The cops must capture all the robbers. Shooting a robber puts them in jail. For a robber to escape from jail they must have one of the other robbers come to the jail to let them out. The robbers win when time reaches 0.",
	"The player declared hot head gets points for discarding other players. Other players can get points by discarding the hot head and thus becoming the hot head so they can earn more points. First player to reach a set number of points wins.",
	"Walk around the levels and familiarize yourself with your future battlegrounds.",
	NULL	
};
	
char gmenu_nset_text[256] = {0};
char gmenu_nick_text[256] = {0};
char gmenu_sync_text[256] = {0};
char gmenu_mode_text[256] = {0};
char gmenu_max_text[256] = {0};
char gmenu_prof_text[256] = {0};

char gmenu_control_title[256] = {0};
char gmenu_control_item[32][256] = {0};
int ptime = 0;
int cp, ck;
char server_list_ip[256] = {0};
int level_step = 0;

void vg_menu_play(void)
{
	vg_current_menu = VG_MENU_PLAY;
}

void vg_menu_play_new(void)
{
	vg_current_menu = VG_MENU_PLAY_NEW;
}

void vg_menu_play_new_normal(void)
{
	ncds_stop_music();
	fade_out(4);
	vg_progress.difficulty = 0;
	VG_CHARACTER_JUMP_VELOCITY = -5;
	VG_CHARACTER_GRAVITY = 0.2;
	vg_level = load_level("world.vgl");
	read_level_data(vg_level);
	vg_player.sprite.x = itofix(32);
	vg_player.sprite.y = itofix(184);
	vg_player.sprite.vx = itofix(0);
	vg_player.sprite.vy = itofix(0);
	vg_player.sprite.state = VG_CHARACTER_STAND_RIGHT;
	vg_player.sprite.active = 1;
	vg_player.score = 0;
	vg_player.lives = 5;
	vg_player.life = 100;
	vg_player.warp_time = 0;
	vg_player.warp_type = 0;
	vg_last_warp.dx = 32;
	vg_last_warp.dy = 184;
	vg_last_warp.ds = 0;
	vg_state = VG_STATE_GAME;
	vg_logic();
	vg_update();
	vg_draw();
	fade_in(vg_palette, 4);
	ncds_play_music("world.xm", 1);
	gametime_reset();
}

void vg_menu_play_new_hard(void)
{
	ncds_stop_music();
	fade_out(4);
	vg_progress.difficulty = 1;
	VG_CHARACTER_JUMP_VELOCITY = -6;
	VG_CHARACTER_GRAVITY = 0.29;
	vg_level = load_level("world.vgl");
	read_level_data(vg_level);
	vg_player.sprite.x = itofix(32);
	vg_player.sprite.y = itofix(184);
	vg_player.sprite.vx = itofix(0);
	vg_player.sprite.vy = itofix(0);
	vg_player.sprite.state = VG_CHARACTER_STAND_RIGHT;
	vg_player.sprite.active = 1;
	vg_player.score = 0;
	vg_player.lives = 5;
	vg_player.life = 100;
	vg_player.warp_time = 0;
	vg_player.warp_type = 0;
	vg_last_warp.dx = 32;
	vg_last_warp.dy = 184;
	vg_last_warp.ds = 0;
	vg_state = VG_STATE_GAME;
	vg_logic();
	vg_update();
	vg_draw();
	fade_in(vg_palette, 4);
	ncds_play_music("world.xm", 1);
	gametime_reset();
}

void vg_menu_play_continue(void)
{
}

void vg_menu_options(void)
{
//	vg_current_menu = VG_MENU_OPTIONS;
}

void vg_menu_quit(void)
{
	fade_out(4);
	vg_done = 1;
}

void create_menu(VG_MENU * mp, char * title, int x, int y, int parent)
{
	mp->items = 0;
	mp->x = x;
	mp->y = y;
	mp->parent = parent;
	mp->selected = 0;
	mp->title = title;
}

void add_menu_item(VG_MENU * mp, FONT * fp, int ox, int oy, char * name, void(*left_proc)(), void(*right_proc)(), void(*enter_proc)())
{
	mp->item[mp->items].type = MENU_ITEM_TYPE_NORMAL;
	mp->item[mp->items].name = name;
	strcpy(mp->item[mp->items].description, "");
	mp->item[mp->items].ox = ox;
	mp->item[mp->items].oy = oy;
	mp->item[mp->items].dp = NULL;
	mp->item[mp->items].dp2 = NULL;
	mp->item[mp->items].left_proc = left_proc;
	mp->item[mp->items].right_proc = right_proc;
	mp->item[mp->items].enter_proc = enter_proc;
	mp->item[mp->items].fp = fp;
	mp->items++;
}

void add_menu_flag(VG_MENU * mp, FONT * fp, int ox, int oy, char * name, int * dp, void(*left_proc)(), void(*right_proc)(), void(*enter_proc)())
{
	mp->item[mp->items].type = MENU_ITEM_TYPE_FLAG;
	mp->item[mp->items].name = name;
	mp->item[mp->items].ox = ox;
	mp->item[mp->items].oy = oy;
	mp->item[mp->items].dp = dp;
	mp->item[mp->items].dp2 = NULL;
	mp->item[mp->items].left_proc = left_proc;
	mp->item[mp->items].right_proc = right_proc;
	mp->item[mp->items].enter_proc = enter_proc;
	mp->item[mp->items].fp = fp;
	mp->items++;
}

void add_menu_header(VG_MENU * mp, FONT * fp, int ox, int oy, char * name)
{
	mp->item[mp->items].type = MENU_ITEM_TYPE_HEADER;
	mp->item[mp->items].ox = ox;
	mp->item[mp->items].oy = oy;
	mp->item[mp->items].dp = NULL;
	mp->item[mp->items].dp2 = NULL;
	mp->item[mp->items].name = name;
	mp->item[mp->items].left_proc = NULL;
	mp->item[mp->items].right_proc = NULL;
	mp->item[mp->items].enter_proc = NULL;
	mp->item[mp->items].fp = fp;
	mp->items++;
}

void add_menu_variable(VG_MENU * mp, FONT * fp, int ox, int oy, int * dp, char * dp2[])
{
	mp->item[mp->items].type = MENU_ITEM_TYPE_VARIABLE;
	mp->item[mp->items].name = NULL;
	mp->item[mp->items].ox = ox;
	mp->item[mp->items].oy = oy;
	mp->item[mp->items].dp = dp;
	mp->item[mp->items].dp2 = dp2;
	mp->item[mp->items].left_proc = NULL;
	mp->item[mp->items].right_proc = NULL;
	mp->item[mp->items].enter_proc = NULL;
	mp->item[mp->items].fp = fp;
	mp->items++;
}

void add_menu_string(VG_MENU * mp, FONT * fp, int ox, int oy, char * string)
{
	mp->item[mp->items].type = MENU_ITEM_TYPE_STRING;
	mp->item[mp->items].name = string;
	mp->item[mp->items].ox = ox;
	mp->item[mp->items].oy = oy;
	mp->item[mp->items].dp = NULL;
	mp->item[mp->items].dp2 = NULL;
	mp->item[mp->items].left_proc = NULL;
	mp->item[mp->items].right_proc = NULL;
	mp->item[mp->items].enter_proc = NULL;
	mp->item[mp->items].fp = fp;
	mp->items++;
}

void add_menu_image(VG_MENU * mp, FONT * fp, int ox, int oy, BITMAP * bp)
{
	mp->item[mp->items].type = MENU_ITEM_TYPE_IMAGE;
	mp->item[mp->items].bp = bp;
	mp->item[mp->items].ox = ox;
	mp->item[mp->items].oy = oy;
	mp->item[mp->items].dp = NULL;
	mp->item[mp->items].dp2 = NULL;
	mp->item[mp->items].left_proc = NULL;
	mp->item[mp->items].right_proc = NULL;
	mp->item[mp->items].enter_proc = NULL;
	mp->item[mp->items].fp = fp;
	mp->items++;
}

void center_menu(VG_MENU * mp, BITMAP * bp, int ox, int oy)
{
	int i;
	int tw;
	int lowest = mp->item[0].oy;
	int highest = mp->item[0].oy;
	int xlowest = mp->item[0].ox;
	int xhighest;

//	xhighest = xlowest + strlen(mp->item[0].name) * pp_font[PP_FONT_MENU_ITEM]->w + pp_font[PP_FONT_MENU_ITEM]->w;
	for(i = 0; i < mp->items; i++)
	{
		if(mp->item[i].oy < lowest)
		{
			lowest = mp->item[i].oy;
		}
		else if(mp->item[i].oy > highest)
		{
			highest = mp->item[i].oy + 16;
		}
	}
//	mp->x = ox;
//	mp->x = vg_screen->w / 2 - (xhighest - xlowest) / 2;
	mp->y = bp->h / 2 - (highest - lowest) / 2 - lowest + 44;
}

void menu_setup(void)
{
	int i;

	gui_bg_color = 31;
	gui_mg_color = 20;
	gui_fg_color = 8;

	/* create main menu */
	create_menu(&vg_menu[VG_MENU_MAIN], "Main Menu", 0, 0, VG_MENU_MAIN);
	add_menu_item(&vg_menu[VG_MENU_MAIN], vg_font[VG_FONT_MENU], 0, 0, "Play Game", NULL, NULL, vg_menu_play);
	add_menu_item(&vg_menu[VG_MENU_MAIN], vg_font[VG_FONT_MENU], 0, 24, "Options", NULL, NULL, vg_menu_options);
	add_menu_item(&vg_menu[VG_MENU_MAIN], vg_font[VG_FONT_MENU], 0, 48, "Quit", NULL, NULL, vg_menu_quit);
	
	create_menu(&vg_menu[VG_MENU_PLAY], "Play", 0, 0, VG_MENU_MAIN);
	add_menu_item(&vg_menu[VG_MENU_PLAY], vg_font[VG_FONT_MENU], 0, 0, "Continue", NULL, NULL, vg_menu_play_continue);
	add_menu_item(&vg_menu[VG_MENU_PLAY], vg_font[VG_FONT_MENU], 0, 24, "Start New Game", NULL, NULL, vg_menu_play_new);
//	add_menu_item(&vg_menu[VG_MENU_PLAY], vg_font[VG_FONT_MENU], 0, 48, "Cancel", NULL, NULL, vg_menu_quit);
	
	create_menu(&vg_menu[VG_MENU_PLAY_NEW], "Play New", 0, 0, VG_MENU_PLAY);
	add_menu_item(&vg_menu[VG_MENU_PLAY_NEW], vg_font[VG_FONT_MENU], 0, 0, "Normal", NULL, NULL, vg_menu_play_new_normal);
	add_menu_item(&vg_menu[VG_MENU_PLAY_NEW], vg_font[VG_FONT_MENU], 0, 24, "Hard", NULL, NULL, vg_menu_play_new_hard);
//	add_menu_item(&vg_menu[VG_MENU_PLAY], vg_font[VG_FONT_MENU], 0, 48, "Cancel", NULL, NULL, vg_menu_quit);
	
	for(i = 0; i < VG_MAX_MENUS; i++)
	{
		center_menu(&vg_menu[i], vg_screen, 16, 0);
	}
}

void menu_fix_item(VG_MENU * mp)
{
	int start = mp->selected;

	while(mp->item[mp->selected].type == MENU_ITEM_TYPE_HEADER || mp->item[mp->selected].type == MENU_ITEM_TYPE_VARIABLE || mp->item[mp->selected].type == MENU_ITEM_TYPE_STRING || mp->item[mp->selected].type == MENU_ITEM_TYPE_IMAGE)
	{
		mp->selected++;
		if(mp->selected >= mp->items)
		{
			mp->selected = 0;
		}
		if(mp->selected == start)
		{
			break;
		}
	}
}

void menu_next_item(VG_MENU * mp)
{
	int start = mp->selected;

	mp->selected++;
	if(mp->selected >= mp->items)
	{
		mp->selected = 0;
	}
	while(mp->item[mp->selected].type == MENU_ITEM_TYPE_HEADER || mp->item[mp->selected].type == MENU_ITEM_TYPE_VARIABLE || mp->item[mp->selected].type == MENU_ITEM_TYPE_STRING || mp->item[mp->selected].type == MENU_ITEM_TYPE_IMAGE)
	{
		mp->selected++;
		if(mp->selected >= mp->items)
		{
			mp->selected = 0;
		}
		if(mp->selected == start)
		{
			break;
		}
	}
}

void menu_prev_item(VG_MENU * mp)
{
	int start = mp->selected;

	mp->selected--;
	if(mp->selected < 0)
	{
		mp->selected = mp->items - 1;
	}
	while(mp->item[mp->selected].type == MENU_ITEM_TYPE_HEADER || mp->item[mp->selected].type == MENU_ITEM_TYPE_VARIABLE || mp->item[mp->selected].type == MENU_ITEM_TYPE_STRING || mp->item[mp->selected].type == MENU_ITEM_TYPE_IMAGE)
	{
		mp->selected--;
		if(mp->selected < 0)
		{
			mp->selected = mp->items - 1;
		}
		if(mp->selected == start)
		{
			break;
		}
	}
}

void menu_logic(void)
{
	int i;

	if(key[KEY_UP] || vg_controller.button[CONTROLLER_UP].pressed)
	{
		menu_prev_item(&vg_menu[vg_current_menu]);
		key[KEY_UP] = 0;
	}
	if(key[KEY_DOWN] || vg_controller.button[CONTROLLER_DOWN].pressed)
	{
		menu_next_item(&vg_menu[vg_current_menu]);
		key[KEY_DOWN] = 0;
	}
	if(key[KEY_ESC] || vg_controller.button[CONTROLLER_JUMP].pressed)
	{
		if(vg_menu[vg_current_menu].parent == vg_current_menu)
		{
			vg_menu[vg_current_menu].selected = vg_menu[vg_current_menu].items - 1;
		}
		else
		{
			vg_current_menu = vg_menu[vg_current_menu].parent;
		}
		key[KEY_ESC] = 0;
	}
	switch(vg_menu[vg_current_menu].item[vg_menu[vg_current_menu].selected].type)
	{
		case MENU_ITEM_TYPE_FLAG:
		{
			if(key[KEY_ENTER] && vg_menu[vg_current_menu].item[vg_menu[vg_current_menu].selected].dp)
			{
				if(*vg_menu[vg_current_menu].item[vg_menu[vg_current_menu].selected].dp)
				{
					*vg_menu[vg_current_menu].item[vg_menu[vg_current_menu].selected].dp = 0;
				}
				else
				{
					*vg_menu[vg_current_menu].item[vg_menu[vg_current_menu].selected].dp = 1;
				}
				key[KEY_ENTER] = 0;
			}
			break;
		}
	}
	if(key[KEY_LEFT] || vg_controller.button[CONTROLLER_LEFT].pressed)
	{
		if(vg_menu[vg_current_menu].item[vg_menu[vg_current_menu].selected].left_proc)
		{
			vg_menu[vg_current_menu].item[vg_menu[vg_current_menu].selected].left_proc();
		}
		key[KEY_LEFT] = 0;
	}
	if(key[KEY_RIGHT] || vg_controller.button[CONTROLLER_RIGHT].pressed)
	{
		if(vg_menu[vg_current_menu].item[vg_menu[vg_current_menu].selected].right_proc)
		{
			vg_menu[vg_current_menu].item[vg_menu[vg_current_menu].selected].right_proc();
		}
		key[KEY_RIGHT] = 0;
	}
	if(key[KEY_ENTER] || vg_controller.button[CONTROLLER_FIRE].pressed && vg_menu[vg_current_menu].selected < vg_menu[vg_current_menu].items)
	{
		if(vg_menu[vg_current_menu].item[vg_menu[vg_current_menu].selected].enter_proc)
		{
			vg_menu[vg_current_menu].item[vg_menu[vg_current_menu].selected].enter_proc();
//			ncds_play_sample(sound[SOUND_MENU_PICK], 127, -1, 0);
		}
		key[KEY_ENTER] = 0;
	}
}

void draw_menu(BITMAP * bp, VG_MENU * mp)
{
	int i, y, j;
	char temp_string[128];

	for(i = 0; i < mp->items; i++)
	{
			switch(mp->item[i].type)
			{
				case MENU_ITEM_TYPE_NORMAL:
				{
					if(i == mp->selected)
					{
						textprintf_centre_ex(bp, mp->item[i].fp, vg_screen->w / 2, mp->y + mp->item[i].oy, 1, -1, "%s", mp->item[i].name);
						textprintf_centre_ex(bp, mp->item[i].fp, vg_screen->w / 2 - 4, mp->y + mp->item[i].oy - 4, 31, -1, "%s", mp->item[i].name);
					}
					else
					{
						textprintf_centre_ex(bp, mp->item[i].fp, vg_screen->w / 2, mp->y + mp->item[i].oy, 1, -1, "%s", mp->item[i].name);
						textprintf_centre_ex(bp, mp->item[i].fp, vg_screen->w/ 2 - 2, mp->y + mp->item[i].oy - 2, 16, -1, "%s", mp->item[i].name);
					}
					break;
				}
				case MENU_ITEM_TYPE_FLAG:
				{
					if(mp->item[i].dp)
					{
						if(mp->item[i].dp)
						{
							if(i == mp->selected)
							{
								if(*mp->item[i].dp)
								{
									textprintf_centre_ex(bp, mp->item[i].fp, vg_screen->w / 2, mp->y + mp->item[i].oy, 236, -1, "%s", mp->item[i].name);
								}
								else
								{
									textprintf_centre_ex(bp, mp->item[i].fp, vg_screen->w / 2, mp->y + mp->item[i].oy, 234, -1, "%s", mp->item[i].name);
								}
							}
							else
							{
								if(*mp->item[i].dp)
								{
									textprintf_centre_ex(bp, mp->item[i].fp, vg_screen->w / 2, mp->y + mp->item[i].oy, 238, -1, "%s", mp->item[i].name);
								}
								else
								{
									textprintf_centre_ex(bp, mp->item[i].fp, vg_screen->w / 2, mp->y + mp->item[i].oy, 236, -1, "%s", mp->item[i].name);
								}
							}
							if(*mp->item[i].dp)
							{
								for(j = 0; j < strlen(mp->item[i].name) + 3; j++)
								{
									temp_string[j] = ' ';
								}
								temp_string[strlen(mp->item[i].name) + 3] = 0;
								temp_string[1] = 'X';
							}
						}
					}
					break;
				}
				case MENU_ITEM_TYPE_HEADER:
				{
					textprintf_centre_ex(bp, mp->item[i].fp, vg_screen->w / 2, mp->y + mp->item[i].oy, 239, -1, "%s", mp->item[i].name);
					break;
				}
				case MENU_ITEM_TYPE_VARIABLE:
				{
					if(mp->item[i].dp && mp->item[i].dp2)
					{
						textprintf_centre_ex(bp, mp->item[i].fp, vg_screen->w / 2, mp->y + mp->item[i].oy, 231, -1, "%s", mp->item[i].dp2[*mp->item[i].dp]);
					}
					else if(mp->item[i].dp)
					{
						textprintf_centre_ex(bp, mp->item[i].fp, vg_screen->w / 2, mp->y + mp->item[i].oy, 231, -1, "%d", *mp->item[i].dp);
					}
					break;
				}
				case MENU_ITEM_TYPE_STRING:
				{
					textprintf_centre_ex(bp, mp->item[i].fp, vg_screen->w / 2, mp->y + mp->item[i].oy, 1, -1, "%s", mp->item[i].name);
					textprintf_centre_ex(bp, mp->item[i].fp, vg_screen->w / 2 - 2, mp->y + mp->item[i].oy - 2, 31, -1, "%s", mp->item[i].name);
					break;
				}
				case MENU_ITEM_TYPE_IMAGE:
				{
					draw_sprite(bp, mp->item[i].bp, mp->x + mp->item[i].ox, mp->y + mp->item[i].oy);
					break;
				}
			}
	}
}

void menu_render(void)
{
	int i;

//	textout_centre_ex(vg_screen, font, vg_menu[vg_current_menu].title, 160, vg_menu[vg_current_menu].y - 32, 1, -1);
//	textout_centre_ex(vg_screen, font, vg_menu[vg_current_menu].title, 160 - 2, vg_menu[vg_current_menu].y - 32 - 2, 239, -1);
//	text_wrapper_bottom(back_buffer, soth_font[1], vg_menu[vg_current_menu].item[vg_menu[vg_current_menu].selected].description, 16, 16, 16, 239);
	draw_menu(vg_screen, &vg_menu[vg_current_menu]);
}
