#include "DeerStudio/EditorEngine/DockPanel/DockPanelInfo.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, DockPanelInfo& data) {
            ar(cereal::make_nvp("name", data.name));
            ar(cereal::make_nvp("author", data.author));
            ar(cereal::make_nvp("version", data.version));
        }

        DockPanelInfo* loadDockPanelInfo(Path dockPanelInfoPath) {
            try {
                std::ifstream is(dockPanelInfoPath);

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