
#include "HdImporter.h"

#include <assert.h>

#include <any>
#include <map>
#include <string>
#include <tuple>
#include <vector>

#include "CoordSys.h"
#include "MapConfig.h"
#include "Tile.h"
#include "World.h"
#include "ajson.hpp"

using namespace jf;

void HdImporter::Import(HdJson &data) {
    auto PutNodeToWorld = [this](const Point &p, HdLink &ld) {
        TileId tid = P2TId(p);
        std::shared_ptr<HdTile> t = HdWorld::GetSingleton()->FindOrAddTile(tid);
        t->PutNode(p, ld.liId, ld);
        return t;
    };

    for (auto &pair : data.links) {
        auto &lk = pair.second;
        std::set<Point> ps;
        // double test_dist = std::min(hdconfig::gps_rect_lon_metres_half * 2, hdconfig::gps_rect_lat_metres_half * 2);
        auto tryInsertToBeIndex = [&ps /*, test_dist*/](const Point &inp) {
            assert(inp != Point(0, 0, 0));
            ps.insert(inp);
        };
        // lane
        for (const HdLane &l : lk.vcthlLane) {
            for (const std::vector<double> &v : l.vctvdGeoLink) {
                Point p(v);
                tryInsertToBeIndex(p);
            }
        }
        // edge
        for (const HdEdge &e : lk.vctheEdge) {
            for (const std::vector<double> &v : e.vctvdGeoLink) {
                Point p(v);
                tryInsertToBeIndex(p);
            }
        }

        for (const Point &p : ps) {
            assert(p != Point(0, 0, 0));
            PutNodeToWorld(p, lk);
        }
    }
}
