#include "DeerStudio/AngelScriptEngine/AngelScriptRegisters.h"
#include "DeerStudio/StudioAPI/Resources.h"

#include "DeerRender/Resource.h"

namespace Deer {
	namespace AngelScriptEngine {
		template <class T>
		void registerResourceBasics(const char* objName) {
			REGISTER_OBJECT_METHOD(objName, "bool isValid() const", StudioAPI::APIResource<T>, isValid);
			REGISTER_OBJECT_METHOD(objName, "string get_name() const property", StudioAPI::APIResource<T>, getName);
			REGISTER_OBJECT_METHOD(objName, "string get_path() const property", StudioAPI::APIResource<T>, getPath);
			REGISTER_OBJECT_METHOD(objName, "int get_resourceId() const property", StudioAPI::APIResource<T>, getResourceId);
		}
	} // namespace AngelScriptEngine

	void AngelScriptEngine::registerResourceStructs() {
		scriptEngine->SetDefaultNamespace("");
		AS_CHECK(scriptEngine->RegisterObjectType("GPUMesh", sizeof(StudioAPI::APIResource<GPUMesh>), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_ALLINTS | asGetTypeTraits<StudioAPI::APIResource<GPUMesh>>()));
		AS_CHECK(scriptEngine->RegisterObjectType("Shader", sizeof(StudioAPI::APIResource<Shader>), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_ALLINTS | asGetTypeTraits<StudioAPI::APIResource<Shader>>()));
		AS_CHECK(scriptEngine->RegisterObjectType("Texture", sizeof(StudioAPI::APIResource<Texture>), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_ALLINTS | asGetTypeTraits<StudioAPI::APIResource<Shader>>()));
		scriptEngine->SetDefaultNamespace("");

		scriptEngine->RegisterEnum("ResourceType");
		scriptEngine->RegisterEnumValue("ResourceType", "None", (int)StudioAPI::ResourceType::NONE);
		scriptEngine->RegisterEnumValue("ResourceType", "Mesh", (int)StudioAPI::ResourceType::MESH);
		scriptEngine->RegisterEnumValue("ResourceType", "Shader", (int)StudioAPI::ResourceType::SHADER);
		scriptEngine->RegisterEnumValue("ResourceType", "Texture", (int)StudioAPI::ResourceType::TEXTURE);
	}

	void AngelScriptEngine::registerResourceFunctions() {
		registerResourceBasics<GPUMesh>("GPUMesh");
		registerResourceBasics<Shader>("Shader");
		registerResourceBasics<Texture>("Texture");

		scriptEngine->SetDefaultNamespace("Resource");
		REGISTER_GLOBAL_FUNC("GPUMesh loadGPUMesh(string&in path)", ResourceManager<GPUMesh>::loadResource<ResourceDataSource>);
		REGISTER_GLOBAL_FUNC("Shader loadShader(string&in path)", ResourceManager<Shader>::loadResource<ResourceDataSource>);
		REGISTER_GLOBAL_FUNC("Texture loadTexture(string&in path)", ResourceManager<Texture>::loadResource<ResourceDataSource>);

		REGISTER_GLOBAL_FUNC("array<string>@ getResourceFolders(string&in path)", StudioAPI::getResourceFolders);
		REGISTER_GLOBAL_FUNC("array<string>@ getResourceFiles(string&in path)", StudioAPI::getResourceFiles);
		REGISTER_GLOBAL_FUNC("ResourceType getResourceType(string&in path)", StudioAPI::getResourceType);
		scriptEngine->SetDefaultNamespace("");
	}
} // namespace Deer