#include "menu.hpp"

menu :: menu(){

	SwitchSound = Mix_LoadWAV( "sounds/menu_switch.wav" );
	SelectSound = Mix_LoadWAV( "sounds/menu_enter.wav" );

	MenuBG = load_image( "menu/menubg.jpg" );
	Fog = load_image( "menu/fog.png" );
	SDL_SetAlpha( Fog, SDL_SRCALPHA, 20 );
	MenuCounter = 0;
	SelectedMenu = -1;

	activecolor.r = 255;
	activecolor.g = 100;
	activecolor.b = 100;

	menucolor.r = 100;
	menucolor.g = 0;
	menucolor.b = 0;

	menuY[ 0 ] = 300;
	menuY[ 1 ] = 370;
	menuY[ 2 ] = 500;
	menuY[ 3 ] = 600;
	menuY[ 4 ] = MenuBG->h - 300;


	bgY = 0;

	FogX = 800;
	FogY = 300;

	font = TTF_OpenFont( "fonts/font1.ttf", 45 );

	started = false;
}

menu :: ~menu(){

	Mix_FreeChunk( SwitchSound );
	Mix_FreeChunk( SelectSound );

	SDL_FreeSurface( MenuBG );
	SDL_FreeSurface( Fog );
	TTF_CloseFont( font );
}

void menu :: handle_event( SDL_Event event, bool & Difficulty ){

	if( event.type == SDL_KEYDOWN ){
		switch( event.key.keysym.sym ){

			case SDLK_DOWN : if( MenuCounter < Menus - 1 ){
								Mix_HaltChannel( 0 );
								Mix_PlayChannel( 0, SwitchSound, 0 );
								MenuCounter++;
			}
			break;

			case SDLK_UP : if( MenuCounter > 0 ) {
								Mix_HaltChannel( 0 );
								Mix_PlayChannel( 0, SwitchSound, 0 );
								MenuCounter--;
			}
			break;
			case SDLK_RETURN : SelectedMenu = MenuCounter;

				Mix_PlayChannel( 2, SelectSound, 0 );

				if( SelectedMenu == 0 ){

					started = true;

				}

				if( SelectedMenu == 1 ){

					Difficulty = !Difficulty;

				}

			break;

			default : break;

		}
	}

}

void menu :: show( SDL_Surface* & screen, bool & Difficulty ){

	FogX -= 1;
	if( FogX <= -1600 ) FogX = 800;

	if( menuY[ MenuCounter ] < 300 ){

		for( int i = 0; i < Menus; i++ )
			menuY[ i ] += 10;

		bgY += 10;
		FogY += 10;
	}

	if( menuY[ MenuCounter ] > 300 ){

		for( int i = 0; i < Menus; i++ )
			menuY[ i ] -= 10;

		bgY -= 10;
		FogY -= 10;
	}


	SDL_Surface* message = NULL;

	apply_surface( 0, bgY, MenuBG, screen );

	apply_surface( FogX, FogY, Fog, screen );

	std::stringstream caption0;
	caption0 << ( started ? "Folytatás" : "Új játék");
	message = TTF_RenderText_Solid( font, caption0.str().c_str(), MenuCounter == 0 ? activecolor : menucolor );
	apply_surface( 100, menuY[ 0 ], message, screen );
	SDL_FreeSurface( message );
	message = NULL;

	std::stringstream caption4;
	caption4 << "Nehézség: "<< ( Difficulty ? "Könnyű" : "Nehéz" );
	message = TTF_RenderText_Solid( font, caption4.str().c_str(), MenuCounter == 1 ? activecolor : menucolor );
	apply_surface( 100, menuY[ 1 ], message, screen );
	SDL_FreeSurface( message );
	message = NULL;

	std::stringstream caption1;
	caption1 << "Tudnivalók";
	message = TTF_RenderText_Solid( font, caption1.str().c_str(), MenuCounter == 2 ? activecolor : menucolor );
	apply_surface( 100, menuY[ 2 ], message, screen );
	SDL_FreeSurface( message );
	message = NULL;

	std::stringstream caption2;
	caption2 << "Készítők";
	message = TTF_RenderText_Solid( font, caption2.str().c_str(), MenuCounter == 3 ? activecolor : menucolor );
	apply_surface( 100, menuY[ 3 ], message, screen );
	SDL_FreeSurface( message );
	message = NULL;

	std::stringstream caption3;
	caption3 << "Kilépés";
	message = TTF_RenderText_Solid( font, caption3.str().c_str(), MenuCounter == 4 ? activecolor : menucolor );
	apply_surface( 100, menuY[ 4 ], message, screen );
	SDL_FreeSurface( message );
	message = NULL;

}

int menu :: selectedmenu(){

	return SelectedMenu;

}

