#include "instructions.hpp"

instructions :: instructions(){

	MessageBox = load_image( "instructions/messagebox.png" );
	font = TTF_OpenFont( "fonts/font1.ttf", 28 );
	textcolor.r = 185;
	textcolor.g = 145;
	textcolor.b = 0;


	Tut_Candle = true;
	Tut_Apple = true;
	Tut_Light = true;
	Tut_Eat = true;
	Tut_Enemy = true;
	Tut_Flower = true;
	Tut_Bigfog = true;
	Tut_Intro = true;

	Tut_Flower1 = true;
	Tut_Flower2 = true;
	Tut_Flower3 = true;
	Tut_Flower4 = true;
	Tut_Flower5 = true;
	Tut_Flower6 = true;
	Tut_Ending = true;
	Tut_End = true;

	PageSound = Mix_LoadWAV( "sounds/menu_switch.wav" );
	Music = Mix_LoadMUS( "sounds/menumusic.wav" );
	BackMusic = Mix_LoadMUS( "sounds/music.wav" );
	ChapterSound = Mix_LoadWAV( "sounds/chapter.wav" );

}

instructions :: ~instructions(){

	Mix_FreeChunk( ChapterSound );
	Mix_FreeChunk( PageSound );
	Mix_FreeMusic( Music );
	Mix_FreeMusic( BackMusic );
	SDL_FreeSurface( MessageBox );


}

void instructions :: show( SDL_Surface* & screen ){

	Mix_HaltChannel( 0 );
	Mix_HaltChannel( 1 );
	Mix_HaltChannel( 2 );
	Mix_HaltChannel( 3 );
	Mix_HaltChannel( 4 );
	Mix_HaltChannel( 5 );
	Mix_HaltChannel( 6 );
	Mix_HaltChannel( 7 );

	MY = 0 - MessageBox->h;
	bool quit = false;
	SDL_Event event;

	Uint8 * keystates = SDL_GetKeyState( NULL );

	SDL_Surface* tempScreen = resize_surface( screen, 800, 600 );
	SDL_Surface* message = NULL;

	while( quit == false ){

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

		if( MY < 0 ) MY += 10;
		if( MY > 0 ) MY = 0;

		apply_surface( 0, 0, tempScreen, screen );
		apply_surface( 0, MY, MessageBox, screen );

		//Showing text
		message = TTF_RenderText_Solid( font, line1.c_str(), textcolor );
		apply_surface( 310, MY + 30, message, screen );
		SDL_FreeSurface( message );
		message = TTF_RenderText_Solid( font, line2.c_str(), textcolor );
		apply_surface( 310, MY + 70, message, screen );
		SDL_FreeSurface( message );
		message = TTF_RenderText_Solid( font, line3.c_str(), textcolor );
		apply_surface( 310, MY + 110, message, screen );
		SDL_FreeSurface( message );
		message = TTF_RenderText_Solid( font, line4.c_str(), textcolor );
		apply_surface( 310, MY + 150, message, screen );
		SDL_FreeSurface( message );
		message = TTF_RenderText_Solid( font, line5.c_str(), textcolor );
		apply_surface( 310, MY + 190, message, screen );
		SDL_FreeSurface( message );
		message = TTF_RenderText_Solid( font, line6.c_str(), textcolor );
		apply_surface( 310, MY + 230, message, screen );
		SDL_FreeSurface( message );


		if( keystates[ SDLK_UP ] || keystates[ SDLK_DOWN] ||
			keystates[ SDLK_LEFT ] || keystates[ SDLK_RIGHT ] )
				quit = false;

		SDL_Flip( screen );

	}

}

