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

public enum HitEffectType
{
    SpawnEffect,
    EnemyHit,
    ZapHit,
    SmackHit,
    SplashHit
}
public class HitEffectManager : MonoBehaviour
{
    public static HitEffectManager Instance { get; private set; }

    [System.Serializable]
    public class HitEffectPool
    {
        public HitEffectType type;
        public HitEffect prefab;
        [HideInInspector] public List<HitEffect> instances = new();
    }

    [SerializeField] private List<HitEffectPool> pools = new();
    private void Awake()
    {
        if (Instance != null && Instance != this)
        {
            Destroy(gameObject);
            return;
        }

        Instance = this;
    }

    public void Spawn(HitEffectType type, Vector2 position, Vector2 direction)
    {
        HitEffectPool pool = pools.Find(p => p.type == type);
        if (pool == null || pool.prefab == null)
        {
            Debug.LogWarning($"No hit effect pool for type {type}");
            return;
        }
        
        HitEffect fx = GetOrCreateFromPool(pool);
        fx.transform.position = position;
        fx.gameObject.SetActive(true);
        fx.Play(direction);
        //Debug.Log("VFX Instantiated");
    }

    private HitEffect GetOrCreateFromPool(HitEffectPool pool)
    {
        foreach (var fx in pool.instances)
        {
            if (!fx.gameObject.activeInHierarchy)
            {
                return fx;
            }
        }
        
        //if none available -> create new
        HitEffect newFx = Instantiate(pool.prefab, transform);
        pool.instances.Add(newFx);
        newFx.gameObject.SetActive(false);
        return newFx;
    }
}
