using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;

public class StageBeatEvent : MonoBehaviour {

	[System.Serializable]
	public class SuccessAction {
		public AnimatorManager am;
		public string anim;
	}

	public SuccessAction[] successActions;
	public bool eventIsFailure;
	public bool isTutorial;

	public void BeatStage() {

		foreach(SuccessAction a in successActions) {
			a.am.Play(a.anim);
		}
		if( isTutorial ) {
			Invoke("_GotoMain", 3.0f);
			return;
		} 

		GameController[] gcs = FindObjectsOfType<GameController>();
		if( gcs.Length > 1 ) {
			Debug.LogError("[Fatal] GameController exists more than 1!!");
		} else if(gcs.Length == 1) {
			if( !eventIsFailure ) {
				gcs[0].success = true;
			}
		} else {
			Scene current = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
			if(eventIsFailure) {
				Debug.Log("Stage " + current.name + " Failed");
			} else {
				Debug.Log("Stage " + current.name + " Clear");
			}

			Invoke("DoAgain", 2.0f);
		}
	}

	public void DoAgain() {
		Scene current = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
		Debug.Log("Playing " + current.name + " Again...");
		SceneManager.LoadScene(current.name);
	}
	void _GotoMain() {
		UnityEngine.SceneManagement.SceneManager.LoadScene("Main");
	}
}
