using UnityEngine;
using System.Collections;

public class BonusScript : MonoBehaviour {
	public float rotateSpeed;
	
	public int health;
	public bool healthUp;
	public int armor;
	public int[] ammo;
	public float reviveTime;
	
	public bool exist=true;
	private float time;
	// Use this for initialization
	void Start () {
		Init();	
		exist=true;
	}
	
	// Update is called once per frame
	void Update () {
		transform.Rotate(new Vector3(0,rotateSpeed*Time.deltaTime,0));
		
		time-=Time.deltaTime;
		
		if(time<=0 & exist==false)
		{
			exist=true;
			Render(true);
			Init();
		}
		
		if(time<0) 
			time =0;
	}
	
	void Init()
	{
		if(health>0)
		{
			GameBrain.health.Add(this);
		} 
		else if(armor>0)
		{
			GameBrain.armor.Add(this);
		}else if(armor==0 & health==0) 
		{	
			GameBrain.ammo.Add(this);
		}
	}
	
	void Delete()
	{
		if(health>0)
		{
			GameBrain.health.Remove(this);
		} 
		else if(armor>0)
		{
			GameBrain.armor.Remove(this);
		}
		else 
		{	
			GameBrain.ammo.Remove(this);
		}
	}
	
	void OnTriggerStay(Collider collide)
	{
		if(collide.transform.GetComponent<UnitStatus>() & exist)
		{
			collide.transform.GetComponent<UnitStatus>().ApplyBonus(health,healthUp,armor,ammo);
			Render(false);
			exist=false;
			time=reviveTime;
			Delete();
		}
	}
	
	void Render(bool b)
	{
		if(transform.renderer)
		{
			transform.renderer.enabled = b;
		}
		for(int i=0;i<transform.GetChildCount();i++)
		{
			if(transform.GetChild(i).renderer)
				transform.GetChild(i).renderer.enabled=b;
		}
	}
}
