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

public class Communicator : MonoBehaviour {

	private BackgroundMaker bgMake;
	private bool[] gamesReady;
	// game index, step, colour
	public bool[,,] colorsActive;
	public bool[,] turtleActive;

	public Transform titleGPrefab;
	public Transform titleNPrefab;

	public Transform[] tutGPrefab;
	public Transform[] tutNPrefab;

	private List<Transform> transforms;

	public bool dead;

	public Main[] games;

	private bool won = false;
	public bool tutorialing = true;

	private int state = 0;

	// Use this for initialization
	void Start () {
		gamesReady		= new bool[2];
		colorsActive	= new bool[2, 3, 4];
		turtleActive	= new bool[2, 3]; 

		games = gameObject.GetComponentsInChildren<Main>();
		bgMake = gameObject.GetComponentInChildren<BackgroundMaker>();
		transforms = new List<Transform>();
		LoadTitle();
	}

	void LoadTitle() {
		Transform trfm;
		trfm = (Transform)GameObject.Instantiate(titleGPrefab, new Vector3(-10.7f,1,0.1f), Quaternion.Euler(90,0,0));
		transforms.Add(trfm);
		trfm = (Transform)GameObject.Instantiate(titleNPrefab, new Vector3(10.7f, 1,0.1f), Quaternion.Euler(90,0,0));
		transforms.Add(trfm);
	}

	void ClearTransforms() {
		foreach (Transform trfm in transforms) {
			GameObject.Destroy(trfm.gameObject);
		}
		transforms.Clear();
	}

	void ShowTutorial(int index) {
		Transform trfm;
		trfm = (Transform)GameObject.Instantiate(tutGPrefab[index], new Vector3(-10.7f,1.3f,-1), Quaternion.Euler(90,0,0));
		trfm.localScale = new Vector3(1.5f, 1.5f, 1.5f);
		transforms.Add(trfm);
		trfm = (Transform)GameObject.Instantiate(tutNPrefab[index], new Vector3(10.7f, 1.3f,-1), Quaternion.Euler(90,0,0));
		trfm.localScale = new Vector3(1.5f, 1.5f, 1.5f);
		transforms.Add(trfm);
	}

	public void StartGame() {
		bgMake.Load(0);
		foreach (Main main in games) {
			main.loadLevel();
		}
	}

	public void SetGameReady(int gameIndex) {
		gamesReady[gameIndex] = true;
	}
	
	public void SetGameNotReady(int gameIndex) {
		gamesReady[gameIndex] = false;
	}

	public void NextLevel() {
		int level = 0;
		foreach (Main main in games) {
			if (main.level < Main.numLevels - 1) {
				main.level ++;
				level = main.level;
			}
			main.reset();
			bgMake.Load(level);
		}
	}

	//public void Die() {
	//	dead = true;
	//	gamesReady[0] = false;
	//	gamesReady[1] = false;
	//}
	
	// Update is called once per frame
	void Update () {
		switch (state) {
		case 0:
			if (Input.GetButtonDown("Go")) {
				ClearTransforms();
				StartGame();
				ShowTutorial(0);
				state ++;
			}
			break;
		case 1:
			if (Input.GetButtonDown("Go")) {
				ClearTransforms();
				ShowTutorial(0);
				state ++;
			}
			break;
		case 2:
			if (Input.GetButtonDown("Go")) {
				ClearTransforms();
				ShowTutorial(1);
				state ++;
			}
			break;
		case 3:
			if (Input.GetButtonDown("Go")) {
				ClearTransforms();
				ShowTutorial(2);
				state ++;
			}
			break;
		case 4:
			if (Input.GetButtonDown("Go")) {
				ClearTransforms();
				ShowTutorial(3);
				state ++;
			}
			break;
		case 5:
			if (Input.GetButtonDown("Go")) {
				ClearTransforms();
				tutorialing = false;
				state ++;
			}
			break;
		case 6:
			if (won) {
				if (Input.GetButtonDown("Go")) {
					won = false;
					foreach (Main main in games) {
						main.atEnd = false;
					}
					NextLevel();
				}
				return;
			}
			dead = false;
			if (Input.GetButtonDown("ResetAll")) {
				foreach (Main main in games) {
					main.reset();
				}
				return;
			}
			if (gamesReady[0] && gamesReady[1] && Input.GetButtonDown("Go")) {
				foreach (Main main in games) {
					main.startPlayback();
				}
				gamesReady[0] = false;
				gamesReady[1] = false;
			}
			
			bool atEnd = true;
			foreach (Main main in games) {
				main.DoLogic();
				atEnd &= main.atEnd;
			}
			
			if (dead) {
				foreach (Main main in games) {
					main.die();
				}
				dead = false;
				return;
			}
			if (atEnd) {
				foreach (Main main in games) {
					main.addVictory();
				}
				won = true;
			}
			break;
		}

	}
}
