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

public class Collisions : MonoBehaviour {

	public GameObject player;
	public Text prompt;
	public Text objectiveText;
	public bool promptPress, destroy, enemyCol;

	public GameObject spawnpoint;


	void Start()
	{
		prompt.text = "";
		destroy = false;
		promptPress = false;
	}

	void Update()
	{
		objectiveText.text = "Score: " + PlayerPrefs.GetInt("Score");
	}

	void OnTriggerEnter(Collider other)
	{
		if (other.gameObject.CompareTag ("Enemy")) {
			if(gameObject.GetComponent<MeshRenderer>().isVisible)
			{
				player = GameObject.Find ("Player");
				Move playerScript = player.GetComponent<Move> ();
				playerScript.sanityLevel -= 10;
				enemyCol = true;
			}
		}

		if (other.gameObject.CompareTag("Point"))
		{
			PlayerPrefs.SetInt ("Score", (PlayerPrefs.GetInt("Score") + (10 * PlayerPrefs.GetInt("Multiplier"))));
			Destroy(other.gameObject);
		}

		if (other.gameObject.CompareTag("Prompt"))
		{
			prompt.text = "Press SPACE to pick up";
			promptPress = true;
		}
		if (other.gameObject.CompareTag("KillBox"))
		{
			gameObject.transform.position = spawnpoint.transform.position;
		}
	}

	void OnTriggerStay(Collider other)
	{
		if (other.gameObject.CompareTag ("Prompt")) 
		{
			if (Input.GetKeyDown(KeyCode.Space))
			{
				GameObject child = other.gameObject.transform.GetChild(0).gameObject;
				if(child.gameObject.CompareTag("Power-Ups"))
				{
					gameObject.GetComponent<PlayerPowerUps>().start = true;
					gameObject.GetComponent<PlayerPowerUps>().type = other.name;
					if(other.name == "Speed")
					{
						gameObject.GetComponent<PlayerPowerUps>().value = 5;

					}
					else if (other.name == "Multiplier")
					{
						gameObject.GetComponent<PlayerPowerUps>().value = 2;
					}
					else if(other.name == "Sanity")
					{
						gameObject.GetComponent<PlayerPowerUps>().value = 10;
					}
				}
				else if(child.gameObject.CompareTag("Objective"))
				{
					PlayerPrefs.SetInt (other.name, 1);
				}
				Destroy(other.gameObject);

				prompt.text = "";
			}
		}
	}

	void OnTriggerExit(Collider other)
	{
		if (other.gameObject.CompareTag("Prompt"))
		{
			prompt.text = "";
			promptPress = false;
		}

		if (other.gameObject.CompareTag ("Enemy")) {
			enemyCol = false;
		}
	}
}
