#include "ridinghood.hpp"

ridinghood :: ridinghood(){

	//sounds
	MapSound = Mix_LoadWAV( "sounds/map.wav" );
	InventorySound = Mix_LoadWAV( "sounds/inventory.wav" );
	StepSound = Mix_LoadWAV( "sounds/step2.wav" );

	Apple = Mix_LoadWAV( "sounds/apple.wav" );
	LightOnSound = Mix_LoadWAV( "sounds/lamp_on.wav" );
	LightOffSound = Mix_LoadWAV( "sounds/lamp_off.wav" );
	LightRecharge = Mix_LoadWAV( "sounds/lamp_recharge.wav" );

	Distance = 0;

	//Character

	SpriteSheet = load_image( "characters/ridinghood.png" );

	for( int i = 0; i < 4; i++ )
		for( int j = 0; j < 4; j++ ){
			Frames[ i ][ j ].x = j * Width;
			Frames[ i ][ j ].y = i * Height;
			Frames[ i ][ j ].h = Height;
			Frames[ i ][ j ].w = Width;
		}

	Status = 0;
	Frame = 0;
	xVel = 0;
	yVel = 0;

	Velocity = 3;

	//MAP
	mapY = 600;
	MapSheet = load_image( "characters/map.png" );
	MapX = load_image( "characters/map_x.png" );
	mapOn = false;
	MapFlowerX = load_image( "characters/map_flower_x.png" );
	MapFog = load_image( "effects/blanker_dark2.png" );
	SDL_SetAlpha( MapFog, SDL_SRCALPHA, 80 );

	//Maximalize Energy
	RunEnergy = 3000;
	isRunning = false;

	//Candle
	LightOn = false;
	ExtraLightOn = false;
	CandleSheet = load_image( "characters/candle_meter.png" );
	Candles = 0;
	CandleMeter = 0;
	CandleFrame = 0;

	for( int i = 0; i < 5; i++ ){
		CandleOffsets[ i ].x = i * CandleWidth;
		CandleOffsets[ i ].y = 0;
		CandleOffsets[ i ].w = CandleWidth;
		CandleOffsets[ i ].h = CandleHeight;
	}

	//Flowers
	Flowers = 0;

	//Apples
	Apples = 0;

	//Inventory
	invOn = false;
	invX = 800;
	InventorySheet = load_image( "characters/inventory.png" );
	font = TTF_OpenFont( "fonts/font1.ttf", 36 );
	textcolor.r = 185;
	textcolor.g = 145;
	textcolor.b = 0;

	maxcolor.r = 200;
	maxcolor.g = 20;
	maxcolor.b = 0;

	//HEART
	HP = 100;
	HeartSheet = load_image( "characters/heart.png" );

	HOffsets[ 0 ].x = 0;
	HOffsets[ 0 ].y = 0;
	HOffsets[ 0 ].w = 125;
	HOffsets[ 0 ].h = 145;

	HOffsets[ 1 ].x = 125;
	HOffsets[ 1 ].y = 0;
	HOffsets[ 1 ].w = 125;
	HOffsets[ 1 ].h = 145;

}

ridinghood :: ~ridinghood(){

	Mix_FreeChunk( StepSound );
	Mix_FreeChunk( MapSound );
	Mix_FreeChunk( InventorySound );
	Mix_FreeChunk( Apple );
	Mix_FreeChunk( LightOnSound );
	Mix_FreeChunk( LightOffSound );
	Mix_FreeChunk( LightRecharge );

	SDL_FreeSurface( SpriteSheet );
	SDL_FreeSurface( MapSheet );
	SDL_FreeSurface( MapX );
	SDL_FreeSurface( MapFlowerX );
	SDL_FreeSurface( CandleSheet );
	SDL_FreeSurface( InventorySheet );
	SDL_FreeSurface( HeartSheet );
	SDL_FreeSurface( MapFog );

	TTF_CloseFont( font );

}

void ridinghood	:: goRun(){
	isRunning = true;

	if( xVel == -3 ) xVel = -7;
	if( xVel == 3 ) xVel = 7;
	if( yVel == -3 ) yVel = -7;
	if( yVel == 3 ) yVel = 7;

	Velocity = 7;
}

void ridinghood :: goWalk(){
	isRunning = false;

	if( xVel == -7 ) xVel = -3;
	if( xVel == 7 ) xVel = 3;
	if( yVel == -7 ) yVel = -3;
	if( yVel == 7 ) yVel = 3;

	Velocity = 3;
}