void menu :: deselect(){
	SelectedMenu = -1;
}

void menu :: notStarted(){
	started = false;
}

void menu :: credits( SDL_Surface* & screen ){

	SDL_Surface* BG = load_image( "menu/credits.jpg" );
	SDL_Surface* BGF = load_image( "menu/credits_fold.png" );
	int Y = 600;
	SDL_Surface* message = NULL;
	bool quit = false;
	SDL_Event event;
	TIMER myTimer;

	TTF_Font* cfont = TTF_OpenFont( "fonts/font1.ttf", 28 );
	SDL_Color color = { 0, 0, 0 };

	while( !quit &&  ( Y + 750 > 0 ) ){

		myTimer.start();

		while( SDL_PollEvent( &event ) ){
			if( event.type == SDL_KEYDOWN && ( event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_SPACE ) )
				quit = true;
		}

		apply_surface( 0, 0, BG, screen );

		message = TTF_RenderText_Solid( cfont, "Készítette:", color );
		apply_surface( 0, Y, message, screen );
		SDL_FreeSurface( message );

		message = TTF_RenderText_Solid( cfont, "Lezli01", color );
		apply_surface( 0, Y + 25, message, screen );
		SDL_FreeSurface( message );

		message = TTF_RenderText_Solid( cfont, "Zene:", color );
		apply_surface( 0, Y + 150, message, screen );
		SDL_FreeSurface( message );

		message = TTF_RenderText_Solid( cfont, "Alex Dragoo", color );
		apply_surface( 0, Y + 175, message, screen );
		SDL_FreeSurface( message );

		message = TTF_RenderText_Solid( cfont, "Sztori:", color );
		apply_surface( 0, Y + 275, message, screen );
		SDL_FreeSurface( message );

		message = TTF_RenderText_Solid( cfont, "Rolna", color );
		apply_surface( 0, Y + 300, message, screen );
		SDL_FreeSurface( message );

		message = TTF_RenderText_Solid( cfont, "Lezli01", color );
		apply_surface( 0, Y + 325, message, screen );
		SDL_FreeSurface( message );

		message = TTF_RenderText_Solid( cfont, "Bétatesztelők:", color );
		apply_surface( 0, Y + 425, message, screen );
		SDL_FreeSurface( message );

		message = TTF_RenderText_Solid( cfont, "Mráz Ákos", color );
		apply_surface( 0, Y + 450, message, screen );
		SDL_FreeSurface( message );

		message = TTF_RenderText_Solid( cfont, "Gallai Tamás", color );
		apply_surface( 0, Y + 475, message, screen );
		SDL_FreeSurface( message );

		message = TTF_RenderText_Solid( cfont, "és a lelkes Pannon Egyetemes", color );
		apply_surface( 0, Y + 500, message, screen );
		SDL_FreeSurface( message );
		message = TTF_RenderText_Solid( cfont, "hallgatók, köszi!", color );
		apply_surface( 0, Y + 525, message, screen );
		SDL_FreeSurface( message );

		message = TTF_RenderText_Solid( cfont, "Látványvilág:", color );
		apply_surface( 0, Y + 575, message, screen );
		SDL_FreeSurface( message );

		message = TTF_RenderText_Solid( cfont, "Lezli01", color );
		apply_surface( 0, Y + 600, message, screen );
		SDL_FreeSurface( message );

		message = TTF_RenderText_Solid( cfont, "Programozta:", color );
		apply_surface( 0, Y + 700, message, screen );
		SDL_FreeSurface( message );

		message = TTF_RenderText_Solid( cfont, "Lezli01", color );
		apply_surface( 0, Y + 725, message, screen );
		SDL_FreeSurface( message );



		apply_surface( 0, 600 - BGF->h, BGF, screen );

		Y -= 1;

		SDL_Flip( screen );
		if( myTimer.get_ticks() < 1000 / 25 ){
			SDL_Delay( 1000 / 25 - myTimer.get_ticks() );
		}

	}

	TTF_CloseFont( cfont );

	SDL_FreeSurface( BG );
	SDL_FreeSurface( BGF );

}

void menu :: help( SDL_Surface* & screen ){

	SDL_Surface* bg = load_image( "menu/help.jpg" );

	bool quit = false;
	SDL_Event event;
	TIMER myTimer;
	while( !quit ){

		myTimer.start();

		while( SDL_PollEvent( &event ) )
			if( event.type == SDL_KEYDOWN && ( event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_SPACE ) )
				quit = true;

		apply_surface( 0, 0, bg, screen );

		SDL_Flip( screen );

		if( myTimer.get_ticks() < 1000 / 25 ){
			SDL_Delay( 1000 / 25 - myTimer.get_ticks() );
		}

	}

	SDL_FreeSurface( bg );

}

