#include "effects.hpp"

effects :: effects(){

	//Sounds
	thunder1 = Mix_LoadWAV( "sounds/thunder1.wav" );
	thunder2 = Mix_LoadWAV( "sounds/thunder2.wav" );

	thunder1->volume = 60;
	thunder2->volume = 60;

	WichThunder = true;

	CroweSound = Mix_LoadWAV( "sounds/crowe.wav" );

	CroweSound->volume = 60;

	srand( time( NULL ) );

	BlankerDark = load_image( "effects/blanker_dark.png" );
	SDL_SetAlpha( BlankerDark, SDL_SRCALPHA, DARKNESS );

	BlankerDark2 = load_image( "effects/blanker_dark2.png" );
	SDL_SetAlpha( BlankerDark2, SDL_SRCALPHA, DARKNESS );

	BlankerDark3 = load_image( "effects/blanker_dark3.png" );
	SDL_SetAlpha( BlankerDark3, SDL_SRCALPHA, DARKNESS - 40 );

	//Thunder
	ThunderCounter = 0;

	//Shadow
	Shadow = load_image( "effects/shadow.png" );
	SDL_SetAlpha( Shadow, SDL_SRCALPHA, 50 );
	ShadowX = -100;

	//Crowe
	CroweSheet = load_image( "effects/crowe.png" );

	crowe_offsets[ 0 ].x = 0;
	crowe_offsets[ 0 ].y = 0;
	crowe_offsets[ 0 ].w = 32;
	crowe_offsets[ 0 ].h = 51;

	crowe_offsets[ 1 ].x = 32;
	crowe_offsets[ 1 ].y = 0;
	crowe_offsets[ 1 ].w = 32;
	crowe_offsets[ 1 ].h = 51;

	crowe_offsets[ 2 ].x = 64;
	crowe_offsets[ 2 ].y = 0;
	crowe_offsets[ 2 ].w = 32;
	crowe_offsets[ 2 ].h = 51;

	crowe_frame = 0;

	CroweCounter = 0;

	Crowe1X = Crowe2X = Crowe1Y = Crowe2Y = -50;

	//Fog
	Fog = load_image( "effects/fog.png" );
	SDL_SetAlpha( Fog, SDL_SRCALPHA, 40 );

	FogX[ 0 ] = 0;
	FogY[ 0 ] = 0;

	FogX[ 1 ] = 4500;
	FogY[ 1 ] = 500;

	FogX[ 2 ] = 7500;
	FogY[ 2 ] = 1500;

	FogX[ 3 ] = 2500;
	FogY[ 3 ] = 2500;

	FogX[ 4 ] = 6000;
	FogY[ 4 ] = 2500;

	FogX[ 5 ] = 3500;
	FogY[ 5 ] = 2700;

	FogX[ 6 ] = 500;
	FogY[ 6 ] = 4000;

	FogX[ 7 ] = 4000;
	FogY[ 7 ] = 4500;

	FogX[ 8 ] = 6000;
	FogY[ 8 ] = 3500;

	FogX[ 9 ] = 8500;
	FogY[ 9 ] = 4500;

	//BigFog

	BigFogH = load_image( "effects/bigfog_h.png" );
	BigFogV = load_image( "effects/bigfog_v.png" );
	BigFogCorner = load_image( "effects/bigfog_cornerUL.png" );
	BigFogCornerUR = load_image( "effects/bigfog_cornerUR.png" );
	BigFogCornerDR = load_image( "effects/bigfog_cornerBR.png" );
	BigFogCornerDL = load_image( "effects/bigfog_cornerBL.png" );
	BigFogHF = load_image( "effects/bigfog_h_f.png" );
	BigFogVF = load_image( "effects/bigfog_v_f.png" );

	XH = 0;
	YH = -600;

	XV = -800;
	YV = 0;

	XC = -800;
	YC = -600;


}

effects :: ~effects(){

	Mix_FreeChunk( thunder1 );
	Mix_FreeChunk( thunder2 );
	Mix_FreeChunk( CroweSound );

	SDL_FreeSurface( BlankerDark );
	SDL_FreeSurface( BlankerDark2 );
	SDL_FreeSurface( BlankerDark3 );
	SDL_FreeSurface( Shadow );
	SDL_FreeSurface( CroweSheet );
	SDL_FreeSurface( Fog );
	SDL_FreeSurface( BigFogH );
	SDL_FreeSurface( BigFogV );
	SDL_FreeSurface( BigFogCorner );
	SDL_FreeSurface( BigFogCornerUR );
	SDL_FreeSurface( BigFogCornerDR );
	SDL_FreeSurface( BigFogCornerDL );
	SDL_FreeSurface( BigFogHF );
	SDL_FreeSurface( BigFogVF );

}

void effects :: move( int xVel, int yVel, bool collision ){

	//Thunder
	thunder();

	//Shadow
	shadow();

	//Crowe
	crowe();

	//Fog
	fog( xVel, yVel, collision );

	//BigFog
	bigfog( xVel, yVel, collision );

}

