using UnityEngine;
using static UnityEngine.ParticleSystem;

[CreateAssetMenu(fileName = "effect_controller", menuName = "Effect Controller")]
public sealed class EffectController : ScriptableObject
{
    [SerializeField]
    private ParticleSystem particleSystemPrefab = null;
    [SerializeField]
    private float minRadius = 0.3f;
    [SerializeField]
    private float maxRadius = 0.3f;
    [SerializeField]
    private float lifeTime = 0.2f;

    private ParticleSystem particleSystem = null;
    private ParticleSystem ParticleSystem {
        get {
            if (particleSystem == null)
                particleSystem = CreateItem(particleSystemPrefab);
            return particleSystem;
        }
    }

    public void Spawn(Vector3 position, float scale = 1) {
        var effectParams = new EmitParams()
        {
            position = position,
            startSize = Random.Range(minRadius, maxRadius) * scale,
            startLifetime = lifeTime
        };
        ParticleSystem.Emit(effectParams, 1);
    }

    private T CreateItem<T>(T prefab) where T: Component
    {
        var item = Instantiate(prefab);
        DontDestroyOnLoad(item.gameObject);
        return item;
    }
}
