
#include "includes.h"

////////////////////////////////////////////////////////////////////////////////

MYMENU * create_menu(int id, char * title, int x, int y, int w, int h)
{
	MYMENU * m=(MYMENU *)malloc(sizeof(MYMENU));
	if(m)
	{
		memset(m, 0, sizeof(MYMENU));
		
		sprintf(m->title, "%s", title);
		if (w==-1){w=100;}
		if (h==-1){h=100;}
		if (x==-1){x=(apage->w/2)-(w/2);}
		if (y==-1){y=((apage->h/2)-(h/2))/2;}
		
		m->id=id;
		m->x=x;
		m->y=y;
		m->w=w;
		m->h=h;
	}
	
	return (m);
}

////////////////////////////////////////////////////////////////////////////////

void destroy_menu(MYMENU * m)
{
	if (m)
	{
		free(m);
	}
}

////////////////////////////////////////////////////////////////////////////////

void draw_menu(MYMENU * m)	//WHITE, GRAY, YELLOW
{	
	if (!m){return;}
	
	if (page2)
	{
		if ((page2->w != m->w) || (page2->h != m->h))
		{
			destroy_bitmap(page2);
			page2=NULL;
		}
	}
	
	if (!page2)
	{
		page2=create_bitmap(m->w, m->h);
	}
	
	if (!page2){return;}
		
	
	clear_to_color(page2, DK_BLUE);
	rect(page2, 0, 0, page2->w-1, page2->h-1, BLUE);
	
	textprintf_centre_ex(page2, font, m->w/2, 10, WHITE, -1, m->title);
	
	if (m->id==1)	//simple text window, nothing special
	{
		
	}
	else
	if (m->id==2)	//staircase confirmation
	{
		if (m->ch==0)
		{
			textprintf_centre_ex(page2, font, m->w/4, m->h-16, -1, WHITE, m->text[0]);	
			textprintf_centre_ex(page2, font, m->w*3/4, m->h-16, WHITE, -1, m->text[1]);
		}
		else
		{
			textprintf_centre_ex(page2, font, m->w/4, m->h-16, WHITE, -1, m->text[0]);	
			textprintf_centre_ex(page2, font, m->w*3/4, m->h-16, -1, WHITE, m->text[1]);			
		}
	}
	else
	if (m->id==3)	//Level Up
	{
		for (int i=0; m->text[i][0]; i++)
		{
			textprintf_ex(page2, font, 15, 25+(10*i), WHITE, -1, m->text[i]);
		}
		
		textprintf_centre_ex(page2, font, m->w/2, m->h-16, WHITE, -1, "- START - ");
	}
	else
	if (m->id==10)  //MAIN MENU
	{
		textprintf_centre_ex(page2, font, m->w/2, 20, WHITE, -1, "By Michael Jensen");
		textprintf_centre_ex(page2, font, m->w/2, m->h-10, WHITE, -1, "(Allegro Speed Hack 2005)");
		
		int c1, c2;
		
		if (m->ch==0){c1=DK_BLUE; c2=WHITE;} else {c1=WHITE; c2=DK_BLUE;}
		textprintf_centre_ex(page2, font, m->w/4, m->h/2, c1, c2, m->text[0]);

		if (m->ch==1){c1=DK_BLUE; c2=WHITE;} else {c1=WHITE; c2=DK_BLUE;}
		if (save_num<1){c1=GRAY; c2=DK_BLUE;}
		textprintf_centre_ex(page2, font, m->w*3/4, m->h/2, c1, c2, m->text[1]);

		if (m->ch==2){c1=DK_BLUE; c2=WHITE;} else {c1=WHITE; c2=DK_BLUE;}
		textprintf_centre_ex(page2, font, m->w/2, m->h*3/4, c1, c2, m->text[2]);
	}
	else
	if (m->id==11) //name
	{
		textprintf_ex(page2, font, 15, 25, WHITE, -1, m->text[0]);
		
		int x=15;
		int y=50;
		for (int i=0; i<26; i++)
		{
			int c1, c2;
			if (m->ch==i){c1=DK_BLUE; c2=WHITE;} else {c1=WHITE; c2=DK_BLUE;}
			textprintf_ex(page2, font, x, y, c1, c2, "%c", 65+i);
			
			x+=15;
			if (x>=(105))
			{x=15; y+=15;}
		}
		
		
	}
	else if (m->id==12)
	{
		int i1=m->ch-5;
		int i2=m->ch+5;
		if (i1<0){i1=0;}
		if (i2>save_num){i2=save_num;}
		
		int y=30;
		for (int i=i1; i<i2; i++)
		{
			int c1, c2;
			if (m->ch==i){c1=DK_BLUE; c2=WHITE;} else {c1=WHITE; c2=DK_BLUE;}
			textprintf_centre_ex(page2, font, m->x/2, y, c1, c2, " %s ", saves[i]);
			y+=10;
		}
	}
	else if (m->id==20)
	{
		int c1, c2;
		
		if (m->ch==0){c1=DK_BLUE; c2=WHITE;} else {c1=WHITE; c2=DK_BLUE;}
		textprintf_centre_ex(page2, font, m->w/4, m->h/2, c1, c2, m->text[0]);

		if (m->ch==1){c1=DK_BLUE; c2=WHITE;} else {c1=WHITE; c2=DK_BLUE;}
		if (save_num<1){c1=GRAY; c2=DK_BLUE;}
		textprintf_centre_ex(page2, font, m->w*3/4, m->h/2, c1, c2, m->text[1]);

		if (m->ch==2){c1=DK_BLUE; c2=WHITE;} else {c1=WHITE; c2=DK_BLUE;}
		textprintf_centre_ex(page2, font, m->w/2, m->h*3/4, c1, c2, m->text[2]);
	}
	
	blit(page2, apage, 0, 0, m->x, m->y, page2->w, page2->h);
}

