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

public class Enemy_Paladin : BaseEnemy
{
    private List<BoxCollider2D> damageableTriggers;

    public AudioClip projectileBlock;

    protected override void Start()
    {
        base.Start();
        damageableTriggers = new List<BoxCollider2D>();

        BoxCollider2D[] possibleTriggers = gameObject.GetComponentsInChildren<BoxCollider2D>();

        for(int t =0; t < possibleTriggers.Length; t++)
        {
            BoxCollider2D disBox = possibleTriggers[t];
            if (disBox.gameObject.name.Contains("Damage"))
                damageableTriggers.Add(disBox);
        }

    }

    public override bool EvaluateImpactWithProjectile(Projectile projHittingMe)
    {
        //Did it hit a damage zone?
        if( damageableTriggers.Count > 0 )
        {
            bool bNoHit = true;
            Debug.Log("I have " + damageableTriggers.Count + " hittable locations! ");
            for(int t=0; t < damageableTriggers.Count; t++)
            {
                BoxCollider2D disBox = damageableTriggers[t];
                if( disBox.IsTouching( projHittingMe.GetComponent<BoxCollider2D>() ))
                {
                    bNoHit = false;
                }

            }

            if( bNoHit )
            {
                Rigidbody2D projectileBody = projHittingMe.GetComponent<Rigidbody2D>();
                projectileBody.fixedAngle = false;
                projectileBody.gravityScale = 1.0f;
                projectileBody.AddTorque(Random.value * 200.0f);
                Vector3 direction = transform.position - projectileBody.transform.position;
                direction.Normalize();
                projectileBody.AddForce(Vector2.up * (100.0f + Random.value * 350.0f));
                projectileBody.AddForce(direction *( -200f - (400.0f * Random.value)));
                projectileBody.gameObject.layer = LayerMask.NameToLayer("NoCollide");

                DestroyObject(projectileBody.gameObject, 3.0f);

                myAudio.PlayOneShot(projectileBlock);

                return false;
            }
        }

        myBody.AddForce(projHittingMe.GetComponent<Rigidbody2D>().velocity * 1000.0f);
        myBody.AddForce(Vector2.up * 5000.0f);
        TakeDamage(100.0f);

        return true;
    }

    public override void StartDying()
    {
        base.StartDying();
        SpawnPowerup();

    }

    public override void OnCollisionEnter2D(Collision2D coll)
    {
        //did we hit the player?
        if (playerScript != null && coll.gameObject == playerScript.gameObject)
        {
            fRemainingAttackTimer = 0.0f;
        }
    }
}
