using HutongGames.PlayMaker;
using System.Threading.Tasks;
using UnityEngine;

namespace BansheeGz.BGDatabase
{
    [ActionCategory("BansheeGz")]
    [HutongGames.PlayMaker.Tooltip("Asynchronously release all addressable assets cached across all groups, then fire an event.")]
    public class BGAddressablesReleaseAllCache : FsmStateAction
    {
        [HutongGames.PlayMaker.Tooltip("Event to send once all caches are released")]
        public FsmEvent ReleasedEvent;

        public override void Reset()
        {
            ReleasedEvent = null;
        }

        public override void OnEnter()
        {
            ReleaseAllAsync();
        }

        private async void ReleaseAllAsync()
        {
            // This delay allows the release operation to be non-blocking
            await Task.Yield(); // Can be replaced with Task.Delay(1) if needed
            BGAddressablesAssetCache.ReleaseAll();
            Fsm.Event(ReleasedEvent);
            Finish();
        }
    }
}

