using UnityEngine;
using System.Collections;

public class Boss : MonoBehaviour 
{
	public float fireRate;
	public GameObject laserAttack;
	public Transform[] hardPoints;
	private int nextAttackPoint = 0;
	public int rotationSpeed = 10; 
	public int health = 100;
	private bool beginSpecialAttack = false;
	public int damage = 10;

	public GameObject[] waypoints;
	public int waypointId = 0;
	public Transform target;
	public int speed = 2;

	void Start () 
	{
		InvokeRepeating("FireWeapon", 0, fireRate);
		waypoints = GameObject.FindGameObjectsWithTag("Boss Points");
		target = waypoints[waypointId].transform;
	}
	void Update ()
	{
		transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);
		if(health <= 50 && !beginSpecialAttack)
		{
			beginSpecialAttack = true;
			Debug.Log("Boss: Special Attack");
			InvokeRepeating("FireWeapon", 0, fireRate/2);
		}
		if(Vector3.Distance(target.position, transform.position) < .2)
		{
			waypointId++;
			if(waypointId >= waypoints.Length)
				waypointId = 0;
			target = waypoints[waypointId].transform;
		}
		transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
		
	}
	
	void FireWeapon()
	{
		Instantiate(laserAttack, hardPoints[nextAttackPoint].position,hardPoints[nextAttackPoint].rotation);
		nextAttackPoint++;
		if(nextAttackPoint >= hardPoints.Length)
			nextAttackPoint = 0;
	}
	
	public void TakeDamage(int damage)
	{
		health -= damage;
		if(health <= 0)
		{
			Debug.Log("Boss: Dead");
			Destroy(gameObject);
		}
	}
	void OnTriggerEnter(Collider damageCheck)
	{
		if(damageCheck.gameObject.GetComponent<Projectile>() != null)
		{
			Debug.Log("Boss: Damage Taken");
			TakeDamage(damageCheck.gameObject.GetComponent<Projectile>().damage);
			Destroy(damageCheck.gameObject);
		}
		if(damageCheck.gameObject.tag == "Player")
		{
			Debug.Log("Player: Damage Taken");
			PlayerHealth.Instance.TakeDamage(damage, false, Element.Component.Soul);
		}
	}
}