using UnityEngine;
using System.Collections.Generic;
using YooAsset;
using Cysharp.Threading.Tasks;
using Tuan.GameFramework;

namespace Tuan.GameScripts.Main
{
    public class SceneLoader : Singleton<SceneLoader>
    {
        Dictionary<string, SceneHandle> sceneHandles = new Dictionary<string, SceneHandle>();
        public async UniTask LoadSceneAsync(string sceneName, LoadingWindow loadingWindow = null)
        {
            SceneHandle sceneHandle = null;
            if (!sceneHandles.ContainsKey(sceneName))
            {
                sceneHandle = YooAssets.LoadSceneAsync("Test");
                await sceneHandle.ToUniTask(loadingWindow);
                sceneHandles[sceneName] = sceneHandle;
            }
            else
            {
                sceneHandle = sceneHandles[sceneName];
            }
            sceneHandle.ActivateScene();
            if(loadingWindow != null)
                UIManager.Inst.CloseUI(loadingWindow);
        }
        public void LoadScene(string sceneName)
        {
            SceneHandle sceneHandle = null;
            if (!sceneHandles.ContainsKey(sceneName))
            {
                sceneHandle = YooAssets.LoadSceneSync("Test");
                sceneHandles[sceneName] = sceneHandle;
            }
            else
            {
                sceneHandle = sceneHandles[sceneName];
            }
            sceneHandle.ActivateScene();
        }
        public SceneHandle GetHandle(string name)
        {
            if (sceneHandles.ContainsKey(name))
                return sceneHandles[name];
            return null;
        }
    }
}