void effects :: show( SDL_Surface* & screen, bool LightOn, bool ExtraLightOn ){

	//Apply Rain
	//show_rain( screen );

	//Apply Thunder
	show_thunder( screen );

	//Apply Shadow
	show_shadow( screen );

	//Apply Fog
	show_fog( screen );

	//Apply Crowes
	show_crowe( screen );

	//Apply Darkness
	apply_surface( 0, 0, ExtraLightOn ? BlankerDark3 : LightOn ? BlankerDark : BlankerDark2, screen );



}

void effects :: thunder(){

	ThunderCounter = rand() % 1000;

}

void effects :: show_thunder( SDL_Surface* & screen ){

	if( ThunderCounter % 50 == 0 ){
		if( rand() % 2 ){
			SDL_SetAlpha( BlankerDark, SDL_SRCALPHA, 0 );
			SDL_SetAlpha( BlankerDark2, SDL_SRCALPHA, 0 );
			SDL_SetAlpha( BlankerDark3, SDL_SRCALPHA, 0 );

			if( WichThunder ) Mix_PlayChannel( 0, thunder1, 0 );
			else Mix_PlayChannel( 1, thunder2, 0 );

			WichThunder = !WichThunder;
		}
	}
	else{
		SDL_SetAlpha( BlankerDark, SDL_SRCALPHA, DARKNESS );
		SDL_SetAlpha( BlankerDark2, SDL_SRCALPHA, DARKNESS );
		SDL_SetAlpha( BlankerDark3, SDL_SRCALPHA, DARKNESS - 40 );
	}

}

void effects :: shadow(){
	ShadowCounter = rand() % 5000;
}

void effects :: show_shadow( SDL_Surface* & screen ){

	if( ShadowCounter == 500 && ShadowX < 0 ){

		ShadowX = 800;
		ShadowY = rand() % 2 ? rand() % 50 + 200 : rand() % 50 + 340;

	}
	if( ShadowX > 0 && ShadowX <= 800 ){

		apply_surface( ShadowX, ShadowY, Shadow, screen );
		ShadowX -= 80;

	}

}

void effects :: crowe(){
	CroweCounter = rand() % 1000;
}

void effects :: show_crowe( SDL_Surface* & screen ){

	if( CroweCounter % 400 == 0 && Crowe1X < 0 ){

		Crowe1Y = rand() % 550;
		Crowe2Y = rand() % 550;

		Crowe2X = Crowe1X = 800;



		Mix_PlayChannel( 2, CroweSound, 0 );

	}
	if( Crowe1X > 0 && Crowe1X <= 800 ){

		Crowe1X -= 15;
		apply_surface( Crowe1X, Crowe1Y, CroweSheet, screen, &crowe_offsets[ crowe_frame / 2 ] );

	}
	if( Crowe2X > 0 && Crowe2X <= 800 ){

		Crowe2X -= 20;
		apply_surface( Crowe2X, Crowe2Y, CroweSheet, screen, &crowe_offsets[ crowe_frame / 2 ] );

	}

	crowe_frame++;

	if( crowe_frame > 5 ) crowe_frame = 0;

}

void effects :: fog( int xVel, int yVel, bool collision ){

	if( !collision )
	for( int i = 0; i < 10; i++ ){
		FogX[ i ] -= xVel;
		FogY[ i ] -= yVel;
		}

}

void effects :: show_fog( SDL_Surface* &screen ){

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

		if( FogX[ i ] < 800 && FogX[ i ] > -800 &&
			FogY[ i ] < 600 && FogY[ i ] > -600 )
		apply_surface( FogX[ i ], FogY[ i ], Fog, screen );

	}

}

void effects :: bigfog( int xVel, int yVel, bool collision ){

	if( !collision ){

		XC -= xVel;
		YC -= yVel;

		XH -= xVel;
		YH -= yVel;

		XV -= xVel;
		YV -= yVel;

		if( XH + BigFogH->w <= 0 ) XH += 800;
		if( XH >= 800 ) XH -= 800;

		if( YV + BigFogV->h <= 0 ) YV += 600;
		if( YV >= 600 ) YV -= 600;

	}

}

SDL_Surface* & effects :: getBigFogH(){
	return BigFogH;
}
SDL_Surface* & effects :: getBigFogV(){
	return BigFogV;
}
SDL_Surface* & effects :: getBigFogHF(){
	return BigFogHF;
}
SDL_Surface* & effects :: getBigFogVF(){
	return BigFogVF;
}
SDL_Surface* & effects :: getBigFogCorner(){
	return BigFogCorner;
}
SDL_Surface* & effects :: getBigFogCornerUR(){
	return BigFogCornerUR;
}
SDL_Surface* & effects :: getBigFogCornerDR(){
	return BigFogCornerDR;
}
SDL_Surface* & effects :: getBigFogCornerDL(){
	return BigFogCornerDL;
}
int& effects :: getXH(){
	return XH;
}
int& effects :: getYH(){
	return YH;
}
int& effects :: getXV(){
	return XV;
}
int& effects :: getYV(){
	return YV;
}
int& effects :: getXC(){
	return XC;
}
int& effects :: getYC(){
	return YC;
}

