let roadAdminName = []; // liste des reponses de ROAD_ADMIN_NAMES_FC
let TheRoute = null;
let TheRouteLinks = null; // liste of links found by the road

async function pdeRoad() {

    let layers = {
        ROAD_ADMIN_NAMES_FC: {
            callback: pdeGotRoad,
            fcLayers: [1, 2, 3, 4, 5]
        },
        TRAFFIC_PATTERN:{
            callback:pdeGotTrafficPattern,
            level:-1
        }
    };

    geomFC = [];
    linkattrib = [];

    pdeManager.setLayers(layers);

    let start = await hm.geocode("8 rue benjamin franklin, paris");
    let stop = await hm.geocode("3 rue claude debussy,buc");
    console.log(start.coord);
    console.log(stop.coord);
    TheRoute = await hm.route(start.coord, stop.coord);
    hm.polyline({
        layer: "route",
        coords: TheRoute.coords
    });

    // get list of linkId in positive way
    let linkids = TheRoute.route.leg[0].link;
    console.log("linkids", linkids);

    TheRouteLinks = linkids.map(elt => Math.abs(elt.linkId));
    console.log("TheRouteLinks", TheRouteLinks);


    linkids.forEach(elt => {
        if (Math.abs(elt.linkId) == 1258355445) {
            console.log("LOST LINK ", elt);
            let shape = elt.shape;
            const _coords = shape.map(latlng => {
                let coord = latlng.split(",");
                coord[0] = parseFloat(coord[0]);
                coord[1] = parseFloat(coord[1]);
                return coord;
            });
            console.log("_coords",_coords);
            hm.polyline({
                layer: "route",
                coords: _coords,
                style: {
                    lineWidth: 4,
                    strokeColor: "red",
                }
            });
            let level = 1 + 8;
            let coord = [48.839711, 2.2010636];
            let coord2 = [48.839711, 2.1997547];
            const tileSizeDegree = 180.0 / (1 << level);

            let tileY = Math.floor((coord[0] + 90.0) / tileSizeDegree);
            let tileX = Math.floor((coord[1] + 180.0) / tileSizeDegree);
            console.log(`${tileX}/${tileY}/9`);
            tileY = Math.floor((coord2[0] + 90.0) / tileSizeDegree);
            tileX = Math.floor((coord2[1] + 180.0) / tileSizeDegree);
            console.log(`${tileX}/${tileY}/9`);


        }
    });


    pdeManager.setBoundingCoords(TheRoute.coords);

    pdeManager.run().then(res => {
        pdeRoadFinished(res);
    })

    let rects = pdeManager.getTilesInfo();
    //console.log(rects);

    rects.forEach(rect => {
        hm.polyline({
            layer: "tiles",
            coords: hm.bb2Poly(rect.bb),
            style: {
                lineWidth: 2,
                strokeColor: "rgba(100, 100, 0, 0.5)",
            }
        });
    });


} //of pdeGeomFC


// only to get sens of travel
function pdeGotRoad(resp) {

    console.log("pdeGotRoad META", resp.Meta);
    if (!resp.Rows && !resp.Rows.length) // no content
    return;
    console.log("pdeGotRoad rows0", resp.Rows[0]);
    resp.Rows.forEach(row => roadAdminName.push(row));

    /*WHERE IS THIS LINK ?*/
    let elt = resp.Rows.find(row => row.LINK_ID == "1258355445")
    if (elt) console.log("found LINK 1258355445 ", elt, resp.Meta.tileX + "/" + resp.Meta.tileY);
    /*WHERE IS THIS LINK ?*/
}

// only to get sens of travel
function pdeGotTrafficPattern(resp) {

    console.log("pdeGotTrafficPattern META", resp.Meta);
    if (!resp.Rows && !resp.Rows.length) // no content
    return;
    console.log("pdeGotRoad rows0", resp.Rows[0]);
  
}

// here, one can combine the right b its and pieces which are of interest
function pdeRoadFinished(ntiles) {
    infoClean();
    infoAdd(`${ntiles} Tiles`);
    // console.log("roadAdminName", roadAdminName);

    let name = "";
    // cherche info pour chaque lien de la route
    TheRouteLinks.forEach(link => {


        let entry = roadAdminName.find(elt => elt.LINK_ID == link);
        if (!entry) {
            console.log("LINK " + link + " not found");
            return;
        }
        if (name != entry.ORDER8_NAMES) { // display only change of ORDER8
            name = entry.ORDER8_NAMES;
            console.log(name.substr(5));

        }



    });

}