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

public class MineColliderManager : ColliderManager
{
    public AudioClip mineHitSFX;
    public ParticleSystem effectA;
    public ParticleSystem effectB;

    public float explosionDistance;

    private GameObject _player;

    // Bool checks if the mine has been hit, this is because the ships are made of multiple collision boxes and would trigger more than once.
    private bool _mineHit = false;

    private void Start()
    {
        _player = GameObject.Find("Player");
    }

    public override void ProcessCollision(CollisionDirection direction, Collision collision, bool wasChild)
    {           
        HitMine();
    }

    /// <summary>
    /// Process the mine exploding and destroy the mine object.
    /// </summary>
    public void HitMine()
    {
        // If the mine has been hit ignore it.
        if (!_mineHit)
        {
            float playerDistance = (_player.transform.position - transform.position).magnitude;

            _mineHit = true;
            AudioSource.PlayClipAtPoint(mineHitSFX, transform.position);
            ParticleSystem boomboom = Instantiate(effectA);
            ParticleSystem boomboom2 = Instantiate(effectB);
            boomboom.transform.position = transform.position;
            boomboom2.transform.position = transform.position;
            if (playerDistance <= explosionDistance)
            {
                PlayerColliderManager playerColliderManager = _player.GetComponent<PlayerColliderManager>();
                if (playerColliderManager)
                {
                    playerColliderManager.PlayerHit();
                }
            }            

            if (transform.childCount > 0)
            {
                for (int i = 0; i < transform.childCount; i++)
                {
                    transform.GetChild(i).gameObject.SetActive(false);
                }
            }
            
            Destroy(gameObject, 2.0f);
        }
    }
}
