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

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

//GFX
OptionState OptionState::m_OptionState;

void OptionState::Init()
{
	ulSetTransparentColor(RGB15(31, 0, 31));
	pointer.assign(ulEFSLoadPNG("efs:/Gui_Pointer.png", UL_IN_VRAM, UL_PF_PAL4));
	ulDisableTransparentColor();
	currentMenu = 0;
}

void OptionState::Cleanup()
{
   pointer.free();
}

void OptionState::Pause()
{
   
}

void OptionState::Resume()
{
   
}

void OptionState::HandleEvents(StateManager* game)
{
   //Gestion des menus
	if(ul_keys.pressed.down)
		currentMenu++;
	if(ul_keys.pressed.up)
		currentMenu--;	
	if(currentMenu < 0)
		currentMenu = 0;
	if(currentMenu >= 2)
		currentMenu = 2;
		
	if(currentMenu == 2 && ul_keys.pressed.A)
		game->PopState();
	
	if(currentMenu == 0)
	{
	 	if(ul_keys.held.right)
			mp3Volume++;
		if(ul_keys.held.left)  
			mp3Volume--;
		if(mp3Volume < 0)
			mp3Volume = 0;
		if(mp3Volume >= 128)
			mp3Volume = 128;
	}
	if(currentMenu == 1)
	{
	 	if(ul_keys.held.right)
			soundVolume++;
		if(ul_keys.held.left)  
			soundVolume--;
		if(soundVolume < 0)
			soundVolume = 0;
		if(soundVolume >= 128)
			soundVolume = 128;
	}   
}

void OptionState::Update(StateManager* game)
{
	AS_SetMP3Volume(mp3Volume);
	//AS_SetSoundVolume(soundVolume);
}

void OptionState::Draw(StateManager* game)
{  	
   //Background Blanc
	ulDrawFillRect(0,0,256,192,RGB15(5,25,30) );
	ulSetTextColor(RGB15(31,31,31));
	
	//Affichage des barres noires
   ulDrawFillRect(0, 0, 256, 16, RGB15(0, 0, 0));
   ulDrawFillRect(0, 192-16, 256, 192, RGB15(0, 0, 0));
   
   //Titre en haut du menu
   ulDrawString(100, 2, "Config menu");
   
   //Textes du menu
   ulSetTextColor(RGB15(0,0,0));
   ulDrawString(30, 50, "Music Volume");
   ulPrintf_xy(120, 50, "%d", mp3Volume);
   ulDrawString(30, 70, "SFX   Volume");
   ulPrintf_xy(120, 70, "%d", soundVolume);
   ulDrawString(30, 90, "Return to main menu");
   ulSetTextColor(RGB15(31,31,31));
   
   //Pointer
   pointer.setXY(20, 52+currentMenu*20);
   pointer.draw();
}