void ridinghood :: refresh( bool Difficulty ){

	//Light
	if( CandleMeter > 0 && LightOn )
		ExtraLightOn ? CandleMeter -= ( Difficulty ? 9 : 18 ) : CandleMeter -= ( Difficulty ? 1 : 2 );

	if( LightOn && CandleMeter <= 0 ){
		LightOn = false;
		ExtraLightOn = false;
	}


	//Running
	if( isRunning && ( xVel != 0 || yVel != 0 ) && RunEnergy > 0 ){
		RunEnergy -= Difficulty ? 10 : 20;
	}

	if( RunEnergy <= 0 )goWalk();

	if( isRunning == false && RunEnergy < 3000 ){
		RunEnergy += 10;
	}

}

void ridinghood :: handle_event( SDL_Event event, bool Difficulty ){


	if( event.type == SDL_KEYDOWN ){
		switch( event.key.keysym.sym ){
			//MOVING
			case SDLK_UP : yVel -= Velocity; break;
			case SDLK_DOWN : yVel += Velocity; break;
			case SDLK_RIGHT : xVel += Velocity; break;
			case SDLK_LEFT : xVel -= Velocity; break;

			//MAP
			case SDLK_m : case SDLK_TAB : mapOn = !mapOn;  Mix_PlayChannel( -1, MapSound, 0 ); break;

			//Light
			case SDLK_RETURN : LightOn = !LightOn;
							  Mix_HaltChannel( 6 );
							  Mix_PlayChannel( 6, LightOn ? LightOnSound : LightOffSound, 0 );
							  if( CandleMeter <= 0 ) LightOn = false;
							  if( LightOn == false ) ExtraLightOn = false;
			break;

			case SDLK_c : case SDLK_3 : if( Candles > 0 ){
							Mix_HaltChannel( 6 );
							Mix_PlayChannel( 6, LightRecharge, 0 );
							Candles -= 1;
							CandleMeter = 2000;

						  }
				break;

			case SDLK_SPACE : if( LightOn ) ExtraLightOn = true; break;

			//Running
			case SDLK_LCTRL : case SDLK_r : goRun();	break;

			//Inventory
			case SDLK_b : case SDLK_1 : invOn = !invOn; Mix_PlayChannel( -1, InventorySound, 0 ); break;

			//Apple
			case SDLK_v : case SDLK_2 : if( Apples > 0 ){

				Mix_HaltChannel( 5 );
				Mix_PlayChannel( 5, Apple, 0 );

				Apples -= 1;
				HP += Difficulty ? 20 : 5;
				if( HP > 100 ) HP = 100;

			}
			break;

			default : break;

		}
	}

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

			//Moving
			case SDLK_UP : yVel += Velocity; break;
			case SDLK_DOWN : yVel -= Velocity; break;
			case SDLK_RIGHT : xVel -= Velocity; break;
			case SDLK_LEFT : xVel += Velocity; break;

			//Running
			case SDLK_LCTRL : case SDLK_r : goWalk(); break;

			//Light
			case SDLK_SPACE : ExtraLightOn = false; break;

			default : break;

		}
	}


	if( yVel > 0 ) Status = Down;
	if( yVel < 0 ) Status = Up;
	if( xVel > 0 ) Status = Right;
	if( xVel < 0 ) Status = Left;


}

SDL_Surface* ridinghood :: getSpriteSheet(){
	return SpriteSheet;
}

SDL_Rect ridinghood :: getFrame(){

	if( xVel != 0 || yVel != 0 ){
		if( !Mix_Playing( 4 ) )
			Mix_PlayChannel( 4, StepSound, -1 );
	}
	else{
		if( Mix_Playing( 4 ) )
			Mix_HaltChannel( 4 );
	}
	Frame++;

	if( Frame >= 8 ) Frame = 0;

	if( xVel != 0 || yVel != 0 )
		return Frames[ Status ][ Frame / 2 ];

	return Frames[ Status ][ 0 ];

}

int ridinghood :: getXVel(){
	return xVel;
}

int ridinghood :: getYVel(){
	return yVel;
}

