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

public class BloodExplosionBehaviour : MonoBehaviour
{
	public float m_EndTime = 0.1f;
	private float m_Timer = 0;
	
	private int m_NrOfSplats;
	
	public List<Material> m_Materials;
	
	// Use this for initialization
	public void Start () 
	{
		GameObject.Find("Radio").GetComponent<Radio>().Splat();
		m_Timer = 0;
		gameObject.GetComponent<ParticleEmitter>().emit = true;	
		gameObject.GetComponent<ParticleEmitter>().enabled = true;	
		m_NrOfSplats = Random.Range((int)gameObject.GetComponent<ParticleEmitter>().minEmission,(int)gameObject.GetComponent<ParticleEmitter>().maxEmission);
	}
	
	// Update is called once per frame
	public void Update () 
	{
		HandleEmitter();
	}
	
	private void HandleEmitter()
	{
		//If the emitter is not enabled yet, the following code has no use.
		if (!gameObject.GetComponent<ParticleEmitter>().emit) return;
		
		if(m_NrOfSplats>=0)
		{
			int matId=Random.Range(0,m_Materials.Count-1);
			gameObject.GetComponent<ParticleRenderer>().material = m_Materials[matId];
			gameObject.GetComponent<ParticleEmitter>().Emit(1);
			m_NrOfSplats--;
		}
		else
		{
			m_Timer += Time.deltaTime;
			if(m_Timer>=m_EndTime)
			{
				gameObject.GetComponent<ParticleEmitter>().emit = false;	
				gameObject.GetComponent<ParticleEmitter>().enabled = false;	
			}
		}
	}
}
