#include	<stdio.h>
#include	<string.h>
#include	<allegro.h>

//#include	"zone.h"
//#include	"structs.h"
//#include	"common.h"
//#include	"text.h"
#include	"obj01.h"
#include	"menus.h"



void InitLevelSelect()
{
	menu_column_size = 7;//13;
	menu_column_count = 2;
	menu_timer1 = 0;
	
	selection_menu[0].offset_x = 0x10;
	selection_menu[0].offset_y = 0x48;
	selection_menu[0].show = 1;
	selection_menu[0].rows = 1;
	strcpy(selection_menu[0].string, "ZONE\0");
	
	selection_menu[0].row[0].var_size = 0;	// char
	// this will be done by scripting in the future
	selection_menu[0].row[0].var_ptr8 = &zone;
	selection_menu[0].row[0].type = 1;
	selection_menu[0].row[0].min_value = 0x00;
	selection_menu[0].row[0].max_value = 0xFF;
	selection_menu[0].row[0].offset_x = 0x70;
	selection_menu[0].row[0].offset_y = 0x48;
	selection_menu[0].row[0].value_digits = 2;
	selection_menu[0].row[0].columns = 0;
	////////////////////////////////////////////////////////////
		
	selection_menu[1].offset_x = 0x10;
	selection_menu[1].offset_y = 0x58;
	selection_menu[1].show = 1;
	selection_menu[1].rows = 1;
	strcpy(selection_menu[1].string, "DEMO\0");
	
	selection_menu[1].row[0].var_size = 0;	// char
	// this will be done by scripting in the future
	selection_menu[1].row[0].var_ptr8 = &demo_number;
	selection_menu[1].row[0].type = 1;
	selection_menu[1].row[0].min_value = 0x0;
	selection_menu[1].row[0].max_value = 0x9;
	selection_menu[1].row[0].offset_x = 0x70;
	selection_menu[1].row[0].offset_y = 0x58;
	selection_menu[1].row[0].value_digits = 1;
	selection_menu[1].row[0].columns = 0;
	////////////////////////////////////////////////////////////
	
	selection_menu[2].offset_x = 0x10;
	selection_menu[2].offset_y = 0x68;
	selection_menu[2].show = 1;
	selection_menu[2].rows = 1;
	strcpy(selection_menu[2].string, "RINGS\0");
	
	selection_menu[2].row[0].var_size = 1;	// short
	// this will be done by scripting in the future
	selection_menu[2].row[0].var_ptr16 = &player[0].Ring_count;
	selection_menu[2].row[0].type = 1;
	selection_menu[2].row[0].min_value = 0;
	selection_menu[2].row[0].max_value = 999;
	selection_menu[2].row[0].offset_x = 0x70;
	selection_menu[2].row[0].offset_y = 0x68;
	selection_menu[2].row[0].value_digits = 3;
	selection_menu[2].row[0].columns = 0;
	////////////////////////////////////////////////////////////
	
	selection_menu[3].offset_x = 0x10;
	selection_menu[3].offset_y = 0x78;
	selection_menu[3].show = 1;
	selection_menu[3].rows = 1;
	strcpy(selection_menu[3].string, "LIVES\0");
	
	selection_menu[3].row[0].var_size = 0;	// char
	// this will be done by scripting in the future
	selection_menu[3].row[0].var_ptr8 = &player[0].Life_count;
	selection_menu[3].row[0].type = 1;
	selection_menu[3].row[0].min_value = 0;
	selection_menu[3].row[0].max_value = 99;
	selection_menu[3].row[0].offset_x = 0x70;
	selection_menu[3].row[0].offset_y = 0x78;
	selection_menu[3].row[0].value_digits = 2;
	selection_menu[3].row[0].columns = 0;
	////////////////////////////////////////////////////////////
	
	selection_menu[4].offset_x = 0x10;
	selection_menu[4].offset_y = 0x88;
	selection_menu[4].show = 1;
	selection_menu[4].rows = 1;
	strcpy(selection_menu[4].string, "EMERALDS\0");
	
	selection_menu[4].row[0].var_size = 0;	// char
	// this will be done by scripting in the future
	selection_menu[4].row[0].var_ptr8 = &Emerald_count;
	selection_menu[4].row[0].type = 1;
	selection_menu[4].row[0].min_value = 0;
	selection_menu[4].row[0].max_value = 7;
	selection_menu[4].row[0].offset_x = 0x70;
	selection_menu[4].row[0].offset_y = 0x88;
	selection_menu[4].row[0].value_digits = 1;
	selection_menu[4].row[0].columns = 0;
	////////////////////////////////////////////////////////////
	
	selection_menu[5].offset_x = 0x10;
	selection_menu[5].offset_y = 0x98;
	selection_menu[5].show = 1;
	selection_menu[5].rows = 1;
	strcpy(selection_menu[5].string, "FRAME SKIP\0");
	
	selection_menu[5].row[0].var_size = 0;	// char
	// this will be done by scripting in the future
	selection_menu[5].row[0].var_ptr8 = &frame_skip;
	selection_menu[5].row[0].type = 1;
	selection_menu[5].row[0].min_value = 0x0;
	selection_menu[5].row[0].max_value = 0x5;
	selection_menu[5].row[0].offset_x = 0x70;
	selection_menu[5].row[0].offset_y = 0x98;
	selection_menu[5].row[0].value_digits = 1;
	selection_menu[5].row[0].columns = 0;
	////////////////////////////////////////////////////////////
	
	selection_menu[6].offset_x = 0x10;
	selection_menu[6].offset_y = 0xA8;
	selection_menu[6].show = 1;
	selection_menu[6].rows = 1;
	strcpy(selection_menu[6].string, "EDIT MODE\0");
	
	selection_menu[6].row[0].var_size = 0;	// char
	// this will be done by scripting in the future
	selection_menu[6].row[0].var_ptr8 = &edit_mode;
	selection_menu[6].row[0].type = 1;
	selection_menu[6].row[0].min_value = 0x0;
	selection_menu[6].row[0].max_value = 0x4;
	selection_menu[6].row[0].offset_x = 0x70;
	selection_menu[6].row[0].offset_y = 0xA8;
	selection_menu[6].row[0].value_digits = 1;
	selection_menu[6].row[0].columns = 0;
	////////////////////////////////////////////////////////////
	
	selection_menu[7].offset_x = 0xB0;
	selection_menu[7].offset_y = 0x48;
	selection_menu[7].show = 1;
	selection_menu[7].rows = 1;
	strcpy(selection_menu[7].string, "VSYNC\0");
	
	selection_menu[7].row[0].var_size = 0;	// char
	// this will be done by scripting in the future
	selection_menu[7].row[0].var_ptr8 = &vsync_enable;
	selection_menu[7].row[0].type = 1;
	selection_menu[7].row[0].min_value = 0x0;
	selection_menu[7].row[0].max_value = 0x1;
	selection_menu[7].row[0].offset_x = 0x110;
	selection_menu[7].row[0].offset_y = 0x48;
	selection_menu[7].row[0].value_digits = 1;
	selection_menu[7].row[0].columns = 0;
	////////////////////////////////////////////////////////////
	
	selection_menu[8].offset_x = 0xB0;
	selection_menu[8].offset_y = 0x58;
	selection_menu[8].show = 1;
	selection_menu[8].rows = 1;
	strcpy(selection_menu[8].string, "SMART MIX\0");
	
	selection_menu[8].row[0].var_size = 0;	// char
	// this will be done by scripting in the future
	selection_menu[8].row[0].var_ptr8 = &smart_mix;
	selection_menu[8].row[0].type = 1;
	selection_menu[8].row[0].min_value = 0x0;
	selection_menu[8].row[0].max_value = 0x1;
	selection_menu[8].row[0].offset_x = 0x110;
	selection_menu[8].row[0].offset_y = 0x58;
	selection_menu[8].row[0].value_digits = 1;
	selection_menu[8].row[0].columns = 0;
	////////////////////////////////////////////////////////////
	
	selection_menu[9].offset_x = 0xB0;
	selection_menu[9].offset_y = 0x68;
	selection_menu[9].show = 1;
	selection_menu[9].rows = 1;
	strcpy(selection_menu[9].string, "VOLUME\0");
	
	selection_menu[9].row[0].var_size = 0;	// char
	// this will be done by scripting in the future
	selection_menu[9].row[0].var_ptr8 = &audio_volume;
	selection_menu[9].row[0].type = 1;
	selection_menu[9].row[0].min_value = -1;
	selection_menu[9].row[0].max_value = 0x1F;
	selection_menu[9].row[0].offset_x = 0x110;
	selection_menu[9].row[0].offset_y = 0x68;
	selection_menu[9].row[0].value_digits = 2;
	selection_menu[9].row[0].columns = 0;
	////////////////////////////////////////////////////////////
	
	selection_menu[10].offset_x = 0xB0;
	selection_menu[10].offset_y = 0x78;
	selection_menu[10].show = 1;
	selection_menu[10].rows = 1;
	strcpy(selection_menu[10].string, "EMPTY 10\0");
	
	selection_menu[10].row[0].type = 0;
	selection_menu[10].row[0].offset_x = 0x110;
	selection_menu[10].row[0].offset_y = 0x78;
	selection_menu[10].row[0].columns = 0;
	strcpy(selection_menu[10].row[0].string[0], "\0");
	////////////////////////////////////////////////////////////
	
	selection_menu[11].offset_x = 0xB0;
	selection_menu[11].offset_y = 0x88;
	selection_menu[11].show = 1;
	selection_menu[11].rows = 1;
	strcpy(selection_menu[11].string, "EMPTY 11\0");
	
	selection_menu[11].row[0].type = 0;
	selection_menu[11].row[0].offset_x = 0x110;
	selection_menu[11].row[0].offset_y = 0x88;
	selection_menu[11].row[0].columns = 0;
	strcpy(selection_menu[11].row[0].string[0], "\0");
	////////////////////////////////////////////////////////////
	
	selection_menu[12].offset_x = 0xB0;
	selection_menu[12].offset_y = 0x98;
	selection_menu[12].show = 1;
	selection_menu[12].rows = 1;
	strcpy(selection_menu[12].string, "EMPTY 12\0");
	
	selection_menu[12].row[0].type = 0;
	selection_menu[12].row[0].offset_x = 0x110;
	selection_menu[12].row[0].offset_y = 0x98;
	selection_menu[12].row[0].columns = 0;
	strcpy(selection_menu[12].row[0].string[0], "\0");
	////////////////////////////////////////////////////////////
	
	selection_menu[13].offset_x = 0xB0;
	selection_menu[13].offset_y = 0xA8;
	selection_menu[13].show = 1;
	selection_menu[13].rows = 1;
	strcpy(selection_menu[13].string, "EMPTY 13\0");
	
	selection_menu[13].row[0].type = 0;
	selection_menu[13].row[0].offset_x = 0x110;
	selection_menu[13].row[0].offset_y = 0xA8;
	selection_menu[13].row[0].columns = 0;
	strcpy(selection_menu[13].row[0].string[0], "\0");
	////////////////////////////////////////////////////////////
	
	//selection_menu[13].show = 0;
}



