using UnityEngine;
using System.Collections;
using System;

public class PowerUp : MonoBehaviour {
	
	public GameObject prefabMinion;
	public GameObject sprite;
	public float powerUpSpeed = 3.00f;
	public float timeBonusSpeed = 6.0f;
	
	GameObject target;
	bool isAlredyEated = false;

	void OnTriggerStay(Collider co) {
		if (co.transform.tag == "MinionCore" && !isAlredyEated){
			/*
			co.transform.parent.gameObject.GetComponent<MinonsMovements>().speed += powerUpSpeed;
			target = co.transform.parent.gameObject;
 			Invoke("stopPowerUpRush", 6);
			*/
			isAlredyEated = true;
			SpawnMinion();
 			Destroy(transform.gameObject);
		}
		if (co.tag == "MonsterCore" && !isAlredyEated){
			isAlredyEated = true;
			Debug.Log("speed !");
			co.transform.parent.gameObject.GetComponent<Movements>().speed += powerUpSpeed;
			target = co.transform.parent.gameObject;
 			Invoke("StopPowerUpRush", timeBonusSpeed);
			sprite.GetComponent<SpriteRenderer>().enabled = false;
		}
	}
	
	void StopPowerUpRush(){
		Debug.Log("speed ecoule");
		target.GetComponent<Movements>().speed -= powerUpSpeed;
		Destroy(transform.gameObject);
	}
	
	void SpawnMinion(){
		GameObject[] floors = GameObject.FindGameObjectsWithTag("Floor");
		Vector3 tmpPos = floors[UnityEngine.Random.Range (0, floors.Length)].transform.position;
		tmpPos.z = -1;
		GameObject newMinion = Instantiate(prefabMinion, tmpPos, Quaternion.identity) as GameObject;
		GameObject.Find("MinionManager").GetComponent<MinionManager>().AddNewMinion(newMinion);
	}
}