#include "Deer/Enviroment.h"
#include "Deer/Scene.h"
#include <unordered_map>

namespace Deer {
    namespace EnvironmentManager {
        struct EnvironmentContainer {
            Environment* env_data = nullptr;
            std::string env_name;
        };

        std::unordered_map<std::string, uint16_t> environment_name_id;
        EnvironmentContainer environments[MAX_ENVIRONMENT_COUNT]{};
        uint16_t maxEnvironmentId = 1;
    }

    
	uint16_t EnvironmentManager::createEnvironment(const std::string& name) {
        uint16_t envId = maxEnvironmentId;
        maxEnvironmentId++;

        environments[envId].env_data = new Environment();
        environments[envId].env_name = name;

        environment_name_id[name] = envId;
        return envId;
    }

	uint16_t EnvironmentManager::getEnvironmentId(const std::string& name) {
        return environment_name_id[name];
    }

	Environment& EnvironmentManager::getEnvironment(uint16_t id) {
        if (id == 0)
            return Scene::environment;

        return *environments[id].env_data;
    }

    const std::string& EnvironmentManager::getEnvironmentName(uint16_t id) {
        const static std::string main_env_name("Main Environment");
        if (id == 0)
            return main_env_name;

        return environments[id].env_name;
    }

	void EnvironmentManager::clearAllEnvironments() {
        for (int i = 1; i < maxEnvironmentId; i++) {
            delete environments[i].env_data;
            environments[i].env_data = nullptr;
        }

        environments[0].env_data = &Scene::environment;
        environments[0].env_name = "Main Environment";

        environment_name_id.clear();
        environment_name_id["Main Environment"] = 0;

        maxEnvironmentId = 1;
    }
}