#include "flower.hpp"

flowers :: flowers(){

	Found = false;

	FlowerSheet = load_image( "items/flower.png" );

	for( int i = 0; i < 6; i++ ){

		Offsets[ i ].x = i * 60;
		Offsets[ i ].y = 0;
		Offsets[ i ].w = 60;
		Offsets[ i ].h = 70;

	}

	for( int i = 0; i < FlowerNumber; i++ ){

		Energy[ i ] = 0;
		isCollectable[ i ] = false;
		isCollected[ i ] = false;
		Coords[ i ].x = -50;
		Coords[ i ].y = -50;
		Coords[ i ].w = 60;
		Coords[ i ].h = 70;

		Frame[ i ] = 0;

	}



	Coords[ 0 ].x = 2941;
	Coords[ 0 ].y = 588;
	Coords[ 1 ].x = 6472;
	Coords[ 1 ].y = 470;
	Coords[ 2 ].x = 7354;
	Coords[ 2 ].y = 2536;
	Coords[ 3 ].x = 4762;
	Coords[ 3 ].y = 4401;
	Coords[ 4 ].x = 8588;
	Coords[ 4 ].y = 5511;
	Coords[ 5 ].x = 1111;
	Coords[ 5 ].y = 4653;

	for( int i = 0; i < FlowerNumber; i++ )
	{
		OriginalCoords[ i ].x = Coords[ i ].x;
		OriginalCoords[ i ].y = Coords[ i ].y;
	}


}

flowers :: ~flowers(){

	SDL_FreeSurface( FlowerSheet );

}

void flowers :: move( int xVel, int yVel, bool collision, bool isLightOn, bool isExtraLightOn ){

	if( !collision )
	for( int i = 0; i < FlowerNumber; i++ ){
		Coords[ i ].x -= xVel;
		Coords[ i ].y -= yVel;
	}

	if( isLightOn ){

		SDL_Rect lighthitbox1, lighthitbox2;

		lighthitbox1.x = 288;
		lighthitbox1.y = 269;
		lighthitbox1.w = 243;
		lighthitbox1.h = 116;

		lighthitbox2.x = 342;
		lighthitbox2.y = 242;
		lighthitbox2.w = 140;
		lighthitbox2.h = 170;

		SDL_Rect lighthitbox3, lighthitbox4;

		lighthitbox3.x = 200;
		lighthitbox3.y = 275;
		lighthitbox3.w = 420;
		lighthitbox3.h = 110;

		lighthitbox4.x = 305;
		lighthitbox4.y = 225;
		lighthitbox4.w = 207;
		lighthitbox4.h = 215;

		for( int i = 0; i < FlowerNumber; i++ ){

			if( isCollected[ i ] == false ){
			if( check_collision( Coords[ i ], lighthitbox3 ) || check_collision( Coords[ i ], lighthitbox4 ) ){
				if( isExtraLightOn ) Energy[ i ] += 39;
				Found = true;
			}

			if( check_collision( Coords[ i ], lighthitbox1 ) || check_collision( Coords[ i ], lighthitbox2 ) ){
				Energy[ i ] += 1;
				Found = true;
			}

				if( Energy[ i ] >= 1000 ) Frame[ i ] = 1;
				if( Energy[ i ] >= 2000 ) Frame[ i ] = 2;
				if( Energy[ i ] >= 3000 ) Frame[ i ] = 3;
				if( Energy[ i ] >= 4000 ) {
						Frame[ i ] = 4;
						isCollectable[ i ] = true;
				}


			}
		}

	}
}

void flowers :: collect( int& FlowerCount ){

	SDL_Rect RHHitbox;

	RHHitbox.x = 400;
	RHHitbox.y = 343;
	RHHitbox.w = 31;
	RHHitbox.h = 4;

	for( int i = 0; i < FlowerNumber; i++ ){
		if( check_collision( RHHitbox, Coords[ i ] ) && isCollectable[ i ] ){
			isCollectable[ i ] = false;
			isCollected[ i ] = true;
			FlowerCount += 1;
			Frame[ i ] = 5;
		}
	}

}

void flowers :: show_stats( SDL_Surface* & screen , bool isLightOn, bool isExtraLightOn ){

	if( isLightOn ){

			SDL_Rect lighthitbox1, lighthitbox2;

			lighthitbox1.x = 288;
			lighthitbox1.y = 269;
			lighthitbox1.w = 243;
			lighthitbox1.h = 116;

			lighthitbox2.x = 342;
			lighthitbox2.y = 242;
			lighthitbox2.w = 140;
			lighthitbox2.h = 170;

			SDL_Rect lighthitbox3, lighthitbox4;

			lighthitbox3.x = 200;
			lighthitbox3.y = 275;
			lighthitbox3.w = 420;
			lighthitbox3.h = 110;

			lighthitbox4.x = 305;
			lighthitbox4.y = 225;
			lighthitbox4.w = 207;
			lighthitbox4.h = 215;

			for( int i = 0; i < FlowerNumber; i++ ){

				if( ( check_collision( Coords[ i ], lighthitbox1 ) || check_collision( Coords[ i ], lighthitbox2 ) ) ||
					( isExtraLightOn && ( check_collision( Coords[ i ], lighthitbox3 ) || check_collision( Coords[ i ], lighthitbox4 )) )){

					if( Energy[ i ] < 4000 ){
						SDL_Surface* fm2 = load_image( "items/flower_meter2.png" );
						SDL_Surface* fm1 = load_image( "items/flower_meter.png" );


						SDL_Rect offset;
						offset.x = 0;
						offset.y = 0;
						offset.w = 100;
						offset.h = 600 - (int)( (float)600 * (float)Energy[ i ] / (float)4000 );

						SDL_FillRect( fm1, &offset, SDL_MapRGB( fm1->format, 0, 0xFF, 0xFF ) );

						apply_surface( 700, 0, fm2, screen );
						apply_surface( 700, 0, fm1, screen );

						SDL_FreeSurface( fm1 );
						SDL_FreeSurface( fm2 );
					}
				}

			}

		}

}

//MAP FUNCTIONS
SDL_Surface* flowers :: getSheet(){
	return FlowerSheet;
}

int* flowers :: getFrame(){
	return Frame;
}

SDL_Rect* flowers :: getOffset(){
	return Offsets;
}

SDL_Rect* flowers :: getCoords(){
	return Coords;
}

SDL_Rect* flowers :: getOriginalCoords(){
	return OriginalCoords;
}

bool* flowers :: Collected(){
	return isCollected;
}

bool flowers :: isFound(){
	return Found;
}

