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

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

        void Awake()
        {
            Col.OnTEnter2D += c =>
            {
                var player = GameManagerTopdown.Instance.Player;
                if (c.GetComponentInParent<PlayerShipTopdown>() == player)
                {
                    HitPlayer(player);
                }
            };
        }
        
        public void OnSpawnFromPool()
        {
        }

        public void OnReturnToPool()
        {
        }

        private void HitPlayer(PlayerShipTopdown player)
        {
            player.Heal(1);
            var vfx = VFXDestruction.Spawn(transform.position);
            vfx.transform.rotation = player.transform.right.DirToRotation();
            vfx.transform.Attach(player.transform);
            AudioManager.Instance.PlayPlayerPickedUpHealth();
            this.Release();
        }
    }
}
