/*****************************/
/* Inside The Machine - MKS  */
/*****************************/

//Includes
#include "StateManager.h"
#include "gamestate.h"
#include "WorldState.h"
#include "PauseState.h"
#include "EndLevelState.h"

//GFX


WorldState WorldState::m_WorldState;

void WorldState::Init()
{
	//MAJ de variables
	worldMan = new CWorldManager();
	//Transition depuis le menu
	alphaFade = 31;
	stopFade = false;
	//Splashscreen
	isSplashActive = true;
	splashTimer = 0;
	splashPosX = 0;
}

void WorldState::Load()
{
  	//Setting des paramètres du premier niveau
	worldMan->SetLevel(level);
   //Chargement du niveau
	worldMan->LoadLevel();
}

void WorldState::Cleanup()
{
}

void WorldState::Pause()
{}

void WorldState::Resume()
{}

void WorldState::HandleEvents(StateManager* game)
{
   //On vérifie si il faut mettre en pause
   if(ul_keys.pressed.start)
   {
   	//On change d'état
		game->PushState(PauseState::Instance() );
		AS_SetMP3Volume(mp3Volume/2);  
   }
   
   //Si on doit aller a la scène de fin de niveau
	if(worldMan->isBossKilled == true || worldMan->isGameFinished == true)
   {
   	//On change d'état
		game->PushState(EndLevelState::Instance() );
		//EndLevelState::Instance()->worldMan = worldMan;
		EndLevelState::Instance()->UnFadeWhite();
   } 
}

void WorldState::Update(StateManager* game)
{
   //Gestion du fading IN
   if(alphaFade > 1)
   {
   	alphaFade--;
   	if(alphaFade == 1)
   	{
   		stopFade = true;
   	}   
	}
	
	//Incrementation du timer
	splashTimer++;
	
	//Appel au worldman
	worldMan->HandleWorld();	
}

void WorldState::Draw(StateManager* game)
{  	
	//Appel au worldman
	worldMan->DrawWorld();
	
	//On affiche le splash
	if(isSplashActive)
	{
		ulDrawFillRect(splashPosX, 0, splashPosX + 256, 192, RGB15(0, 0, 0) );
		ulSetTextColor(RGB15(31,31,31));
		ulDrawString(50+splashPosX, 50, "Level...");
		ulDrawString(70+splashPosX, 60, worldMan->curLevel->name.c_str() );
	}			
	if(splashTimer > 160)
		splashPosX -= 2;  
	if(splashPosX < -256)
	{
		isSplashActive = false;
		worldMan->hero->isCanMove = true;
	}
	
	//Fade in
	/*if(!stopFade)
	{
	   ulSetAlpha(UL_FX_ALPHA, alphaFade, 0);
	   ulDrawFillRect(0, 0, UL_SCREEN_WIDTH, UL_SCREEN_HEIGHT, RGB15(31, 31, 31));
	   ulSetAlpha(UL_FX_DEFAULT, alphaFade, 0);
	}*/
	
	//Fading
	if(isFadingWhite)
	{
	   ulSetAlpha(UL_FX_ALPHA, fadeWhiteVal, 0);
	   ulDrawFillRect(0, 0, UL_SCREEN_WIDTH, UL_SCREEN_HEIGHT, RGB15(31, 31, 31));
	   ulSetAlpha(UL_FX_DEFAULT, 0, 0);
	   fadeWhiteVal++;
	   if(fadeWhiteVal >= 31)
	   	isFadingWhite = false;
	}
	
	if(isUnFadingWhite)
	{
	   ulSetAlpha(UL_FX_ALPHA, fadeUnWhiteVal, 0);
	   ulDrawFillRect(0, 0, UL_SCREEN_WIDTH, UL_SCREEN_HEIGHT, RGB15(31, 31, 31));
	   ulSetAlpha(UL_FX_DEFAULT, 0, 0);
	   fadeUnWhiteVal--;
	   if(fadeUnWhiteVal <= 0)
	   	isUnFadingWhite = false;
	}			
		 
	//Debug funcs
	/*ulPrintf_xy(1, 30, "Free Vram memory : %d ko", ulGetTexVramAvailMemory() / 1024);
   ulPrintf_xy(155, 30, "GPU Load : %d %%", (GFX_VERTEX_RAM_USAGE * 100) / 6144);*/
}

void WorldState::FadeWhite()
{
   isFadingWhite = true;
   fadeWhiteVal = 0;
}
   
void WorldState::UnFadeWhite()
{
   isUnFadingWhite = true;
   fadeUnWhiteVal = 31;
}
   
void WorldState::Splash()
{
   //Splashscreen
	isSplashActive = true;
	splashTimer = 0;
	splashPosX = 0;
}   
