using UnityEngine;
using System.Collections;

public class ShipController : MonoBehaviour 
{
	public GameObject[] soulEffects;
	public GameObject[] mercuryEffects;
	public GameObject[] plasmaEffects;
	public GameObject[] fireEffects;
	public GameObject[] lifeEffects;
	public GameObject[] elecEffects;

	public int speed = 10;
	public int rotationSpeed = 10;
    public bool useAcceleration = true;
	public float weaponCooldown = 5;
	public float cooldownReset = 5; 
	public bool hasFired = false;

	void Update()
	{
		if(hasFired) 
			weaponCooldown -= Time.deltaTime;
		if(weaponCooldown < 0)
		{
			weaponCooldown = cooldownReset;
			hasFired = false;
		}
	}
	public void Left(float boostFactor = 1.0f)
	{
        if(useAcceleration)
            rigidbody.AddForce(-transform.right * speed * boostFactor);
	}
    public void Right(float boostFactor = 1.0f)
	{
        if(useAcceleration)
            rigidbody.AddForce(transform.right * speed * boostFactor);
	}

	public void FireWeapon(int effectIndex, Element ElementType)
	{
		if(effectIndex >= 1)
			effectIndex = 0;
		if(ElementType == Element.Soul)
			GameObject.Instantiate(soulEffects[effectIndex], transform.position, transform.rotation);
		else if(ElementType == Element.Mercury)
			GameObject.Instantiate(mercuryEffects[effectIndex], transform.position, transform.rotation);
		else if(ElementType == Element.Plasma)
			GameObject.Instantiate(plasmaEffects[effectIndex], transform.position, transform.rotation);

		if(!hasFired)
		{
			hasFired = true;
			if(ElementType == Element.Life)
				GameObject.Instantiate(lifeEffects[effectIndex], transform.position, Quaternion.identity);
			else if(ElementType == Element.Electricity)
				GameObject.Instantiate(elecEffects[effectIndex], transform.position, transform.rotation);
			else if(ElementType == Element.Fire)
				GameObject.Instantiate(fireEffects[effectIndex], transform.position, transform.rotation);
		}
	}
	void OnTriggerEnter(Collider damageCheck)
	{
		var baseEnemy = damageCheck.gameObject.GetComponent<BaseEnemy>();
		if(baseEnemy != null)
		{
			PlayerHealth.Instance.TakeDamage(baseEnemy.damage, baseEnemy.doesElementalDamage, baseEnemy.elementComponent);
			Destroy(damageCheck.gameObject);
		}
	}
}