////////////////////////////////////////////////////////////////////////////////

void POP_MENU()
{
	if (current_menu)
	{
		destroy_menu(current_menu);
		current_menu=NULL;
	}
	
	menu_stack_depth--;
	if (menu_stack_depth<0){menu_stack_depth=0;}
	
	current_menu=menu_stack[menu_stack_depth];
	menu_stack[menu_stack_depth]=NULL;
}

////////////////////////////////////////////////////////////////////////////////

void PUSH_MENU()
{
	menu_stack[menu_stack_depth]=current_menu;
	menu_stack_depth++;
	
	if (menu_stack_depth>=100)
	{menu_stack_depth=99;}
	
	current_menu=NULL;
}

////////////////////////////////////////////////////////////////////////////////

void menu_logic()
{	
	MYMENU *m = current_menu;
	if ((!m) || (!display_menu)){return;}
	
	if (m->id==1)	//A single dialogue window.
	{
		if (control(BTN_START))
		{
			if (current_menu->state[0])
			{
				player->dir=current_menu->state[1];
			}
			
			destroy_menu(current_menu);
			current_menu=NULL;
			display_menu=false;
			
			if (player->hp<1)
			{
				//load_world();
				quitting=true;
				
			}
			
			while (control(BTN_ANY));
			tmcLogic=0;
		}
	}
	else if (m->id==2)	//Up or Down Stair Case, Simple Yes/No
	{
		if (control(BTN_LF))
		{
			m->ch=0;
		}
		
		if (control(BTN_RT))
		{
			m->ch=1;
		}
		
		if (control(BTN_A) || control(BTN_START))	//DO IT!
		{
			if (m->ch==1)
			{
				//change dungeon level:
				if (m->state[1] != dungeon_level)
				{

					int ol=dungeon_level;
					save_map(dungeon_level);
					load_map(m->state[1]);	//crashes here
					
					for (int i=0; i<100; i++)
					{
						if (obj[i])
						{
							if (obj[i]->type == 2 || obj[i]->type==3)
							{
								if (obj[i]->state[1]==ol)
								{
									player->x=obj[i]->x;	
									player->y=obj[i]->y;
								}
							}
						}
					}
				}
			}
			
			destroy_menu(current_menu);
			current_menu=NULL;
			display_menu=false;
			
			while (control(BTN_ANY));
			tmcLogic=0;
		}
	}
	else if (m->id==3)	//LEVEL UP SCREEN
	{
		if (control(BTN_START))
		{
			destroy_menu(current_menu);
			current_menu=NULL;
			display_menu=false;
			
			while (control(BTN_ANY));
			tmcLogic=0;
		}
		
	}
	else if (m->id==10) //MENU SCREEN
	{	
		if (control(BTN_UP)){m->ch=0;}
		if (control(BTN_LF)){m->ch=0;}
		if (control(BTN_RT) && (save_num>0)){m->ch=1;}
		if (control(BTN_DN)){m->ch=2;}
		
		if (control(BTN_START) || control(BTN_A))
		{
			if (m->ch==0) //new game
			{
				destroy_menu(current_menu);
				current_menu=create_menu(11, "NEW GAME", -1, -1, 115, 130);
			}
			else if (m->ch==1) //load game
			{
				destroy_menu(current_menu);
				current_menu=create_menu(12, "LOAD GAME", -1, -1, 100, 150);
			}	
			else if (m->ch==2) //quit
			{quitting=true;}
			
			while (control(BTN_ANY));
			tmcLogic=0;
		}
	}
	else if (m->id==11)
	{
		static int counter=0;
		
		if (counter<1)
		{
			int och=m->ch;
			
			if (control(BTN_UP)){m->ch-=6;counter=10;}
			else if (control(BTN_DN)){m->ch+=6;counter=10;}
			else if (control(BTN_LF)){m->ch--;counter=10;}
			else if (control(BTN_RT)){m->ch++;counter=10;}
			else if (control(BTN_A))
			{
				counter=10;	
				int l=strlen(m->text[0]);
				if (l>11){l=11;}
				
				m->text[0][l]=65+m->ch;
			}	
			else if (control(BTN_B))
			{
				counter=10;
				int l=strlen(m->text[0]);
				l--;
				if (l<0){l=0;}
				m->text[0][l]=0;
			}
			
			while (m->ch<0){m->ch=och;}
			while (m->ch>25){m->ch=och;}
		}
		else
		{counter--;}
		
		if (control(BTN_START))
		{
			int l=strlen(m->text[0]);
			if (l>0)
			{
				sprintf(player_name, m->text[0]);
				if (save_num<100)
				{
					sprintf(saves[save_num], m->text[0]);
					save_num++;
				}
				
				destroy_menu(current_menu);
				current_menu=NULL;
				display_menu=false;
				DRAW_ALL=true;
				
				save_world();
				put_saves();
			}
		}
	}
	else if (m->id==12)
	{
		static int counter=0;
		
		if (counter<1)
		{
			int och=m->ch;
			
			if (control(BTN_UP)){m->ch--;counter=10;}
			else if (control(BTN_DN)){m->ch++;counter=10;}
			
			if (m->ch>=save_num)
			{m->ch=save_num-1;}
			if (m->ch<0)
			{m->ch=0;}
		}
		else
		{counter--;}
		
		if (control(BTN_START) || control(BTN_A))
		{
			sprintf(player_name, saves[m->ch]);
			
			destroy_menu(current_menu);
			current_menu=NULL;
			display_menu=false;
			DRAW_ALL=true;
			
			load_world();
		}
	}
	else if (m->id==20)
	{
		if (control(BTN_UP)){m->ch=0;}
		if (control(BTN_LF)){m->ch=0;}
		if (control(BTN_RT) && (save_num>0)){m->ch=1;}
		if (control(BTN_DN)){m->ch=2;}
		
		
		if (control(BTN_A) || control(BTN_START))
		{
			if (m->ch==0)
			{
				player->hp=player->max_hp;	
			}
			
			if (m->ch==1)
			{
				save_world();	
			}
			
			if (m->ch==2)
			{
				quitting=true;
				saving=true;
			}
		
			while (!control(BTN_ANY));
		
			if (current_menu->state[0])
			{
				player->dir=current_menu->state[1];
			}
			
			destroy_menu(current_menu);
			current_menu=NULL;
			display_menu=false;
			
			while (control(BTN_ANY));
			tmcLogic=0;
		}
	}
}

////////////////////////////////////////////////////////////////////////////////
