#include <sstream>
#include <string>
#include <vector>

#include "Deer/Scene.h"
#include "Deer/Scene/Serialization/Serialization.h"
#include "cereal/archives/json.hpp"
#include "cereal/archives/portable_binary.hpp"

#include "Deer/Scene.h"

namespace Deer {
	void DataStore::loadScene(const Path& name) {
		Path realName;
		realName = Path(DEER_SCENE_PATH) / (name.string() + ".dscn");

		uint32_t size;
		uint8_t* data = DataStore::readFile(realName, &size);

		std::string strData((char*)data, size);
		std::istringstream stream(strData);

		{
			cereal::JSONInputArchive archive(stream);
			archive(cereal::make_nvp("environment", Scene::environment));
		}

		delete[] data;
	}

	void DataStore::exportScene(const Path& name) {
		Path realName;
		realName = Path(DEER_SCENE_PATH) / (name.string() + ".dscn");

		std::stringstream output;
		{
			cereal::JSONOutputArchive archive(output);
			archive(cereal::make_nvp("environment", Scene::environment));
		}
		std::string_view view = output.view();

		DataStore::saveFile(realName, (uint8_t*)view.data(), view.size());
	}

	void DataStore::exportRuntimeScene() {
		Path realName;
		realName = Path(DEER_TEMP_PATH) / ("temp_scene.dbscn");

		std::stringstream output;
		{
			cereal::PortableBinaryOutputArchive archive(output);
			archive(cereal::make_nvp("environment", Scene::environment));
		}
		std::string_view view = output.view();

		DataStore::saveFile(realName, (uint8_t*)view.data(), view.size());
	}
	
	void DataStore::importRuntimeScene() {
		Path realName;
		realName = Path(DEER_TEMP_PATH) / ("temp_scene.dbscn");

		uint32_t size;
		uint8_t* data = DataStore::readFile(realName, &size);

		std::string strData((char*)data, size);
		std::istringstream stream(strData);

		{
			cereal::PortableBinaryInputArchive archive(stream);
			archive(cereal::make_nvp("environment", Scene::environment));
		}

		delete[] data;
	}

}  // namespace Deer
