#include "DeerRender/Enviroment.h"
#include "DeerRender/Scene.h"
#include "DeerRender/Tools/Memory.h"
#include "DeerRender/Resource.h"

#include "DeerStudio/StudioAPI/Environment.h"

#include "DeerRender/FrameBuffer.h"
#include "DeerRender/Scene.h"

#include <vector>

namespace Deer {
	namespace StudioAPI {
		// The first element has to allways point to the main scene env
		std::vector<Environment*> environments{&Scene::environment};

		void EnvironmentStruct::render(FrameBufferHandleStruct frameBuffer_handle, SceneCamera& sc) {
			Resource<FrameBuffer> frameBufferResource = Resource<FrameBuffer>::unsafeFromId(frameBuffer_handle.frameBufferId);
			FrameBuffer& frameBuffer = frameBufferResource.getData();

			Environment* envPtr = nullptr;
			if (environmentId < 0) {
			    envPtr = &Scene::environment;
			} else {
			    Resource<Environment> environmentResource = Resource<Environment>::unsafeFromId(environmentId);
			    envPtr = &environmentResource.getData();
			}

			Environment& env = *envPtr;

			frameBuffer.bind();

			// TODO
			env.render(sc);

			frameBuffer.unbind();
		}

		EntityHandleStruct EnvironmentStruct::getRootEntity() {
			return EntityHandleStruct(0, environmentId);
		}

		EntityHandleStruct EnvironmentStruct::getEntity(int id) {
			return EntityHandleStruct(id, environmentId);
		}

		EnvironmentHandleStruct getMainEnvironment() {
			return EnvironmentHandleStruct(-1);
		}

		EnvironmentHandleStruct createLoadEnvironment(std::string& envId) {
			EnvironmentData envData;
			Resource<Environment> environmentResource = ResourceManager<Environment>::loadResourceFromData(envData, envId);

			return EnvironmentHandleStruct(environmentResource.getResourceId());

		}
	} // namespace StudioAPI
} // namespace Deer