 using UnityEngine;
using System.Collections;
using Util;

namespace Alcatraz
{
	public class MainController : MonoBehaviour
	{
		public bool isDebug = false;
		public float delayLoadTime = 1f;
		public string mainBGMString = "bgm";

		void Start () {
			SoundManager.GetInstance ().PlayBGM (mainBGMString);
		}

		void OnGameStart() {
			// exec when Game Start
			Debug.Log ("called OnGameStart()");
		}

		void OnTimeUp() {
			// exec when Time-Up
			Debug.Log ("called OnTimeUp()");
			EffectManager.GetInstance ().PlayEffectGameEnd ();
			Invoke ("GoToResult", 3f);
		}

		public void GoToResult() {
			StartCoroutine (WaitAndLoadResultScene());
		}

		IEnumerator  WaitAndLoadResultScene() { 
			// TODO: FadeOut before load next scene
			yield return new WaitForSeconds (delayLoadTime);
//			Application.LoadLevel ("Result");
			Application.LoadLevel ("Title");
		}

		void OnGUI () {
			if (isDebug) {
				if (GUILayout.Button("go to result scene")) {
					GameObject.FindWithTag("GameController").SendMessage("GoToResult");
				}
			}
		}

		void OnDestroy() {
			SoundManager.GetInstance ().StopBGM ();
		}
	}
}