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

public class Powerups : MonoBehaviour {

	/*public MeshFilter f;

	public Mesh speedM;
	public Mesh knifeM;
	public Mesh stealthM;
	public Mesh trackerM;


	public Material[] speedMT;
	public Material[] knifeMT;
	public Material[] stealthMT;
	public Material[] trackerMT;


	public Quaternion speedR;
	public Quaternion knifeR;
	public Quaternion stealthR;
	public Quaternion trackerR;*/


	public Text speedText;
	public Text knifeText;
	public Text stealthText;
	public Text mirrorText;
	public Text trackerText;
	public Text inverterText;
	public Text bombText;

	public bool inStealth = false;

	void OnTriggerEnter (Collider other) {
		Powerup pwp;
		if(other.GetComponent<Powerup>()) {
			pwp = other.GetComponent<Powerup> ();
			if (pwp.name == "Speed") {
				StartCoroutine(Speed ());
				Destroy (pwp.gameObject);
			}
			if (pwp.name == "Knife") {
				StartCoroutine(Knife ());
				Destroy (pwp.gameObject);
			}
			if (pwp.name == "Stealth") {
				StartCoroutine(Stealth ());
				Destroy (pwp.gameObject);
			}
			if (pwp.name == "Mirror") {
				StartCoroutine(Mirror ());
				Destroy (pwp.gameObject);
			}
			if (pwp.name == "Tracker") {
				StartCoroutine(Tracker ());
				Destroy (pwp.gameObject);
			}
			if (pwp.name == "Inverter") {
				StartCoroutine(Inverter ());
				Destroy (pwp.gameObject);
			}
			if (pwp.name == "Bomb") {
				StartCoroutine(Bomb ());
				pwp.GetComponent<Bomb> ().Activate ();
			}
		}
		if(other.GetComponent<Knife>()) {
			if (other.transform.parent.name == "Player1" && transform.name == "Player2") {
				GameManager.WinP1 ("knife");
			} else if (other.transform.parent.name == "Player2" && transform.name == "Player1") {
				GameManager.WinP2 ("knife");
			}
		}
	}


	IEnumerator Speed() {
		/*f.mesh = speedM;
		f.GetComponent<Renderer> ().materials = speedMT;
		f.transform.rotation = speedR;*/
		GetComponent<Movement> ().speed *= 2;
		speedText.color = new Color32 (245, 245, 245, 255);
		yield return new WaitForSeconds (10);
		speedText.color = new Color32 (50, 50, 50, 255);
		GetComponent<Movement> ().speed /= 2;
		//f.mesh = null;
	}
	IEnumerator Knife() {
		/*f.mesh = knifeM;
		f.GetComponent<Renderer> ().materials = knifeMT;
		f.transform.rotation = knifeR;*/
		transform.GetChild (0).gameObject.SetActive (true);
		knifeText.color = new Color32 (245, 245, 245, 255);
		yield return new WaitForSeconds (6);
		knifeText.color = new Color32 (50, 50, 50, 255);
		transform.GetChild (0).gameObject.SetActive (false);
		//f.mesh = null;
	}
	IEnumerator Stealth() {
		/*f.mesh = stealthM;
		f.GetComponent<Renderer> ().materials = stealthMT;
		f.transform.rotation = stealthR;*/
		stealthText.color = new Color32 (245, 245, 245, 255);
		inStealth = true;
		yield return new WaitForSeconds (10);
		stealthText.color = new Color32 (50, 50, 50, 255);
		inStealth = false;
		//f.mesh = null;
	}
	IEnumerator Mirror() {
		mirrorText.color = new Color32 (245, 245, 245, 255);
		transform.GetChild (1).gameObject.SetActive (true);
		yield return new WaitForSeconds (10f);
		transform.GetChild (1).gameObject.SetActive (false);
		mirrorText.color = new Color32 (50, 50, 50, 255);
	}
	IEnumerator Tracker() {
		//f.mesh = trackerM;
		//f.GetComponent<Renderer> ().materials = trackerMT;
		//f.transform.rotation = trackerR;
		trackerText.color = new Color32 (245, 245, 245, 255);
		float time = 10;
		while(time > 0) {
			yield return new WaitForSeconds (0.6f);
			if (transform.name == "Player1") {
				StartCoroutine(GameManager.lastWave.HitPlayer1 ());
			} else {
				StartCoroutine(GameManager.lastWave.HitPlayer2 ());
			}
			time -= 0.5f;
		}
		trackerText.color = new Color32 (50, 50, 50, 255);
		//f.mesh = null;
	}
	IEnumerator Inverter() {
		inverterText.color = new Color32 (245, 245, 245, 255);
		GetComponent<Movement> ().speed = -GetComponent<Movement> ().speed;
		yield return new WaitForSeconds (0.25f);
		inverterText.color = new Color32 (50, 50, 50, 255);
		//f.mesh = null;
	}
	IEnumerator Bomb() {
		bombText.color = new Color32 (245, 245, 245, 255);
		yield return new WaitForSeconds (0.25f);
		bombText.color = new Color32 (50, 50, 50, 255);
	}
}
