using UnityEngine;
using System.Collections;

public enum Age {
	Child,
	Teen,
	Adult,
	Old
}

public class PlayerCont : MonoBehaviour {

	protected Animator anim;
	protected GameObject fadeScreen, nightMask, torchMask;
	protected GameController gc;
	protected Room room;
	public float speed = 3;
	protected Transform myTr;
	protected Rigidbody2D myRb;
	Item interactItem;
	bool canExit = false, canInteract = false, fading = false;
	public Age age = Age.Teen;
	public RoomName exitTo;
	public GameObject child, childDog;


	// Use this for initialization
	void Start () {
		fadeScreen = GameObject.Find("FadeScreen");
		gc = GameController.instance;
		room = FindObjectOfType<Room>();
		anim = GetComponentInChildren <Animator>();
		myTr = transform;
		myRb = rigidbody2D;
		if(gc.currentAge==Age.Teen){
			if(GameController.inventory.Contains(items.Slippers)){
				UseSlippers ();
			}
			nightMask = GameObject.Find ("nightmask");
			torchMask = GameObject.Find ("torchmask");
			if(GameController.inventory.Contains (items.Mobile)){
				nightMask.SetActive (false);
				torchMask.SetActive (true);
			} else{
				nightMask.SetActive (true);
				torchMask.SetActive (false);
			}
			if(room.lit){
				nightMask.SetActive (false);
				torchMask.SetActive (false);
			}
		}
		if(gc.currentAge == Age.Child){
			child = GameObject.Find ("Baby");
			childDog = GameObject.Find ("Childog");
			if(GameController.hasDog){
				child.SetActive (false);
				childDog.SetActive (true);
				anim = childDog.GetComponent <Animator>();
			}else{
				child.SetActive (true);
				childDog.SetActive (false);
				anim = child.GetComponent<Animator>();
			}
		}
	}
	
	// Update is called once per frame
	void Update () {
		Vector3 scale = Vector3.one;
		if(Input.GetButtonDown("Right")){
			anim.SetBool("walk",true);
			scale.x = 1;
			myTr.localScale = scale;
			myRb.velocity=Vector2.right*speed;
		} 
		if(Input.GetButtonDown("Left")){
			anim.SetBool("walk",true);
			scale.x = -1;
			myTr.localScale = scale;
			myRb.velocity=-Vector2.right*speed;
		}
		if((Input.GetButtonUp("Left") || Input.GetButtonUp("Right"))&& !(Input.GetButton("Left") ||Input.GetButton("Right")) ) {
			anim.SetBool("walk", false);
			myRb.velocity = Vector2.zero;
		}
		if(canExit && Input.GetButton("Interact")){
			StartCoroutine(LoadRoom (age.ToString ()+exitTo.ToString ()));
		}
		if(canInteract && Input.GetButton("Interact")){
			interactItem.SendMessage("Action");
		}

	}

	void SetExit(RoomName r){
		canExit = true;
		exitTo = r;
	}

	void UnsetExit(){
		canExit = false;
	}

	void SetInteractiveItem( Item item){
		canInteract = true;
		interactItem = item;
	}

	void UnsetInteractiveItem(){
		canInteract = false;
	}

	IEnumerator LoadRoom(string r){
		if(!fading){
			fading = true;
			fadeScreen.SendMessage ("FadeIn",1f);
			yield return new WaitForSeconds(1f);
			Application.LoadLevel(r);
		}
	}

	void GetTorch(){
		gc.SendMessage ("PickItem", items.Mobile);
		nightMask.SetActive (false);
		torchMask.SetActive (true);
	}

	void UseSlippers(){
		anim.SetFloat("boots",1);
	}

	void GetDog(){
		GameController.hasDog = true;
		child.SetActive (false);
		childDog.SetActive (true);
		anim = childDog.GetComponent<Animator>();
	}
}
