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

public class scrPlayer : MonoBehaviour {

	public AudioClip acBreak;
	public AudioClip[] acStartled;
	public AudioClip[] acMeow;
	public AudioClip[] acTaskSFX;
	public string strHorMove;
	public string strVerMove;
	public string strMeowKey;
	public string strActionKey;
	public Human scrHuman;
	public Human scrHuman2;
	public scrPlayer scrOtherPlayer;
	public Transform trPrompt;
	public Transform[] trRooms;
	public GameObject goBird;
	public GameObject goVase;
	public GameObject goYarn;
	public GameObject goBox;

	string strCurrentRoom;
	string strPreviousRoom;

	Transform tr;
	Rigidbody2D rb;
	Animator ator;
	Text textPrompt;
	float flMoveSpeed = 15000.0f;
	bool isDefaultState = true;
	GameObject goDisable;
	Transform trCurrentTask; //The task that this player is currently performing

	[System.NonSerialized] public List<Transform> liTasks = new List<Transform>(); //A list of Tasks that are close enough to this player to be activated by him/her
	[System.NonSerialized] public List<Transform> liCompletedTasks = new List<Transform>(); //A list of Tasks that have already been completed by this player

	Dictionary<string, AudioClip> dictTaskSFX = new Dictionary<string, AudioClip>();

	void Awake () {
		
		tr = transform;
		rb = GetComponent<Rigidbody2D>();
		ator = GetComponent<Animator>();
		textPrompt = trPrompt.GetComponent<Text>();

		dictTaskSFX.Add("PISS", acTaskSFX[0]);
		dictTaskSFX.Add("SCRATCH", acTaskSFX[1]);
		dictTaskSFX.Add("BOX", acTaskSFX[2]);
		dictTaskSFX.Add("PLAY", acTaskSFX[3]);
		dictTaskSFX.Add("BREAK", acTaskSFX[4]);
		dictTaskSFX.Add("SIT", acTaskSFX[5]);
		dictTaskSFX.Add("CATCH", acTaskSFX[6]);
		dictTaskSFX.Add("BEG", acTaskSFX[7]);
		dictTaskSFX.Add("STARE", acTaskSFX[8]);

	}

	void Update() {

		if ( Input.GetKey(KeyCode.Z) && Input.GetKey(KeyCode.P) ) {

			Application.LoadLevel("startMenu");

		}

		trPrompt.position = tr.position -Vector3.up*75;

		if ( !isDefaultState && trCurrentTask != tr ) {
			
			if (strCurrentRoom == scrHuman.strCurrentRoom || strCurrentRoom == scrHuman2.strCurrentRoom) {
				
				scrSoundManager.instance.doPlaySFX(acStartled[Random.Range(0,acStartled.Length)]);
				tr.FindChild("startled").GetComponent<Animator>().SetTrigger("STARTLE");
				doCancelTask();

			}

		}

		#region Set Prompt text
		if ( scrHuman.strCurrentRoom == strCurrentRoom || scrHuman2.strCurrentRoom == strCurrentRoom ) {

			textPrompt.color = Color.red;

		} else {

			textPrompt.color = Color.white;

		}

		if ( liTasks.Count > 1 ) {

			float _flDis = Vector3.Distance(tr.position, liTasks[0].position);
			float _flDis2 = 0.0f;
			int _intClosestTask = 0;

			for (int i = 1; i < liTasks.Count; i++) {

				_flDis2 = Vector3.Distance(tr.position, liTasks[i].position);

				_intClosestTask = (_flDis2 > _flDis) ? _intClosestTask : i;

			}

			textPrompt.text = liTasks[_intClosestTask].GetComponent<scrTask>().strTaskName;

		} else if ( liTasks.Count > 0 ) {

			textPrompt.text = liTasks[0].GetComponent<scrTask>().strTaskName;

		} else {

			textPrompt.text = "";

		}
		#endregion

		#region Flip sprite
		if ( Mathf.Abs(rb.velocity.x) > 0.01f || Mathf.Abs(rb.velocity.y) > 0.01f ) {

			ator.SetBool("isMoving", true);

			if ( rb.velocity.x < 0.0f ) {

				transform.localScale = Vector3.one;

			} else  if ( rb.velocity.x > 0.1f ) {

				transform.localScale = Vector3.one -Vector3.right*2;

			}

		} else {

			ator.SetBool("isMoving", false);

		}
		#endregion

		#region Activate Task
		if ( scrHuman.strCurrentRoom != strCurrentRoom && scrHuman2.strCurrentRoom != strCurrentRoom ) {
			
			if ( Input.GetButtonDown(strActionKey) && liTasks.Count > 0 && isDefaultState ) {

				if ( liTasks.Count > 1 ) {



				} else {

					trCurrentTask = liTasks[0];

				}

				doTask(trCurrentTask.GetComponent<scrTask>().strTaskName);

			}

		}
		#endregion

		#region Meow
		if ( Input.GetButtonDown(strMeowKey) && isDefaultState ) {

			doTask("MEOW");
			trCurrentTask = tr;

			for (int i = 0; i < trRooms.Length; i++ ) {

				if ( strCurrentRoom == trRooms[i].name ) {

					if ( scrHuman.newArea == i ) {

						//scrHuman2.doChangePos(i);

					} else if ( scrHuman2.newArea == i ) {

						//scrHuman.doChangePos(i);

					} else {

						int _int = Random.Range(0, 2);

						if ( _int == 0 ) {

							scrHuman.doChangePos(i);

						} else {

							scrHuman2.doChangePos(i);

						}

					}
						
					break;

				}

			}

		}
		#endregion

	}
	
