using Assets.Source.Core.Managers;
using Assets.Source.Misc;
using Assets.Source.Patterns.ObjectPool;
using UnityEngine;

namespace Assets.Source.Core.Gameplay.Topdown
{
    public class AsteroidTopdown : MonoBehaviour, IPoolObject, IDamageable
    {
        public VFXEffect VFXDestruction;
        public CollisionDetector Col;

        void Awake()
        {
            Col.OnTEnter2D += c =>
            {
                var damageable = c.transform.GetComponentInParent<IDamageable>();
                var player = GameManagerTopdown.Instance.Player;
                if (damageable == player)
                {
                    damageable.TakeDamage(null, 1);
                    TakeDamage(player, 1);
                }
            };
        }
        
        public void OnSpawnFromPool()
        {
        }

        public void OnReturnToPool()
        {
        }

        public void TakeDamage(IDamager damager, int damage)
        {
            var vfx = VFXDestruction.Spawn(transform.position);
            //var delta = transform.position - damager.transform.position;
            vfx.transform.rotation = damager.transform.right.DirToRotation();
            AudioManager.Instance.PlayAsteroidExplosion();
            this.Release();
        }
    }
}