bool instructions :: Tutorial( SDL_Surface* & screen, int flag ){

	line1 = "";
	line2 = "";
	line3 = "";
	line4 = "";
	line5 = "";
	line6 = "";

	if( flag & TUT_CANDLE ){

		if( Tut_Candle ){

			Tut_Candle = false;

			line1 = "Össze kéne szednem pár gyertyát, mert";
			line2 = "túlságosan sötét van. Ha jól látom van is";
			line3 = "itt egy pár elszórva. Ez érdekes, odabent egy";
			line4 = "darabot se találtam. Vajon ki szórta el ezeket?";
			line5 = "Térkép: ( M ) vagy ( Tab )";
			line6 = "Mozgás: ( Nyilakkal )";
			show( screen );
			return true;

		}

	}

	if( flag & TUT_APPLE ){

		if( Tut_Apple ){

			Tut_Apple = false;

			line1 = "Úgy nézem lehullottak az almák a fákról.";
			line2 = "Ha már bent nem találtam semmi harapni valót,";
			line3 = "akkor ezek is megteszik. Biztos szükségem lesz";
			line4 = "majd az energiára.";
			show( screen );
			return true;

		}

	}

	if( flag & TUT_LIGHT ){

		if( Tut_Light ){

			Tut_Light = false;

			line1 = "Mostmár van gyertyám!";
			line2 = "Csak be kell tennem a lámpásba és már van";
			line3 = "is fény! Elég nagyok, nem hiszem, hogy";
			line4 = "háromnál többet elbírok.";
			line5 = "Behelyezés: ( C ) vagy ( 3 )";
			line6 = "Lámpagyújtás: ( Enter )";
			show( screen );
			return true;

		}

	}

	if( flag & TUT_EAT ){

		if( Tut_Eat ){

			Tut_Eat = false;

			line1 = "Éljen! Szereztem ennivalót!";
			line2 = "Mostmár tudok enni, ha fogytán az energiám!";
			line3 = "Sajnos háromnál több nem fér a zsebembe.";
			line4 = "Energiatöltés: ( V ) vagy ( 2 )";
			line5 = "Leltár megtekintése: ( B ) vagy ( 1 )";
			show( screen );
			return true;

		}

	}

	if( flag & TUT_ENEMY ){

		if( Tut_Enemy ){

			Tut_Enemy = false;

			line1 = "Úristen! Ez miféle túlvilági teremtmény?";
			line2 = "A sötétből jött elő, megpróbálom erős fénnyel";
			line3 = "elűzni!";
			line5 = "Fény erősítése: ( Space )";
			line6 = "Futás: ( Bal Ctrl ) vagy ( R )";
			show( screen );
			return true;

		}

	}

	if( flag & TUT_FLOWER ){

		if( Tut_Flower ){

			Tut_Flower = false;

			line1 = "Jajj, el is felejtettem!";
			line2 = "A virág csak nappal szedhető le, este";
			line3 = "visszabújik a földbe. Meg kell próbálnom";
			line4 = "a lámpásommal előcsalogatni, és utána leszedni.";
			line5 = "Talán erős fénnyel még gyorsabban kinő!";
			show( screen );
			return true;

		}

	}

	if( flag & TUT_BIGFOG ){

		if( Tut_Bigfog ){

			Tut_Bigfog = false;

			line1 = "Úristen! Ez hogy lehetséges?";
			line2 = "Lehet, hogy csak hallucinálok, de akkor";
			line3 = "is jobb, ha nem megyek a közelébe!";
			line4 = "Kezdek egyre jobban becsavarodni...";
			show( screen );
			return true;

		}

	}

	if( flag & TUT_END ){

		if( Tut_End ){

			Tut_End = false;

			line1 = "Ideje mostmár hazamenni!";
			line2 = "Remélem még időben el tudom";
			line3 = "készíteni a gyógyszert!";
			line4 = "Most azonban futás hazafele!";
			show( screen );
			return true;

		}

	}



	return false;

}

