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

public enum items{
	//Child
	Ducky,
	Rattle,
	Flower,
	Remote,
	//Mobile here too

	//Teen
	Bone,
	Key,
	Mobile,
	Knife,
	Makeup,
	Vase,
	Boots,
	Slippers

	//Adult

	//Old
}

public class GameController : MonoBehaviour {

	public Age currentAge = Age.Child;
	Dictionary<Age,float> ageTimes = new Dictionary<Age, float>(4);
	Dictionary<Age,RoomName> ageStartRoom = new Dictionary<Age, RoomName>(4);
	public static Dictionary<Age,AudioClip> ageAudio = new Dictionary<Age, AudioClip>(4);
	public static Dictionary<items,Sprite> invImages = new Dictionary<items, Sprite>(12);
	float timer=999;
	public static float roomStart = -6;
	public static bool hasMakeup = false, hasSneaked = false, hasUnlockedTeenGardenDoor = false;
	public static bool hasDog = false, hasDogFood = false;
	public static bool brokenVase = false;
	AudioSource aSrc;
	public static List<items> inventory = new List<items>();
	Dictionary<items,GameObject> invIcons = new Dictionary<items,GameObject>();
	public static Font font;
	bool started = false;


	private static GameController _instance;
	public static GameController instance
	{
		get
		{
			if (!_instance)
			{
				GameObject container = new GameObject();
				container.name = "GameController";
				_instance = container.AddComponent<GameController>();
			}
			
			return _instance;
		}
	}

	void Awake () {
		DontDestroyOnLoad(gameObject);
		ageTimes.Add(Age.Child,5*60);
		ageTimes.Add(Age.Teen, 4*60);
		ageTimes.Add(Age.Adult, 3*60);
		ageTimes.Add(Age.Old, 2*60);
		ageStartRoom.Add(Age.Child, RoomName.LandingDn);
		ageStartRoom.Add(Age.Teen, RoomName.Bedroom);
		ageStartRoom.Add(Age.Adult, RoomName.Entrance);
		ageStartRoom.Add(Age.Old, RoomName.Bedroom);
		aSrc = gameObject.AddComponent<AudioSource>();
		aSrc.loop=true;
	}
		
	void OnLevelWasLoaded(){
		if(_instance!=this){
			Destroy (gameObject);
		}
		FillInventory();
	}

	void StartAge(Age age){
	
		started = true;

		currentAge = age;
		timer = ageTimes[age];
		aSrc.clip = ageAudio[currentAge];
		aSrc.volume = 1;
		aSrc.loop = true;
		aSrc.Play ();
		if(age==Age.Teen){
			roomStart=6;
		}
		Application.LoadLevel(currentAge.ToString ()+ageStartRoom[currentAge].ToString ());
	}

	void EndAge(Age age){
		started = false;
		inventory.Clear();
		invIcons.Clear ();
		switch (age){
		case Age.Child:
			Application.LoadLevel("EndedChild");
			break;

		case Age.Teen:
			Application.LoadLevel("EndedTeen");
			break;

		}

	}

	void WinAge(Age age){
		inventory.Clear();
		invIcons.Clear ();
		switch (age){
		case Age.Child:
			Application.LoadLevel("WonChild");
			break;
			
		case Age.Teen:
			Application.LoadLevel("WonTeen");
			break;
			
		}
		
	}

	void Update(){
		if(started){		
			timer-=Time.deltaTime;
			if(timer<=0){
				EndAge(currentAge);
			}
		}

		if(inventory.Contains(items.Ducky) &&
		   inventory.Contains(items.Rattle) &&
		   inventory.Contains(items.Flower)){
			WinAge (currentAge);
		}



		//print (timer);
	}

	void PickItem(items i){
		if(!inventory.Contains(i)){
			inventory.Add (i);
			invIcons.Add(i,new GameObject());
			SpriteRenderer rend = invIcons[i].AddComponent<SpriteRenderer>();
			rend.sprite = invImages[i];
			rend.sortingLayerName = "Inventory";
			invIcons[i].transform.localScale = 0.2f*Vector3.one;
			Vector3 topleft = Camera.main.ScreenToWorldPoint(new Vector3(0, Screen.height,0));
			invIcons[i].transform.position = new Vector2(topleft.x+(inventory.Count-1)*1.4f,topleft.y);
		}
	}

	void LoseItem(items i){
		if(inventory.Contains(i)){
			inventory.Remove(i);
			Destroy( invIcons[i]);
			invIcons.Remove(i);
		}
	}

	void EndGame(){

	}
	public void Penalty(float t){
		timer -= t;
	}
	void ClearTeenItems(){
		inventory.Clear ();
		invIcons.Clear ();
		hasMakeup = false;
		hasSneaked = false;
		hasUnlockedTeenGardenDoor = false;

	}
	void ClearChildItems(){
		inventory.Clear ();
		invIcons.Clear ();
		hasDog = false;
		hasDogFood=false;
	}

	void FillInventory(){
		invIcons.Clear ();
		Vector3 topleft = Camera.main.ScreenToWorldPoint(new Vector3(0, Screen.height,0));
		for (int i = 0 ;i<inventory.Count;i++){
			var j = inventory[i];
			invIcons.Add(j,new GameObject());
			SpriteRenderer rend = invIcons[j].AddComponent<SpriteRenderer>();
			rend.sprite = invImages[j];
			rend.sortingLayerName = "Inventory";
			invIcons[j].transform.localScale = 0.2f*Vector3.one;
			invIcons[j].transform.position = new Vector2(topleft.x+i*1.4f,topleft.y);
			
		}
	}
}
