#if USING_ADDRESSABLES

using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.SceneManagement;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceProviders;

namespace SimplifyXR
{   
    /// <summary>
    /// Addressables Runtime Bridge Adapter for Module Management
    /// </summary>
    public class AddressablesRuntimeBridge : IModuleRuntimeManager
    {

        AsyncOperationHandle<SceneInstance> loadedScene;

        public void LoadModuleAsync(
            string objectKey, 
            LoadModule.ModuleLoadType loadType, 
            System.Action successAction = null, 
            System.Action failAction = null)
        {

            var loadOperation = Addressables.LoadSceneAsync(objectKey,
                (UnityEngine.SceneManagement.LoadSceneMode)loadType);

            loadOperation.Completed +=
                (e) =>
                {
                    if (e.Status == AsyncOperationStatus.Succeeded)
                    {
                        successAction?.Invoke();
                        loadedScene = e;
                    }
                    else
                    {
                        failAction?.Invoke();
                    }
                };
        }

        public void UnloadModuleAsync()
        {
            try
            {
                var unloadOperation = Addressables.UnloadSceneAsync(loadedScene);
            }
            catch (System.Exception e) { }
        }
    }


}

#endif

