using UnityEngine;

public class AsteroidColliderManager : ColliderManager
{
    public AudioClip asteroidDestroyedSFX;
    public float bounceForce;

    private AudioSource _audioSource;

    // Is the asteroid destroyed when it collides.
    public bool destroyOnHit;
    private bool _exploding = false;

    public override void DoAwakeTasks()
    {
        _audioSource = gameObject.GetComponent<AudioSource>();
        base.DoAwakeTasks();
    }


    /// <summary>
    /// Process the asteroids collision.
    /// </summary>
    /// <param name="direction">Direction the collision happened, typically checking if hit from above or below.</param>
    /// <param name="collision">Collision object holding the details on the collided object.</param>
    /// <param name="wasChild">Was the hit collider a child of this game object.</param>
    public override void ProcessCollision(CollisionDirection direction, Collision collision, bool wasChild)
    {
        GameObject collisionGO;
        if (wasChild)
        {
            // if the collision was a child, get the parent game object.
            collisionGO = collision.transform.parent.gameObject;
        }
        else
        {
            collisionGO = collision.gameObject;
        }

        // Check the tag of the collided object, Collision direction doesnt matter i assume.
        switch (collisionGO.tag)
        {
            case "Mine":
                MineColliderManager mineColliderManager = collisionGO.GetComponent<MineColliderManager>();
                if (mineColliderManager)
                {
                    mineColliderManager.HitMine();
                }
                break;
            case "Enemy":
            case "Player":
                BounceAway(collisionGO, -collision.contacts[0].normal);
                ExplodeAsteroid();
                break;
        }

    }

    /// <summary>
    /// Repel the collided object and the asteroid in opposing directions.
    /// </summary>
    /// <param name="bouncedObject">Game object of the collided object</param>
    /// <param name="collisionPos">Position the collision happened</param>
    public void BounceAway(GameObject bouncedObject, Vector3 collisionPos)
    {
        bouncedObject.GetComponent<Rigidbody>().AddForce(collisionPos * bounceForce, ForceMode.Impulse);
        gameObject.GetComponent<Rigidbody>().AddForce(-collisionPos * bounceForce, ForceMode.Impulse);
    }

    /// <summary>
    /// Destroy the asteroid game object, it should deal with the destruction, animations and sounds related to an asteroid being removed from play.
    /// </summary>
    public void ExplodeAsteroid()
    {
        if (_audioSource != null)
        {
            _audioSource.PlayOneShot(asteroidDestroyedSFX);            
        }
        if (destroyOnHit)
        {
            if (!_exploding)
            {
                if (transform.childCount > 0)
                {
                    for (int i = 0; i < transform.childCount; i++)
                    {
                        transform.GetChild(i).gameObject.SetActive(false);
                    }
                }
                
                Destroy(gameObject, 2.0f);
                _exploding = true;
            }            
        }
    }
}
