#ifndef _JFXMAP_SDID2HDID_H_
#define _JFXMAP_SDID2HDID_H_

#include <map>

#include "HdTypes.h"
#include "MapConfig.h"
#include "SdTypes.h"
#include "Serialize.h"
#include "Singleton.h"
#include "Utils.h"
#include "World.h"

namespace jf {

    struct sdid2hdid_serialize_data {
        std::vector<RoadId> rids;
        std::vector<HdLinkId> hdlkids;
    };

    class SdId2HdId {
    public:
        static void Deserialize() {
            sdid2hdid_serialize_data d;
            for (const auto& [k, v] : convert_map) {
                d.rids.push_back(k);
                d.hdlkids.push_back(v);
            }
            std::string content = DoSerialize(d, MapConfig::serialize_type_json1_binary2);
            FileSys::Save(MapConfig::sdid2hdid_path, content, MapConfig::tile_encrypt);
        }
        static void Init() {
            std::string content;
            if (!FileSys::Read(MapConfig::sdid2hdid_path, content, MapConfig::tile_encrypt)) {
                return;
            }
            sdid2hdid_serialize_data d;
            DoSeserialize(content, d, MapConfig::serialize_type_json1_binary2);
            if (d.rids.size() != d.hdlkids.size())  // jf_log_error("[SdId2HdId::init]");
                for (int i = 0; i < d.rids.size(); ++i) {
                    convert_map.insert(std::make_pair(d.rids[i], d.hdlkids[i]));
                }
        }
        static bool Convert(const RoadId& in, HdLinkId& hdid) { return jf::Utils::MapValue(convert_map, in, hdid); }
        static void Put(const RoadId& r, const HdLinkId& id) { convert_map.insert(std::make_pair(r, id)); }
        inline static std::map<RoadId, HdLinkId> convert_map;
    };

}  // namespace jf

jfxmap_serialize(jf::sdid2hdid_serialize_data, rids, hdlkids);

#endif