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

//Includes
#include "StateManager.h"
#include "gamestate.h"
#include "SplashState.h"
#include "MenuState.h"

//GFX


SplashState SplashState::m_SplashState;

void SplashState::Init()
{
	//MAJ de variables
	//Transition depuis le menu
	alphaFade = 31;
	stopFade = false;
	timer = 0;
	
	//Creation des images
	ulSetTransparentColor(RGB15(31, 0, 31));
	spr1.assign(ulEFSLoadPNG("efs:/hbb.png", UL_IN_VRAM, UL_PF_5550));
	spr2.assign(ulEFSLoadPNG("efs:/mkprod.png", UL_IN_VRAM, UL_PF_PAL8));
	ulDisableTransparentColor();
}


void SplashState::Cleanup()
{
   spr1.free();
   spr2.free();
}

void SplashState::Pause()
{}

void SplashState::Resume()
{}

void SplashState::HandleEvents(StateManager* game)
{
  
}

void SplashState::Update(StateManager* game)
{
   //Gestion du fading IN
   if(alphaFade > 1)
   {
   	alphaFade--;
   	if(alphaFade == 1)
   	{
   		stopFade = true;
   	}   
	}
	
	//Timersplash
	timer++;
}

void SplashState::Draw(StateManager* game)
{  
   if(timer == 1)
   	UnFadeWhite();
   if(timer < 330)
		spr1.draw();
	if(timer == 300)
		FadeWhite();
	if(timer == 330)
		UnFadeWhite();
	if(timer > 330 && timer < 430)
		spr2.draw();
	if(timer == 400)
		FadeWhite();
	if(timer == 430)
		game->ChangeState(MenuState::Instance() );	
			
	//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;
	}			
		
}

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