#include "map.hpp"

map :: map(){

	BigFogCollision = false;

	end = false;
	housechange = false;

	Images[ 0 ] = load_image( "map/images/tree_normal.png" );
	Images[ 1 ] = load_image( "map/images/tree_normal2.png" );
	Images[ 2 ] = load_image( "map/images/tree2_normal.png" );
	Images[ 3 ] = load_image( "map/images/tree_scary.png" );
	Images[ 4 ] = load_image( "map/images/tree_crowe.png" );
	Images[ 5 ] = load_image( "map/images/tree_deadman.png" );
	Images[ 6 ] = load_image( "map/images/tree2_leaves.png" );
	Images[ 7 ] = load_image( "map/images/tree2_leaves2.png" );
	Images[ 8 ] = load_image( "map/images/tree2_scary.png" );
	Images[ 9 ] = load_image( "map/images/bush.png" );
	Images[ 10 ] = load_image( "map/images/bush2.png" );
	Images[ 11 ] = load_image( "map/images/bush3.png" );
	Images[ 12 ] = load_image( "map/images/ground.jpg" );
	Images[ 13 ] = load_image( "map/images/house.png" );

	LevelCounter = 0;

	GroundX = GroundY = 0;

	loadlevel( LevelCounter );

}

map :: ~map(){

	for( int i = 0; i < 14; i++ ){
		SDL_FreeSurface( Images[ i ] );
	}

}

void map :: loadlevel( int LevelCounter ){

	std::ifstream load( "map/map1.dat" );

		for( int i = 0; i < 50; i++ ){
			for( int j = 0; j < 50; j++ ){
				load >> ObjectMap[ LevelCounter ][ i ][ j ];
			}
		}

	load.close();

	for( int i = 0; i < 50; i++ ){
		for( int j = 0; j < 50; j++ ){


			ObjectCoords[ LevelCounter ][ i ][ j ].x = j * 200 + ( i % 2 ? 25 : -25 );

			ObjectCoords[ LevelCounter ][ i ][ j ].y = i * 120;

		}
	}

	shift_boxes();

}

