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

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

MenuState MenuState::m_MenuState;

void MenuState::Init()
{
   //MAJ de variables
   currentMenu = 0;
   curColChoice = 1;
   day_top = RGB15(8, 26, 31);
   twilight_top = RGB15(31, 13, 4);
   night_top = RGB15(0, 12, 20);
   day_bot = RGB15(13, 31, 31);
   twilight_bot = RGB15(31, 18, 9);
   night_bot = RGB15(5, 16, 25);
   
   scrollBG = 0;
   alphaDay = 31*256;
   alphaFade = 0;
   alphaNight = alphaTwilight = 0;
   
   scrollBG = 0;
   buttonScale = 0;
   buttonSens = 1;
   scaleButton = true;
   isScaling = false;
   transScale = 4096;
   transScaleSpeed = 512;
   motionangle = 0;
   mp3Volume = 64;
   soundVolume = 64;
   
	//Creation des images
	ulSetTransparentColor(RGB15(31, 0, 31));
	title.assign(ulEFSLoadPNG("efs:/Menu_Title.png", UL_IN_VRAM, UL_PF_PAL4));
	button_new.assign(ulEFSLoadPNG("efs:/Menu_New.png", UL_IN_VRAM, UL_PF_PAL4));
	button_continue.assign(ulEFSLoadPNG("efs:/Menu_Continue.png", UL_IN_VRAM, UL_PF_PAL4));
	button_config.assign(ulEFSLoadPNG("efs:/Menu_Config.png", UL_IN_VRAM, UL_PF_PAL4));
	ulDisableTransparentColor();
	bgitm.assign(ulEFSLoadPNG("efs:/BG_ITM.png", UL_IN_VRAM, UL_PF_5550));
	
	//Positionnement des images
	title.glow(true, 3);
	title.setXY(256/2-128/2, 20);
	
	button_new.setXY(64, 192/2 - 32/2 + 16);
	button_continue.setXY(128, 192/2 - 32/2 + 16);
	button_config.setXY(192, 192/2 - 32/2 + 16);
	
	button_new.setRotCenter(32/2, 32/2);
	button_continue.setRotCenter(32/2, 32/2);
	button_config.setRotCenter(32/2, 32/2);
	
	//On charge la zic
	music = LoadMusic("efs:/sounds/OoO.mp3");
	AS_MP3DirectPlay(music.data, music.size);
	
	//On charge les effets sonores
	snd1 = LoadMusic("Menu_Switch.raw");
}

void MenuState::Cleanup()
{
   //Free images
   title.free();
   button_new.free();
   button_continue.free();
   button_config.free();
   bgitm.free();
   AS_MP3Stop();
   FreeMusic(music);
}

void MenuState::Pause()
{}

void MenuState::Resume()
{}

void MenuState::HandleEvents(StateManager* game)
{
   //Gestion des touches du menu
   if(isScaling == false)
   {
		if(ul_keys.pressed.right)
		{
			currentMenu++;
			buttonScale = 0;
			//button_new.scale(32, 32);
			AS_SoundDefaultPlay(sndMenu, 6912, soundVolume, 64, 0, 1);
		}
		if(ul_keys.pressed.left)
		{
		   currentMenu--;
		   buttonScale = 0;
		   //button_continue.scale(32, 32);
		   AS_SoundDefaultPlay(sndMenu, 6912, soundVolume, 64, 0, 1);
		}
	}	
	
	if(currentMenu < 0)
		currentMenu = 0;
	if(currentMenu > 2)
		currentMenu = 2;
	
	//Validation dans le menu
	if(currentMenu == 0 && ul_keys.pressed.A)
	{
		//Changement d'état bouton New
		button_new.scale(32, 32);
		isScaling = true;
		scaleButton = false;
	}
	if(currentMenu == 1 && ul_keys.pressed.A)
	{
		//Changement d'état bouton continue
		button_continue.scale(32, 32);
		isScaling = true;
		scaleButton = false;
	}
	//Validation dans le menu
	if(currentMenu == 2 && ul_keys.pressed.A)
	{
		//Changement d'état bouton New
		button_config.scale(32, 32);
		game->PushState(OptionState::Instance());
	}
	
	//Sortie du menu (new)
	if(alphaFade == 31)
	{
	   //Changement d'état vers le worldstate
	   if(currentMenu == 0)
	   {
			game->ChangeState(WorldState::Instance() );
			WorldState::Instance()->level = LEV_LEVEL1;
			WorldState::Instance()->Load();
			WorldState::Instance()->UnFadeWhite();
		}
		if(currentMenu == 1)
	   {
	      //On lit la valeur de la sauvegarde
	      FILE* fichier;
	      fichier = fopen("efs:/save/data.txt", "r");
	      int lev = 0;
	      if(fichier != NULL)
	      {
	      	fscanf(fichier, "%d", &lev);
				fclose(fichier);
	      }   
			game->ChangeState(WorldState::Instance() );
			WorldState::Instance()->level = lev;
			WorldState::Instance()->Load();
			WorldState::Instance()->UnFadeWhite();
		}			
		
		//Setting des paramètres du premier niveau
		//WorldState::Instance()->worldMan->SetLevel(LEV_LEVEL1);
		//Chargement du niveau
		//WorldState::Instance()->worldMan->LoadLevel();
	}	
	
	//gestion dsmotion
	/*motionangle += motion_rotation();	 */
}