	void FixedUpdate () {
	
		if ( isDefaultState ) {

			Vector2 _v2Dir = Vector2.right *Input.GetAxisRaw(strHorMove) +Vector2.up *Input.GetAxisRaw(strVerMove);

			rb.AddForce(_v2Dir.normalized *flMoveSpeed);

		}

	}

	void doTask(string _strTask) {

		if ( _strTask == "MEOW" ) {

			scrSoundManager.instance.doPlaySFX(acMeow[Random.Range(0,acMeow.Length)]);

		} else {

			scrSoundManager.instance.doPlaySFX(dictTaskSFX[_strTask]);

		}

		if ( _strTask == "CATCH" ) {

			goDisable = goBird;
			Invoke("doDisableObject", 2.0f);

		} else if ( _strTask == "BREAK" ) {

			goDisable = goVase;
			Invoke("doDisableObject", 0.0f);

		} else if ( _strTask == "BOX" ) {

			goDisable = goBox;
			Invoke("doDisableObject", 0.0f);

		} else if ( _strTask == "PLAY" ) {

			goDisable = goYarn;
			Invoke("doDisableObject", 0.0f);

		}

		isDefaultState = false;
		ator.SetTrigger(_strTask);

	}

	public void doPlaySFX(int _intSFX) {

		switch (_intSFX) {

		case 0:

			scrSoundManager.instance.doPlaySFX(acBreak);

			break;

		}

	}

	public void doCompleteTask() {

		liTasks.Remove(trCurrentTask);

		//Check if this Task is already in the player's list of completed tasks
		if ( !liCompletedTasks.Contains(trCurrentTask) ) {

			liCompletedTasks.Add(trCurrentTask);

			if (trCurrentTask.GetComponent<scrTask>().strTaskName == "PISS") {

				scrOtherPlayer.liCompletedTasks.Remove(trCurrentTask);

			} else {

				Destroy(trCurrentTask.gameObject);

			}

		}

		Camera.main.GetComponent<scrTaskManager>().doUpdateLists();

		isDefaultState = true;
		//ator.SetTrigger("DEFAULT");

	}

	public void doCancelTask() {
		
		CancelInvoke("doDisableObject");

		if ( !isDefaultState && trCurrentTask != tr ) {

			if (trCurrentTask.GetComponent<scrTask>().strTaskName == "CATCH" || trCurrentTask.GetComponent<scrTask>().strTaskName == "BREAK" || trCurrentTask.GetComponent<scrTask>().strTaskName == "PLAY" || trCurrentTask.GetComponent<scrTask>().strTaskName == "BOX") {

				doRestoreObject();

			}

		}

		isDefaultState = true;
		ator.SetTrigger("DEFAULT");

	}

	void OnTriggerEnter2D(Collider2D _col)
	{
		
		if (_col.gameObject.layer == 9) {

			doChangeCurrentRoom(_col.gameObject.name);

		}
	}

	void OnTriggerExit2D(Collider2D _col)
	{
		
		if (_col.gameObject.layer == 9 && _col.gameObject.name == strCurrentRoom) {

			doChangeCurrentRoom("");

		}
	}

	void doChangeCurrentRoom(string _strRoom) {

		if ( _strRoom == "" ) {

			strCurrentRoom = strPreviousRoom;

		} else {

			strPreviousRoom = strCurrentRoom;
			strCurrentRoom = _strRoom;

		}

	}

	void doDisableObject() {

		goDisable.SetActive(false);

	}

	void doRestoreObject() {

		goDisable.SetActive(true);

	}

}
	