void Menu(PLAYER *p){
	int a;
	int b;
	int value;
	char digits;
	
	if(frame == 1){
		if(menu_timer1 > 0)
			menu_timer1--;
		if(menu_timer2 > 0)
			menu_timer2--;
		
		if(key[KEY_UP]){
			if(menu_timer1 == 0){
				menu_timer1 = 12;
				menu_timer2 = 0;
				menu_timer3 = 1;
				menu_timer4 = 1;
				menu_sub_selection--;
				if(menu_sub_selection < 0){
					if(menu_selection == 0)
						menu_selection = (menu_column_size * menu_column_count);
					menu_selection--;
				}
				
				while(selection_menu[menu_selection].show == 0){
					if(menu_selection == 0)
						menu_selection = (menu_column_size * menu_column_count);
					menu_selection--;
				}
				
				if(menu_sub_selection < 0)
					menu_sub_selection = selection_menu[menu_selection].rows - 1;
			}
		}
		else if(key[KEY_DOWN]){
			if(menu_timer2 == 0){
				menu_timer1 = 0;
				menu_timer2 = 12;
				menu_timer3 = 1;
				menu_timer4 = 1;
				menu_sub_selection++;
				if(menu_sub_selection >= selection_menu[menu_selection].rows){
					menu_sub_selection = 0;
					menu_selection++;
					menu_selection %= (menu_column_size * menu_column_count);
				}
				
				while(selection_menu[menu_selection].show == 0){
					menu_selection++;
					menu_selection %= (menu_column_size * menu_column_count);
				}
			}
		}
		else if(key[KEY_LEFT]){
			if(menu_timer3 == 0 && selection_menu[menu_selection].row[menu_sub_selection].type == 0){
				menu_timer1 = 0;
				menu_timer2 = 0;
				menu_timer3 = 1;
				menu_timer4 = 0;
				menu_selection -= menu_column_size;
				if(menu_selection < 0)
					menu_selection += (menu_column_size * menu_column_count);
				while(selection_menu[menu_selection].show == 0){
					menu_selection -= menu_column_size;
					if(menu_selection < 0)
						menu_selection += (menu_column_size * menu_column_count);
				}
				if(menu_sub_selection >= selection_menu[menu_selection].rows)
					menu_sub_selection = selection_menu[menu_selection].rows - 1;
			}
			else if(menu_timer3 == 0 && selection_menu[menu_selection].row[menu_sub_selection].type == 1){
				menu_timer1 = 0;
				menu_timer2 = 0;
				menu_timer3 = 1;
				menu_timer4 = 0;
				if(selection_menu[menu_selection].row[menu_sub_selection].var_size == 0){
					(*selection_menu[menu_selection].row[menu_sub_selection].var_ptr8)--;
					if(*selection_menu[menu_selection].row[menu_sub_selection].var_ptr8 < selection_menu[menu_selection].row[menu_sub_selection].min_value)
						*selection_menu[menu_selection].row[menu_sub_selection].var_ptr8 = (char)(selection_menu[menu_selection].row[menu_sub_selection].max_value);
				}
				else{
					(*selection_menu[menu_selection].row[menu_sub_selection].var_ptr16)--;
					if(*selection_menu[menu_selection].row[menu_sub_selection].var_ptr16 < selection_menu[menu_selection].row[menu_sub_selection].min_value)
						*selection_menu[menu_selection].row[menu_sub_selection].var_ptr16 = (short int)(selection_menu[menu_selection].row[menu_sub_selection].max_value);
				}
			}
		}
		else if(key[KEY_RIGHT]){
			if(menu_timer4 == 0 && selection_menu[menu_selection].row[menu_sub_selection].type == 0){
				menu_timer1 = 0;
				menu_timer2 = 0;
				menu_timer3 = 0;
				menu_timer4 = 1;
				menu_selection += menu_column_size;
				menu_selection %= (menu_column_size * menu_column_count);
				while(selection_menu[menu_selection].show == 0){
					menu_selection += menu_column_size;
					menu_selection %= (menu_column_size * menu_column_count);
				}
				if(menu_sub_selection >= selection_menu[menu_selection].rows)
					menu_sub_selection = selection_menu[menu_selection].rows - 1;
			}
			else if(menu_timer4 == 0 && selection_menu[menu_selection].row[menu_sub_selection].type == 1){
				menu_timer1 = 0;
				menu_timer2 = 0;
				menu_timer3 = 0;
				menu_timer4 = 1;
				if(selection_menu[menu_selection].row[menu_sub_selection].var_size == 0){
					if(*selection_menu[menu_selection].row[menu_sub_selection].var_ptr8 >= selection_menu[menu_selection].row[menu_sub_selection].max_value)
						*selection_menu[menu_selection].row[menu_sub_selection].var_ptr8 = (char)(selection_menu[menu_selection].row[menu_sub_selection].min_value);
					else	// we do it this way to prevent bugs with the max value and size
						(*selection_menu[menu_selection].row[menu_sub_selection].var_ptr8)++;
				}
				else{
					if(*selection_menu[menu_selection].row[menu_sub_selection].var_ptr16 >= selection_menu[menu_selection].row[menu_sub_selection].max_value)
						*selection_menu[menu_selection].row[menu_sub_selection].var_ptr16 = (short int)(selection_menu[menu_selection].row[menu_sub_selection].min_value);
					else	// we do it this way to prevent bugs with the max value and size
						(*selection_menu[menu_selection].row[menu_sub_selection].var_ptr16)++;
				}
			}
		}
		if(!key[KEY_UP])
			menu_timer1 = 0;
		if(!key[KEY_DOWN])
			menu_timer2 = 0;
		if(!key[KEY_LEFT])
			menu_timer3 = 0;
		if(!key[KEY_RIGHT])
			menu_timer4 = 0;
	}
	
	
	
	for(a=0; a < (menu_column_size * menu_column_count); a++){
		if(a == menu_selection)
			DrawText(
				selection_menu[a].offset_x,
				selection_menu[a].offset_y,
				1,
				selection_menu[a].string);
		else
			DrawText(
				selection_menu[a].offset_x,
				selection_menu[a].offset_y,
				0,
				selection_menu[a].string);
		
		
		
		for(b=0; b < selection_menu[a].rows; b++){
			if(selection_menu[a].row[b].type == 1){				
				for(digits = selection_menu[a].row[b].value_digits; digits > 0; digits--){
					if(selection_menu[a].row[b].var_size == 0)
						value = ((*selection_menu[a].row[b].var_ptr8) & (0xF << ((digits * 4) - 4))) >> ((digits * 4) - 4);
					else
						value = ((*selection_menu[a].row[b].var_ptr16) & (0xF << ((digits * 4) - 4))) >> ((digits * 4) - 4);
					
					
					if(value > 9)
						selection_menu[a].row[b].string[0][selection_menu[a].row[b].value_digits - digits] = (value-10)+'A';
					else
						selection_menu[a].row[b].string[0][selection_menu[a].row[b].value_digits - digits] = value+'0';
				}
				
				selection_menu[a].row[b].string[0][(short)selection_menu[a].row[b].value_digits] = '\0';
			}
			
			
			if(b == menu_sub_selection && a == menu_selection)
				DrawText(
					selection_menu[a].row[b].offset_x,
					selection_menu[a].row[b].offset_y,
					1,
					selection_menu[a].row[b].string[0]);
			else
				DrawText(
					selection_menu[a].row[b].offset_x,
					selection_menu[a].row[b].offset_y,
					0,
					selection_menu[a].row[b].string[0]);
		}
	}
}