bool instructions :: show( SDL_Surface* & screen, int flag ){
	bool rbool = false;
	vector< SDL_Surface* > Pages( 0 );
	SDL_Surface* Chapter = NULL;
	Uint8 *keystates = SDL_GetKeyState( NULL );

	if( flag & TUT_INTRO && Tut_Intro == true ){
		Tut_Intro = false;
		rbool = true;
		Pages.resize( 3 );
		Pages[ 0 ] = load_image( "instructions/book/0101.jpg" );
		Pages[ 1 ] = load_image( "instructions/book/0102.jpg" );
		Pages[ 2 ] = load_image( "instructions/book/0103.jpg" );
		Chapter = load_image( "instructions/book/ch1.jpg" );
	}

	if( flag & TUT_FLOWER1 && Tut_Flower1 == true ){
		Tut_Flower1 = false;
		rbool = true;
		Pages.resize( 1 );
		Pages[ 0 ] = load_image( "instructions/book/0201.jpg" );
		Chapter = load_image( "instructions/book/ch2.jpg" );
	}
	if( flag & TUT_FLOWER2 && Tut_Flower2 == true ){
		Tut_Flower2 = false;
		rbool = true;
		Pages.resize( 1 );
		Pages[ 0 ] = load_image( "instructions/book/0301.jpg" );
		Chapter = load_image( "instructions/book/ch3.jpg" );
	}
	if( flag & TUT_FLOWER3 && Tut_Flower3 == true ){
		Tut_Flower3 = false;
		rbool = true;
		Pages.resize( 1 );
		Pages[ 0 ] = load_image( "instructions/book/0401.jpg" );
		Chapter = load_image( "instructions/book/ch4.jpg" );
	}
	if( flag & TUT_FLOWER4 && Tut_Flower4 == true ){
		Tut_Flower4 = false;
		rbool = true;
		Pages.resize( 1 );
		Pages[ 0 ] = load_image( "instructions/book/0501.jpg" );
		Chapter = load_image( "instructions/book/ch5.jpg" );
	}
	if( flag & TUT_FLOWER5 && Tut_Flower5 == true ){
		Tut_Flower5 = false;
		rbool = true;
		Pages.resize( 1 );
		Pages[ 0 ] = load_image( "instructions/book/0601.jpg" );
		Chapter = load_image( "instructions/book/ch6.jpg" );
	}
	if( flag & TUT_FLOWER6 && Tut_Flower6 == true ){
		Tut_Flower6 = false;
		rbool = true;
		Pages.resize( 1 );
		Pages[ 0 ] = load_image( "instructions/book/0701.jpg" );
		Chapter = load_image( "instructions/book/ch7.jpg" );
	}
	if( flag & TUT_ENDING && Tut_Ending == true ){
		Tut_Ending = false;
		rbool = true;
		Pages.resize( 3 );
		Pages[ 0 ] = load_image( "instructions/book/0801.jpg" );
		Pages[ 1 ] = load_image( "instructions/book/0802.jpg" );
		Pages[ 2 ] = load_image( "instructions/book/0803.jpg" );
		Chapter = load_image( "instructions/book/ch8.jpg" );
	}

	if( rbool ){

		Mix_HaltChannel( 0 );
		Mix_HaltChannel( 1 );
		Mix_HaltChannel( 2 );
		Mix_HaltChannel( 3 );
		Mix_HaltChannel( 4 );
		Mix_HaltChannel( 5 );
		Mix_HaltChannel( 6 );
		Mix_HaltChannel( 7 );

		int Alpha = 0;
		bool quit = false;
		TIMER myTimer;

		Mix_HaltMusic();
		Mix_PlayMusic( Music, -1 );

		SDL_Event event;
		unsigned int CurrentPage = 0;

		SDL_Color color = { 150, 20, 0 };
		SDL_Surface* message = TTF_RenderText_Solid( font, "Lapozás : Space", color );

		while( CurrentPage != ( Pages.size() ) ){
			myTimer.start();
			SDL_SetAlpha( Pages[ CurrentPage ], SDL_SRCALPHA, Alpha );
			if( Alpha < 255 ) Alpha += 5;
			apply_surface( 0, 0, Pages[ CurrentPage ], screen );
			apply_surface( 0, 0, message, screen );
			SDL_Flip( screen );

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

						case SDLK_SPACE : CurrentPage++; Mix_PlayChannel( -1, PageSound, 0 );  Alpha = 0; break;

						case SDLK_ESCAPE : CurrentPage = Pages.size(); break;

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

		for( vector< SDL_Surface* >::iterator i = Pages.begin(); i!= Pages.end(); i++ )
			SDL_FreeSurface( *i );
		SDL_FreeSurface( message );

		Mix_HaltMusic();
		Mix_PlayMusic( BackMusic, -1 );

		Mix_PlayChannel( -1, ChapterSound, 0 );
		Alpha = 0;

		TIMER myTimer2;
		myTimer2.start();

		while( !quit ){
			myTimer.start();
			SDL_SetAlpha( Chapter, SDL_SRCALPHA, Alpha );
			if( Alpha < 255 ) Alpha += 5;

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

						case SDLK_SPACE : quit = true; break;
						case SDLK_ESCAPE : quit = true; break;

						default : break;
					}
			}

			if( myTimer2.get_ticks() > 7000 ) quit = true;

			apply_surface( 0, 0, Chapter, screen );
			SDL_Flip( screen );

			if( keystates[ SDLK_UP ] || keystates[ SDLK_DOWN] ||
				keystates[ SDLK_LEFT ] || keystates[ SDLK_RIGHT ] )
				quit = false;

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

		}

		SDL_FreeSurface( Chapter );

	}

	return rbool;
}