void map :: show( SDL_Surface* & screen, SDL_Surface* RSpriteSheet, SDL_Rect Frame, SDL_Surface* FlowerSheet, SDL_Rect* FlowerOffsets, SDL_Rect* FlowerCoords, int* FlowerFrames, SDL_Surface* CandleSheet, SDL_Rect* Coords, bool* isOut, SDL_Rect* Offsets, int & CFrame, SDL_Surface* AppleSheet, bool* appleIsOut, SDL_Rect* AppleCoords,
				  SDL_Surface* & BigFogH, SDL_Surface* & BigFogV, SDL_Surface* & BigFogCorner, int XH, int YH, int XV, int YV, int XC, int YC, SDL_Surface* & BigFogCornerUR, SDL_Surface* & BigFogCornerDR, SDL_Surface* & BigFogCornerDL, SDL_Surface* & BigFogHF, SDL_Surface* & BigFogVF ){

	//Applying ground
	if( GroundX <= -800 || GroundX >= 800 ) GroundX = 0;
	if( GroundY <= -600 || GroundY >= 600 ) GroundY = 0;

	//center
	apply_surface( GroundX, GroundY, Images[ ground ], screen );
	//right
	apply_surface( GroundX + 800, GroundY, Images[ ground ], screen );
	//left
	apply_surface( GroundX - 800, GroundY, Images[ ground ], screen );
	//right-up
	apply_surface( GroundX + 800, GroundY - 600, Images[ ground ], screen );
	//right-down
	apply_surface( GroundX + 800, GroundY + 600, Images[ ground ], screen );
	//left-down
	apply_surface( GroundX - 800, GroundY + 600, Images[ ground ], screen );
	//left-up
	apply_surface( GroundX - 800, GroundY - 600, Images[ ground ], screen );
	//up
	apply_surface( GroundX, GroundY - 600, Images[ ground ], screen );
	//down
	apply_surface( GroundX, GroundY + 600, Images[ ground ], screen );


	bool RHShown = false;

	for( int i = 0; i < 6; i++ ){
		if( FlowerCoords[ i ].x < 800 && FlowerCoords[ i ].x > - 60 &&
			FlowerCoords[ i ].y < 600 && FlowerCoords[ i ].y > - 60 ){

			if( FlowerCoords[ i ].y < 275 )
				apply_surface( FlowerCoords[ i ].x, FlowerCoords[ i ].y, FlowerSheet, screen, &FlowerOffsets[ FlowerFrames[ i ] ] );

		}
	}

	CFrame++;
	if( CFrame >= 3 ) CFrame = 0;

	for( int i = 0; i < 52; i++ ){
		if( Coords[ i ].x > - 21 && Coords[ i ].x < 800 &&
			Coords[ i ].y > - 61 && Coords[ i ].y < 600 && isOut[ i ] ){

			if( Coords[ i ].y < 315 )
			apply_surface( Coords[ i ].x, Coords[ i ].y, CandleSheet, screen, &Offsets[ CFrame ] );
		}
	}

	for( int i = 0; i < 31; i++ ){
		if( AppleCoords[ i ].x > - 21 && AppleCoords[ i ].x < 800 &&
			AppleCoords[ i ].y > - 61 && AppleCoords[ i ].y < 600 && appleIsOut[ i ] ){

			if( AppleCoords[ i ].y < 315 )
			apply_surface( AppleCoords[ i ].x, AppleCoords[ i ].y, AppleSheet, screen );
		}


	}

	//BIGFOG
	int WWidth = 11200;
	int WHeight = 7200;

	//Horizontal
	if( XH <= 800 && XH + BigFogH->w >= 0 &&
		YH <= 600 && YH + BigFogH->h >= 0 ){

			apply_surface( XH, YH, BigFogH, screen );

			if( XH + BigFogH->w <= 800 )
				apply_surface( XH + BigFogH->w, YH, BigFogH, screen );

			if( XH >= 0 )
				apply_surface( XH - BigFogH->w, YH, BigFogH, screen );

	}
	if( XH <= 800 && XH + BigFogH->w >= 0 &&
		YH + WHeight <= 600 && YH + BigFogH->h + WHeight >= 0 ){

			apply_surface( XH, YH + WHeight, BigFogHF, screen );

			if( XH + BigFogH->w <= 800 )
				apply_surface( XH + BigFogH->w, YH + WHeight, BigFogHF, screen );

			if( XH >= 0 )
				apply_surface( XH - BigFogH->w, YH + WHeight, BigFogHF, screen );

	}

	//Vertical
	if( XV <= 800 && XV + BigFogV->w >= 0 &&
		YV <= 600 && YV + BigFogV->h >= 0 ){

			apply_surface( XV, YV, BigFogV, screen );

			if( YV + BigFogV->h <= 600 )
				apply_surface( XV, YV + BigFogV->h, BigFogV, screen );

			if( YV >= 0 )
				apply_surface( XV, YV - BigFogV->h, BigFogV, screen );

	}
	if( XV + WWidth <= 800 && XV + BigFogV->w + WWidth >= 0 &&
		YV <= 600 && YV + BigFogV->h >= 0 ){

			apply_surface( XV + WWidth, YV, BigFogVF, screen );

			if( YV + BigFogV->h <= 600 )
				apply_surface( XV + WWidth, YV + BigFogV->h, BigFogVF, screen );

			if( YV >= 0 )
				apply_surface( XV + WWidth, YV - BigFogV->h, BigFogVF, screen );

	}
	//Corner
	//UL


	if( XC <= 800 && XC + BigFogCorner->w >= 0 &&
		YC <= 600 && YC + BigFogCorner->h >= 0 )

			apply_surface( XC, YC, BigFogCorner, screen );


	//UR
	if( XC + WWidth <= 800 && XC + BigFogCorner->w + WWidth >= 0 &&
		YC <= 600 && YC + BigFogCorner->h >= 0 ){

		apply_surface( XC + WWidth, YC, BigFogCornerUR, screen );

	}

	//DL
	if( XC <= 800 && XC + BigFogCorner->w >= 0 &&
		YC + WHeight <= 600 && YC + BigFogCorner->h + WHeight >= 0 ){

		apply_surface( XC, YC + WHeight, BigFogCornerDL, screen );

	}

	//DR
	if( XC + WWidth <= 800 && XC + BigFogCorner->w + WWidth >= 0 &&
		YC + WHeight <= 600 && YC + BigFogCorner->h + WHeight >= 0 ){

		apply_surface( XC + WWidth, YC + WHeight, BigFogCornerDR, screen );

	}

	for( int i = 0; i < 50; i++ )
		for( int j = 0; j < 50; j++ ){

			if( ObjectCoords[ LevelCounter ][ i ][ j ].x >= -1000 &&
				ObjectCoords[ LevelCounter ][ i ][ j ].x <= 800 &&
				ObjectCoords[ LevelCounter ][ i ][ j ].y >= -1000 &&
				ObjectCoords[ LevelCounter ][ i ][ j ].y <= 600 &&
				ObjectMap[ LevelCounter ][ i ][ j ] != 99 )
			{

				if( RHShown == false && ( ObjectCoords[ LevelCounter ][ i ][ j ].y + Images[ ObjectMap[ LevelCounter ][ i ][ j ] ]->h ) > 300 + Frame.h ){
					apply_surface( 400, 300, RSpriteSheet, screen, &Frame );
					RHShown = true;
				}

				if( 400 > ObjectCoords[ LevelCounter ][ i ][ j ].x + 30 && 430 < ObjectCoords[ LevelCounter ][ i ][ j ].x - 30 + Images[ ObjectMap[ LevelCounter ][ i ][ j ] ]->w &&
					300 > ObjectCoords[ LevelCounter ][ i ][ j ].y + 50 && 344 < ObjectCoords[ LevelCounter ][ i ][ j ].y - 5 + Images[ ObjectMap[ LevelCounter ][ i ][ j ] ]->h )
						SDL_SetAlpha( Images[ ObjectMap[ LevelCounter ][ i ][ j ] ], SDL_SRCALPHA, 175 );

				apply_surface( ObjectCoords[ LevelCounter ][ i ][ j ].x,
						       ObjectCoords[ LevelCounter ][ i ][ j ].y,
						       Images[ ObjectMap[ LevelCounter ][ i ][ j ] ],
						       screen );

				if( 400 > ObjectCoords[ LevelCounter ][ i ][ j ].x + 30 && 430 < ObjectCoords[ LevelCounter ][ i ][ j ].x - 30 + Images[ ObjectMap[ LevelCounter ][ i ][ j ] ]->w &&
					300 > ObjectCoords[ LevelCounter ][ i ][ j ].y + 50 && 344 < ObjectCoords[ LevelCounter ][ i ][ j ].y - 5 + Images[ ObjectMap[ LevelCounter ][ i ][ j ] ]->h )
						SDL_SetAlpha( Images[ ObjectMap[ LevelCounter ][ i ][ j ] ], SDL_SRCALPHA, 255 );


			}

		}

		if( RHShown == false ){
			apply_surface( 400, 300, RSpriteSheet, screen, &Frame );
		}

		for( int i = 0; i < 6; i++ ){
			if( FlowerCoords[ i ].x < 800 && FlowerCoords[ i ].x > - 60 &&
				FlowerCoords[ i ].y < 600 && FlowerCoords[ i ].y > - 60 ){

				if( FlowerCoords[ i ].y >= 275 )
					apply_surface( FlowerCoords[ i ].x, FlowerCoords[ i ].y, FlowerSheet, screen, &FlowerOffsets[ FlowerFrames[ i ] ] );

			}
		}

		for( int i = 0; i < 52; i++ ){
			if( Coords[ i ].x > - 21 && Coords[ i ].x < 800 &&
				Coords[ i ].y > - 61 && Coords[ i ].y < 600 && isOut[ i ] ){

				if( Coords[ i ].y >= 315 )
				apply_surface( Coords[ i ].x, Coords[ i ].y, CandleSheet, screen, &Offsets[ CFrame ] );
			}
		}

		for( int i = 0; i < 31; i++ ){
			if( AppleCoords[ i ].x > - 21 && AppleCoords[ i ].x < 800 &&
				AppleCoords[ i ].y > - 61 && AppleCoords[ i ].y < 600 && appleIsOut[ i ] ){

				if( AppleCoords[ i ].y >= 315 )
				apply_surface( AppleCoords[ i ].x, AppleCoords[ i ].y, AppleSheet, screen );
			}


		}

}

