#include "scene_manager.h"
#include "zlog.h"
namespace engineapi {
	Scene* SceneManager::GetScene(const string& name)
	{
		auto sceneInfo = GetSceneInfo(name);
		if (sceneInfo == nullptr)
		{
			return nullptr;
		}
		else
		{
			return sceneInfo->scene;
		}
	}

	void SceneManager::LoadScene(const string& path, bool switchNow)
	{
		curScene = new SceneInfo{
			path,
			new Scene()
		};
	}

	void SceneManager::SwitchScene(const string& name)
	{
		
	}

	void SceneManager::DeleteScene(const string& name)
	{
		map<string, SceneInfo*>::iterator iter = scenes.find(name);
		if (iter != scenes.end())
		{
			delete iter->second->scene;
			delete iter->second;
			scenes.erase(iter);
		}
		else
		{
			zlog::info("Attempt to delete a nonexistent scene: {}", name);
		}
	}

	void SceneManager::DeleteAllScene()
	{
		for (auto& iter : scenes)
		{
			delete iter.second->scene;
			delete iter.second;
		}
		scenes.clear();
	}

	void SceneManager::ReloadScene()
	{
		// 先等待当前所有绘制结束，再卸载场景释放资源

	}

	void SceneManager::Render()
	{
		curScene->scene->Render();
	}

	void SceneManager::Update()
	{
		curScene->scene->Update();
	}

	Scene* SceneManager::GetCurScene()
	{
		return curScene->scene;
	}

	SceneInfo* SceneManager::GetSceneInfo(const string& name)
	{
		map<string, SceneInfo*>::iterator iter = scenes.find(name);
		if (iter != scenes.end())
			return iter->second;
		else
			return nullptr;
	}
}

