using UnityEngine;
using System.Collections;

public class GameStateManager : MonoBehaviour {

	public GlobalVars.GAME_STATE m_eGameState;
	public Transform m_eMenuPause, m_eGameOverScreen;
	public static GameStateManager m_Instance;
	public AudioSource m_MusicAudioSource;

	void Awake()
	{
		m_Instance = this;
	}

	// Use this for initialization
	void Start () {
	
		m_eGameState = GlobalVars.GAME_STATE.RUN ;
		m_eMenuPause.gameObject.SetActive(false);
		m_eGameOverScreen.gameObject.SetActive(false);
		
		

	}
	
	// Update is called once per frame
	void Update () {

		if( m_eGameState == GlobalVars.GAME_STATE.RUN )
		{
			if( Input.GetButtonDown("Start") || Input.GetKeyDown("return") )
			{
				SetStatePause();
				m_MusicAudioSource.Pause();
			}
		}
		else if( m_eGameState == GlobalVars.GAME_STATE.PAUSE )
		{
			if( Input.GetButtonDown("Start") || Input.GetKeyDown("return") )
			{
				ExitPause();
				m_MusicAudioSource.Play();
			}
		}
		else  if( m_eGameState == GlobalVars.GAME_STATE.GAME_OVER )
		{
			if(m_MusicAudioSource.isPlaying)
			{
				m_MusicAudioSource.Stop();
			}
			if( Input.GetButtonDown("Start")  || Input.GetKeyDown("return"))
			{
				MainBoard.ReinitializeRatios();

				CharacterManager.m_Instance.ActivateCharacterByIndex(0);

				m_MusicAudioSource.Play();
				Time.timeScale = 1;
				Application.LoadLevel( Application.loadedLevelName );
			}
		}
	
	}

	public void SetStatePause()
	{
		m_eGameState = GlobalVars.GAME_STATE.PAUSE;			
		Time.timeScale = 0;
		m_eMenuPause.gameObject.SetActive(true);
	}

	public void ExitPause()
	{
		m_eGameState = GlobalVars.GAME_STATE.RUN;			
		Time.timeScale = 1f;
		m_eMenuPause.gameObject.SetActive(false);
		
	}

	public void SetStateRun()
	{
		m_eGameState = GlobalVars.GAME_STATE.RUN;		
		Time.timeScale = 1f;
	}

	public void SetStateGameOver()
	{
		m_eGameState = GlobalVars.GAME_STATE.GAME_OVER;
		m_eGameOverScreen.gameObject.SetActive(true);

		//start play die sound of current caracter
		CharacterManager.m_Instance.PlayDieSound();

		Time.timeScale = 0f;
		
	}

}
