using Pausable;
using UnityEngine;

namespace AI
{
    [RequireComponent(typeof(Rigidbody2D))]
    public class Projectile : MonoBehaviour, IPausable
    {
        public Rigidbody2D Rigidbody { get; private set; } = null;
        private AudioSource audioSource = null;

        private void Awake()
        {
            Rigidbody = GetComponent<Rigidbody2D>();
            audioSource = GetComponent<AudioSource>();
        }

        public void SetVelocity(Vector2 velocity)
        {
            Rigidbody.velocity = velocity;
        }

        private void OnCollisionEnter2D(Collision2D other)
        {
            ProjectilePool.Instance.Return(this);
            audioSource.Play();

            if (other.transform.CompareTag("Player"))
                other.gameObject.GetComponent<Player.Death>().Die();
        }

        public bool IsPaused { get; protected set; } = false;

        public void Pause()
        {
            IsPaused = true;
            UpdateRigidbody();
        }
        public void Resume()
        {
            IsPaused = false;
            UpdateRigidbody();
        }

        private void UpdateRigidbody()
        {
            Rigidbody.simulated = !IsPaused;
        }
    }
}
