using UnityEngine;
using System.Collections;

public class SpawnPoint : MonoBehaviour
{
		private GameObject spawned;
		private bool spawnedGood;
		public GameObject goodFoodPrefab;
		public GameObject evilFoodPrefab;
		public GameObject goodLightPrefab;
		public GameObject evilLightPrefab;
		public GameObject goodBenchPrefab;
		public GameObject evilBenchPrefab;
		public GameObject[] goodStatuePrefabs;
		public GameObject evilStatuePrefab;

		void Shuffle (GameObject[] statues)
		{
				GameObject tmp;
				int index = Mathf.FloorToInt (statues.Length * UnityEngine.Random.value);
				tmp = statues [index];
				statues [index] = statues [0];
				statues [0] = tmp;
				index = Mathf.FloorToInt (statues.Length * UnityEngine.Random.value);
				tmp = statues [index];
				statues [index] = statues [1];
				statues [1] = tmp;
				index = Mathf.FloorToInt (statues.Length * UnityEngine.Random.value);
				tmp = statues [index];
				statues [index] = statues [2];
				statues [2] = tmp;
		}

		public void Visit ()
		{
				GameObject pl = GameObject.FindGameObjectWithTag ("Player");
				
				((SpawnController)pl.GetComponent ("SpawnController")).SpawnPointVisited (this);
		}

		private void SpawnGeneric (GameObject goodPrefab, GameObject evilPrefab)
		{
				SpawnGeneric (goodPrefab, evilPrefab, UnityEngine.Random.value >= .2f);
		}

		private void SpawnGeneric (GameObject goodPrefab, GameObject evilPrefab, bool good)
		{
				GameObject go;
					
				if (good) {
						go = (GameObject)GameObject.Instantiate (goodPrefab);
				} else {
						go = (GameObject)GameObject.Instantiate (evilPrefab);
				}

				go.transform.position = transform.position;
				spawned = go;
				spawnedGood = good;
				go.SetActive (true);
				((Spawnable)(go.GetComponent<Spawnable> ())).spawner = this;
				
				Debug.Log ("Spawned: " + go);
		}

		public void Reset ()
		{
				spawned = null;
				spawnedGood = false;
		}

		public void StateChanged (Player pl)
		{
				bool hasStatue;
				bool wasGood;
				if (spawned == null)
						return;

				hasStatue = spawned.GetComponent<Spawnable> () is Statue;
				wasGood = spawnedGood;
				GameObject.Destroy (spawned);
				Reset ();

				switch (pl.MostImportantState) {
				case Player.States.Hungry:
						SpawnGeneric (goodFoodPrefab, evilFoodPrefab, wasGood);
						break;
				case Player.States.Scared:
						SpawnGeneric (goodLightPrefab, evilLightPrefab, wasGood);
						break;
				case Player.States.Sleepy:
						SpawnGeneric (goodBenchPrefab, evilBenchPrefab, wasGood);
						break;
				case Player.States.Base:
						if (hasStatue || UnityEngine.Random.value < .4f)
								SpawnGeneric (goodStatuePrefabs [pl.Goals], evilStatuePrefab, wasGood);
						break;
				}
		}

		public void Spawn (Player pl)
		{
				if (spawned != null)
						return;
		
				switch (pl.MostImportantState) {
				case Player.States.Hungry:
						SpawnGeneric (goodFoodPrefab, evilFoodPrefab);
						break;
				case Player.States.Scared:
						SpawnGeneric (goodLightPrefab, evilLightPrefab);
						break;
				case Player.States.Sleepy:
						SpawnGeneric (goodBenchPrefab, evilBenchPrefab);
						break;
				case Player.States.Base:
						if (UnityEngine.Random.value < .4f)
								SpawnGeneric (goodStatuePrefabs [pl.Goals], evilStatuePrefab);
						break;
				}
		}
}