void map :: move( int xVel, int yVel, int& Flowers ){

	GroundX -= xVel;
	GroundY -= yVel;

	SDL_Rect RHHitbox;

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

	bool collision = false;
	Collision = false;

	if( Flowers == 6 && housechange == false ){
		SDL_FreeSurface( Images[ 13 ] );
		Images[ 13 ] = load_image( "map/images/house2.png" );
	}


	for( int i = 0; i < 50; i++ )
			for( int j = 0; j < 50; j++ ){
				ObjectCoords[ LevelCounter ][ i ][ j ].x -= xVel;
				ObjectCoords[ LevelCounter ][ i ][ j ].y -= yVel;
				ObjectHitbox[ LevelCounter ][ i ][ j ].x -= xVel;
				ObjectHitbox[ LevelCounter ][ i ][ j ].y -= yVel;

				if( check_collision( RHHitbox, ObjectHitbox[ LevelCounter ][ i ][ j ] ) ){
					collision = true;

					if( ObjectMap[ LevelCounter ][ i ][ j ] == 13 && Flowers == 6 ){
						end = true;
					}

				}
			}

	//Collision by Bigfog
	/*	//BIGFOG
	int WWidth = 11200;
	int WHeight = 7200;
	 *
	 */
	if( ObjectCoords[ LevelCounter ][ 0 ][ 0 ].x > 580 ||
		ObjectCoords[ LevelCounter ][ 0 ][ 0 ].y > 380 ||
		ObjectCoords[ LevelCounter ][ 0 ][ 0 ].x < -10150 ||
		ObjectCoords[ LevelCounter ][ 0 ][ 0 ].y < -6320 ){
		collision = true;
		BigFogCollision = true;
	}

	if( collision ){

		Collision = true;

		GroundX += xVel;
		GroundY += yVel;

		for( int i = 0; i < 50; i++ )
				for( int j = 0; j < 50; j++ ){
					ObjectCoords[ LevelCounter ][ i ][ j ].x += xVel;
					ObjectCoords[ LevelCounter ][ i ][ j ].y += yVel;
					ObjectHitbox[ LevelCounter ][ i ][ j ].x += xVel;
					ObjectHitbox[ LevelCounter ][ i ][ j ].y += yVel;
				}
	}

}