void MenuState::Update(StateManager* game)
{
   //Changement de couleur
      //Day
	/*if(alphaDay < 31*256 && curColChoice == 0)
	{
	   alphaNight = alphaNight - 64;
   	alphaDay = alphaDay + 64;
 	}  	
   else if(alphaNight == 0 && curColChoice == 0)
   {
      alphaNight = 0;
      curColChoice = 1;
   }
   	//Twilight
   if(alphaTwilight < 31*256 && curColChoice == 1)
   {
   	alphaTwilight = alphaTwilight + 64;
 	}  	
   else if(!(alphaTwilight < 31*256) && curColChoice == 1)
   {
      alphaDay = 0;
      curColChoice = 2;
   }
   	//Night
	if(alphaNight < 31*256 && curColChoice == 2)
	{
   	alphaNight = alphaNight + 64;
	}   
   else if(!(alphaNight < 31*256) && curColChoice == 2)
   {
      alphaTwilight = 0;
      alphaDay = 0;
      curColChoice = 0;
   }*/
	
	//Changement du scaling selon le bouton selectionné
	if(scaleButton)
	{
		if(currentMenu == 0)
		{
			button_new.scale(32+buttonScale, 32+buttonScale);
			button_continue.scale(32, 32);
			button_config.scale(32, 32);
		}			
		if(currentMenu == 1)
		{
			button_continue.scale(32+buttonScale, 32+buttonScale);
			button_new.scale(32, 32);
			button_config.scale(32, 32);
		}			
		if(currentMenu == 2)
		{
			button_config.scale(32+buttonScale, 32+buttonScale);
			button_continue.scale(32, 32);
			button_new.scale(32, 32);
		}			
		
		if(buttonScale >= 12)
			buttonSens = -1;
		if(buttonScale <= 0)
			buttonSens = 1;
	}		
	
	buttonScale = buttonScale + buttonSens;
	
	
	//Scrolling du BG
	scrollBG -= 64;
	if(scrollBG < -FIX(512))
		scrollBG = 0;
	
	//Fade out
	if(isScaling)
	{
	   if(alphaFade < 31)
	   	alphaFade++;
	}
}

void MenuState::Draw(StateManager* game)
{  
   //Scaling de l'écran
	if(isScaling)
	{
	   ulMoveScreenView(64+16, 192/2-32/2+16);
		ulScaleScreenView(transScale, transScale);
		ulMoveScreenView(-(64+16), -(192/2-32/2+16));
		transScale = transScale + transScaleSpeed;
	}
	
	//Rotation de l'écran
	/*ulMoveScreenView(UL_SCREEN_WIDTH / 2, UL_SCREEN_HEIGHT / 2);
	ulRotateScreenView(motionangle);
	ulMoveScreenView(-UL_SCREEN_WIDTH / 2, -UL_SCREEN_HEIGHT / 2);*/

	
   //Affichage du background
   //Couleurs
   /*ulSetAlpha(UL_FX_ALPHA, alphaDay>>8, 0);
   ulDrawGradientRect(0, 0, UL_SCREEN_WIDTH, UL_SCREEN_HEIGHT, day_top, day_top, day_bot, day_bot);
   ulSetAlpha(UL_FX_DEFAULT, alphaDay>>8, 0);
   
   ulSetAlpha(UL_FX_ALPHA, alphaTwilight>>8, 0);
   ulDrawGradientRect(0, 0, UL_SCREEN_WIDTH, UL_SCREEN_HEIGHT, twilight_top, twilight_top, twilight_bot, twilight_bot);
   ulSetAlpha(UL_FX_DEFAULT, alphaTwilight>>8, 0);
   
   ulSetAlpha(UL_FX_ALPHA, alphaNight>>8, 0);
	ulDrawGradientRect(0, 0, UL_SCREEN_WIDTH, UL_SCREEN_HEIGHT, night_top, night_top, night_bot, night_bot);
	ulSetAlpha(UL_FX_DEFAULT, alphaNight>>8, 0);*/
	
	//BG
	bgitm.setXY(UNFIX(scrollBG),0);
	bgitm.draw();
	bgitm.setXY(UNFIX(scrollBG+FIX(512)),0);
	bgitm.draw();
	
   //Affichage des barres noires
   ulDrawFillRect(0, 0, 256, 16, RGB15(0, 0, 0));
   ulDrawFillRect(0, 192-16, 256, 192, RGB15(0, 0, 0));
   
   //Affichage des textes d'explication
   if(currentMenu == 0)
  		ulDrawString(60, 180, "Start a new adventure !");
  	if(currentMenu == 1)
  		ulDrawString(8, 180, "Continue your adventure in the machine !");
  	if(currentMenu == 2)
  		ulDrawString(65, 180, "Configure the game");
   
   //Affichage des menus
   button_new.draw();
   button_continue.draw();
   button_config.draw();
   
   //Affichage de l'écran titre
   title.draw();
   ulDrawString(100, 2, "Main menu");
  
   //Fade out
   if(isScaling)
   {
	   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);
	}
	
	//Debug funcs
	//ulPrintf_xy(1, 1, "Free Vram memory : %d ko", ulGetTexVramAvailMemory() / 1024);
   //ulPrintf_xy(155, 1, "GPU Load : %d %%", (GFX_VERTEX_RAM_USAGE * 100) / 6144);
}
