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

[RequireComponent(typeof(AudioSource))]
public class GameController : MonoBehaviour {

	[System.Serializable]
	public class Period
	{
		public int numberOfBeats = 8;
		public AudioClip audioClipForStage;
		public Level[] levels;
	}

	[System.Serializable]
	public class Level
	{
		public int times = 1;
		public string[] scenes;
	}

	public int currentPeriod = 0;
	public int lives = 5;
	public float speed = 1.0f;
	public bool success = false;

	public Period[] periods;

	public AudioClip audioClipForStageStart;
	public AudioClip audioClipForSuccessCase;
	public AudioClip audioClipForFailureCase;

	public ThemeColoredSpriteManager colorManager;

//	private bool displayGUI = false;

	private int numberOfStagesInCurrentPeriod;
	private int currentStageInCurrentPeriod;

	public int NumberOfStagesInCurrentPeriod {
		get {
			return numberOfStagesInCurrentPeriod;
		}
	}

	public int CurrentStageInCurrentPeriod {
		get {
			return currentStageInCurrentPeriod;
		}
	}

	void Awake() {
		DontDestroyOnLoad(gameObject);
	}

	IEnumerator Start () {
		do {
			lives = 5;

			for (int i = 0; i < periods.Length; i++) {
				Period period = periods [i];

				numberOfStagesInCurrentPeriod = currentStageInCurrentPeriod = 0;
				for (int j = 0; j < period.levels.Length; j++) {
					numberOfStagesInCurrentPeriod += period.levels [j].times;
				}

				speed = 1.0f;
				for (int j = 0; j < period.levels.Length; j++) {
					Level level = period.levels [j];

					int times = level.times;
					do {
						Debug.Log ("Period: " + i + " Level: " + j + " Times remaining: " + times);
						Time.timeScale = speed;

						FindObjectOfType<GameStatusGUIManager> ().OpenStage ();

						AudioSource audioSource = GetComponent<AudioSource> ();
						audioSource.pitch = speed;

						audioSource.PlayOneShot (audioClipForStageStart);

						string sceneName = level.scenes [Random.Range (0, level.scenes.Length)];
						SceneManager.LoadScene (sceneName, LoadSceneMode.Additive);

						success = false;

						yield return new WaitForSeconds (60.0f / 130.0f * 4.0f);

						audioSource.loop = true;
						audioSource.clip = period.audioClipForStage;
						audioSource.Play ();

						yield return new WaitForSeconds (60.0f / 130.0f * period.numberOfBeats);
							
						if (!success) {
							lives--;
						} else {
							Debug.Log ("Stage clear");

							speed += 0.1f;

							times--;
							currentStageInCurrentPeriod++;
						}

						FindObjectOfType<GameStatusGUIManager> ().CloseStage ();

						audioSource.Stop ();
						audioSource.PlayOneShot (success ? audioClipForSuccessCase : audioClipForFailureCase);

						SceneManager.UnloadScene (sceneName);

						yield return new WaitForSeconds (60.0f / 130.0f * 4.0f);

						if (i == periods.Length - 1 && j == period.levels.Length - 1) {
							break;
						}

						if (lives == 0) {
							Debug.Log ("Game over");
							break;
						}
					} while (times > 0);

					if (lives == 0) {
						break;
					}
				}

				if (success) {
					// ライフ回復
					lives += 1;
				} else {
					if (lives == 0) {
						break;
					}
				}
			}

			Debug.Log ("Game clear");
		} while (success || lives == 0);

		SceneManager.LoadScene ("Ending");
	}

}