void map :: shift_boxes(){

	for( int i = 0; i < 50; i++ ){
		for( int j = 0; j < 50; j++ ){

			if( ObjectMap[ LevelCounter ][ i ][ j ] <= 8 ){

				ObjectHitbox[ LevelCounter ][ i ][ j ].x = ObjectCoords[ LevelCounter ][ i ][ j ].x + 50;
				ObjectHitbox[ LevelCounter ][ i ][ j ].y = ObjectCoords[ LevelCounter ][ i ][ j ].y + 295;
				ObjectHitbox[ LevelCounter ][ i ][ j ].w = 100;
				ObjectHitbox[ LevelCounter ][ i ][ j ].h = 13;

			}

			if( ObjectMap[ LevelCounter ][ i ][ j ] == 13 ){

				ObjectHitbox[ LevelCounter ][ i ][ j ].x = ObjectCoords[ LevelCounter ][ i ][ j ].x;
				ObjectHitbox[ LevelCounter ][ i ][ j ].y = ObjectCoords[ LevelCounter ][ i ][ j ].y + 295;
				ObjectHitbox[ LevelCounter ][ i ][ j ].w = Images[ 13 ]->w;
				ObjectHitbox[ LevelCounter ][ i ][ j ].h = 13;

			}

		}
	}

}

int map :: getX(){
	return ObjectCoords[ LevelCounter ][ 0 ][ 0 ].x;
}
int map :: getY(){
	return ObjectCoords[ LevelCounter ][ 0 ][ 0 ].y;
}

bool map :: isCollision(){
	return Collision;
}

bool map :: isEnd(){
	return end;
}

bool map :: isBigFogCollision(){
	return BigFogCollision;
}