void ridinghood :: show_stats( SDL_Surface* & screen, int mapx, int mapy, SDL_Rect* FlowerCoords, bool* Collected ){

	//Show candle meter if its on
	if( LightOn ){

		apply_surface( 5, 555, CandleSheet, screen, &CandleOffsets[ 4 ] );

		int i = 0;
		for( i = 0; i < CandleMeter / 100; i++ ){
			apply_surface( 5, 555 - ( i * 15 ), CandleSheet, screen, &CandleOffsets[ 3 ] );
		}

		CandleFrame++;
		if( CandleFrame >= 3 ) CandleFrame = 0;
		apply_surface( 5, 555 - ( i * 15 ), CandleSheet, screen, &CandleOffsets[ CandleFrame ] );

	}

	//Inventory
	if( invOn ){
		if( invX > 520 )invX -= 50;
	}
	if( !invOn ){
		if( invX < 800 ) invX += 50;
	}
	if( invX < 800 ){

		apply_surface( invX, 0, InventorySheet, screen );

		//Inventory
		SDL_Surface* message = NULL;
		std::stringstream caption0;
		caption0 << "LELTÁR";
		message = TTF_RenderText_Solid( font, caption0.str().c_str(), textcolor );
		apply_surface( invX + 125, 70, message, screen );
		SDL_FreeSurface( message );
		message = NULL;

		//Flower number
		std::stringstream caption1;
		caption1 << Flowers << " darab";
		message = TTF_RenderText_Solid( font, caption1.str().c_str(), textcolor );
		apply_surface( invX + 160, 130, message, screen );
		SDL_FreeSurface( message );
		message = NULL;

		//Candles number
		std::stringstream caption2;
		caption2 << Candles << " darab";
		message = TTF_RenderText_Solid( font, caption2.str().c_str(), Candles == 3 ? maxcolor : textcolor );
		apply_surface( invX + 160, 220, message, screen );
		SDL_FreeSurface( message );
		message = NULL;

		//Apples number
		std::stringstream caption3;
		caption3 << Apples << " darab";
		message = TTF_RenderText_Solid( font, caption3.str().c_str(), Apples == 3 ? maxcolor : textcolor );
		apply_surface( invX + 160, 310, message, screen );
		SDL_FreeSurface( message );
		message = NULL;

	}

	//Show Heart
	SDL_Surface* temp1 = load_image( "characters/heart.png" );

	SDL_Rect Hoffset;
	Hoffset.x = 0;
	Hoffset.y = 0;
	Hoffset.w = 250;
	Hoffset.h = 145 - (int)( (float)( (float)145 * (float)HP ) / (float)100 );

	SDL_FillRect( temp1, &Hoffset, SDL_MapRGB( temp1->format, 0, 0xFF, 0xFF ) );

	apply_surface( 0, 0, HeartSheet, screen, &HOffsets[ 0 ] );
	apply_surface( 0, 0, temp1, screen, &HOffsets[ 1 ] );

	SDL_FreeSurface( temp1 );

	//Unfoggin MAPFOG
	SDL_Rect XOffset;
	XOffset.x = (int)(( 780 * -mapx ) / 10000) + 32;
	XOffset.y = (int)(( 580 * -mapy ) / 6000) + 5;
	XOffset.w = MapX->w;
	XOffset.h = MapX->h;

	SDL_FillRect( MapFog, & XOffset, SDL_MapRGB( MapFog->format, 0, 0xFF, 0xFF ) );

	//Show stamina

	SDL_Surface* StaminaSheet = load_image( "characters/stamina.png" );

	SDL_Rect Stamina;
	Stamina.x = 0;
	Stamina.y = 0;
	Stamina.w = 10;
	Stamina.h = 145 - (int)( (float)( (float)145 * (float)RunEnergy ) / (float)3000 );;
	SDL_FillRect( StaminaSheet, &Stamina, SDL_MapRGB( StaminaSheet->format, 0, 0xFF, 0xFF ) );
	SDL_SetAlpha( StaminaSheet, SDL_SRCALPHA, 50 );
	apply_surface( 0, 0, StaminaSheet, screen );

	SDL_FreeSurface( StaminaSheet );

	//Show map if on
	if( mapOn ){

		if( mapY > 10 ) mapY -= 50;
		if( mapY < 10 ) mapY = 10;

		apply_surface( 0, mapY, MapSheet, screen );
		apply_surface( 0, mapY, MapFog, screen );

		if( mapY == 10 ){

			float x = ( 780 * -mapx ) / 10000;

			float y = ( 580 * -mapy ) / 6000;

			apply_surface( (int)x + 25 + 10, (int)y + 15, MapX, screen );

			for( int i = 0; i < 6; i++ ){
				if( Collected[ i ] == false ){
					float fx = ( 780 * FlowerCoords[ i ].x ) / 10000;
					float fy = ( 580 * FlowerCoords[ i ].y ) / 6000;
					apply_surface( (int)fx + 10, (int)fy - 10, MapFlowerX, screen );
				}

			}
		}

	}
	if( !mapOn ){
		if( mapY < 600 ) {
			mapY += 50;
			apply_surface( 10, mapY, MapSheet, screen );
		}
		if( mapY > 600 ) mapY = 600;
	}
}

bool ridinghood :: isLightOn(){
	return LightOn;
}

bool ridinghood :: isExtraLightOn(){
	return ExtraLightOn;
}

int& ridinghood :: getCandles(){
	return Candles;
}

int& ridinghood :: getFlowers(){
	return Flowers;
}

int& ridinghood :: getApples(){
	return Apples;
}

int& ridinghood :: getHP(){
	return HP;
}

void ridinghood :: NullVel(){
	xVel = 0;
	yVel = 0;
	goWalk();
	ExtraLightOn = false;
}
