using System;
using System.Collections;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;

public class AddrResourceMgr : MonoBehaviourSingletonTemplate<AddrResourceMgr>
{
    // Start is called before the first frame update
    void Start()
    {
        DontDestroy();
    }

    public void LoadObject<T>(string path, Action<AsyncOperationHandle<T>> onCompleted)
    {
        // Addressables에서 비동기 로드
        AsyncOperationHandle<T> handle = Addressables.LoadAssetAsync<T>($"Assets/Res_Addr/{path}");

        // 로딩 완료 시 호출될 콜백
        handle.Completed += onCompleted;
    }
    //public void Set_AddressableReleaseSelf(GameObject go)
    //{
    //    if (DSUtil.CheckNull(go.GetComponent<AddressableReleaseSelf>()))
    //        go.AddComponent<AddressableReleaseSelf>();
    //}

    public IEnumerator LoadObjectSequential<T>(string path, Action<T> onLoaded)
    {
        // 로드 요청
        AsyncOperationHandle<T> handle = Addressables.LoadAssetAsync<T>($"Assets/Res_Addr/{path}");

        // 완료될 때까지 대기
        yield return handle;

        // 결과 전달
        if (handle.Status == AsyncOperationStatus.Succeeded)
        {
            onLoaded?.Invoke(handle.Result);
        }
        else
        {
            Debug.LogError($"Failed to load: {path}");
        }

        // 필요하다면 해제는 여기서
        // Addressables.Release(handle);
    }
}