#include "DeerStudio/EditorEngine/ServiceScript/ServiceScriptInfo.h"
#include "Deer/Log.h"

#include "cereal/cereal.hpp"
#include "cereal/archives/json.hpp"
#include "cereal/types/string.hpp"

#include <filesystem>
#include <fstream>
#include <cstring>

namespace Deer {
    namespace EditorEngine {
        template <class Archive>
        void serialize(Archive& ar, ServiceScriptInfo& data) {
            ar(cereal::make_nvp("name", data.name));
            ar(cereal::make_nvp("author", data.author));
            ar(cereal::make_nvp("version", data.version));
        }

        ServiceScriptInfo* loadServiceScriptInfo(Path seriveScriptInfoPath) {
            try {
                std::ifstream is(seriveScriptInfoPath);

                if (!is)
                    return new ServiceScriptInfo();
                
                ServiceScriptInfo* info = new ServiceScriptInfo();
                {
                    cereal::JSONInputArchive archive(is);
                    archive(cereal::make_nvp("serviceScriptModule", *info));
                }
 
                return info;
            } catch (const std::exception& e) {
                DEER_EDITOR_ENGINE_TRACE(e.what());
                return new ServiceScriptInfo();
            }
        }
    }
}