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

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

//GFX
EndLevelState EndLevelState::m_EndLevelState;

void EndLevelState::Init()
{
	//On décharge l'ancien niveau du worldman
	WorldState::Instance()->worldMan->FreeLevel();
	//On vide la musique de la mémoire
	music = LoadMusic("efs:/sounds/k-EndLevel.mp3");
	AS_MP3DirectPlay(music.data, music.size);
}

void EndLevelState::Cleanup()
{
   
}

void EndLevelState::Pause()
{
   
}

void EndLevelState::Resume()
{
   
}

void EndLevelState::HandleEvents(StateManager* game)
{
   if(ul_keys.pressed.start && WorldState::Instance()->worldMan->lastLevel == false)
   {				
		//On charge le nouveau niveau
		if(WorldState::Instance()->worldMan->curLevel->id == LEV_LEVEL1)
		{
			WorldState::Instance()->worldMan->SetLevel(LEV_LEVEL2);
			FILE* fichier;
	      fichier = fopen("efs:/save/data.txt", "w+");
	      int lev = 0;
	      if(fichier != NULL)
	      {
	      	fprintf(fichier, "%d", LEV_LEVEL2);
				fclose(fichier);
	      }  
		}			
		else if(WorldState::Instance()->worldMan->curLevel->id == LEV_LEVEL2)
		{
			WorldState::Instance()->worldMan->SetLevel(LEV_LEVEL3);
			FILE* fichier;
	      fichier = fopen("efs:/save/data.txt", "w+");
	      int lev = 0;
	      if(fichier != NULL)
	      {
	      	fprintf(fichier, "%d", LEV_LEVEL3);
				fclose(fichier);
	      } 
		}			
		else if(WorldState::Instance()->worldMan->curLevel->id == LEV_LEVEL3)
		{
			WorldState::Instance()->worldMan->SetLevel(LEV_LEVEL4);
			FILE* fichier;
	      fichier = fopen("efs:/save/data.txt", "w+");
	      int lev = 0;
	      if(fichier != NULL)
	      {
	      	fprintf(fichier, "%d", LEV_LEVEL4);
				fclose(fichier);
	      } 
		}
  
		
		//On décharge l'ancienne musique
		AS_MP3Stop();
		FreeMusic(music);	
		
		//On charge le nouveau niveau
		WorldState::Instance()->worldMan->LoadLevel();
		
		//On reaffiche le splash du worldstate
		WorldState::Instance()->Splash();
		WorldState::Instance()->UnFadeWhite();
		
		//On change d'état
		game->PopState();
   }
}

void EndLevelState::Update(StateManager* game)
{

}

void EndLevelState::Draw(StateManager* game)
{  	
   //Background Blanc
	ulDrawFillRect(0,0,256,192,RGB15(31,31,31) );
	ulSetTextColor(RGB15(0,0,0));
	
	//Affichage du texte
	if(WorldState::Instance()->worldMan->lastLevel == false)
		ulDrawString(40, 160, "Press Start to Continue...");
	else
		ulDrawString(40, 160, "The End ...");
	
	//On affiche les textes correspondants des interniveaux
	if(WorldState::Instance()->worldMan->curLevel->id == LEV_LEVEL1)
	{
		ulDrawTextBox(10, 30, 246, 150, "You killed Larva, the first Boss. But, before die, he ate you! You\'ll have to get out his body...", 0);
	}			
	else if(WorldState::Instance()->worldMan->curLevel->id == LEV_LEVEL2)
	{
		ulDrawTextBox(10, 30, 246, 150, "That was easy ? Let\'s continue !", 0);
	}			
	else if(WorldState::Instance()->worldMan->curLevel->id == LEV_LEVEL3)
	{
		ulDrawTextBox(10, 30, 246, 150, "You are near the exit, keep up the good work !", 0);
	}
	else if(WorldState::Instance()->worldMan->curLevel->id == LEV_LEVEL4)
	{
		ulDrawTextBox(10, 30, 246, 150, "Goob job, now you\'re Outside The Machine. This game was short and easy but I've made it in 6 months x) ...\n Credits :\nMain code : Morukutsu \nMusic : Me, Kirikool and Mikom\n Libs Used:µLib, Palib, EFS and ASlib\nMerci à tout les testeurs et à tout ceux qui m\'ont conseillé !", 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;
	}			
	
	//Affichage des barres noires
   ulDrawFillRect(0, 0, 256, 16, RGB15(0, 0, 0));
   ulDrawFillRect(0, 192-16, 256, 192, RGB15(0, 0, 0));
}

void EndLevelState::FadeWhite()
{
   isFadingWhite = true;
   fadeWhiteVal = 0;
}
   
void EndLevelState::UnFadeWhite()
{
   isUnFadingWhite = true;
   fadeUnWhiteVal = 31;
}
