using UnityEngine;
using System.Collections;

public class EnemyFighter : MonoBehavclops
{
	public float fireRate;
	public GameObject laserAttack;
	public Transform[] hardPoints;
	private int nextAttackPoint = 0;
	public int rotationSpeed = 10; 
	public int health = 100;
	public int damage = 10;
	public Sprite soulSprite;
	public Sprite mercurySprite;
	public Sprite plasmaSprite;

	private Element.Component _elementComponent;
	public Element.Component ElementComponent
	{
		get { return _elementComponent; }
		set
		{
			_elementComponent = value;
			LayoutForElement();
		}
	}

	public int speed = 2;

	private Vector3 player;

	void Start () 
	{
		ElementComponent = Random.value > 0.67f ? Random.value > 0.5f ? Element.Component.Soul : Element.Component.Mercury : Element.Component.Plasma;
		InvokeRepeating("FireWeapon", 0, fireRate);
	}
	
	void FireWeapon()
	{
		Instantiate(laserAttack, hardPoints[nextAttackPoint].position,hardPoints[nextAttackPoint].rotation);
		nextAttackPoint++;
		if(nextAttackPoint >= hardPoints.Length)
			nextAttackPoint = 0;
	}
	
	private void LayoutForElement()
	{
		if (ElementComponent == Element.Component.Soul) {
			Cached<SpriteRenderer>().sprite = soulSprite;
		} else if (ElementComponent == Element.Component.Mercury) {
			Cached<SpriteRenderer>().sprite = mercurySprite;
		} else if (ElementComponent == Element.Component.Plasma) {
			Cached<SpriteRenderer>().sprite = plasmaSprite;
		}
	}


	public void TakeDamage(int damage)
	{
		health -= damage;
		if(health <= 0)
		{
			Debug.Log("Boss: Dead");
			Destroy(gameObject);
		}
	}
	void OnTriggerEnter(Collider damageCheck)
	{
		var projectile = damageCheck.gameObject.GetComponent<Projectile>();
		if(projectile != null)
		{
			int dmg = projectile.damage;
			dmg = (int)((float)dmg * Element.ElementalDamageFactor(projectile.ProjectileType, ElementComponent));
			TakeDamage(dmg);
			Destroy(damageCheck.gameObject);
		}
		if(damageCheck.gameObject.tag == "Player")
		{
			PlayerHealth.Instance.TakeDamage(damage, false, Element.Component.Soul);
		}
	}
}