/*
 * Copyright (c) 2015-2019, Statens vegvesen
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package no.vegvesen.nvdbapi.client.clients;

import no.vegvesen.nvdbapi.client.gson.RouteParser;
import no.vegvesen.nvdbapi.client.model.Coordinates;
import no.vegvesen.nvdbapi.client.model.Geometry;
import no.vegvesen.nvdbapi.client.model.Projection;
import no.vegvesen.nvdbapi.client.model.RouteOnRoadNet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Flux;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.UriBuilder;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static no.vegvesen.nvdbapi.client.gson.GsonUtil.rt;

public class RoadNetRouteClient extends AbstractJerseyClient {
    private static final Logger LOG = LoggerFactory.getLogger(RoadNetRouteClient.class);

    public RoadNetRouteClient(String baseUrl, Client client) {
        super(baseUrl, client);
    }

    public List<RouteOnRoadNet> getRoutesOnRoadnet(RoadNetRouteRequest request) {
        return getRoutesOnRoadnetAsync(request).toStream().collect(Collectors.toList());
    }

    public Flux<RouteOnRoadNet> getRoutesOnRoadnetAsync(RoadNetRouteRequest request) {
        WebTarget target = getWebTarget(request);
        return doRequest(target);
    }

    private WebTarget getWebTarget(RoadNetRouteRequest request) {
        Objects.requireNonNull(request, "Missing page info argument.");

        UriBuilder path = endpoint();

        if (request.usesGeometry()) {
            Geometry geometry = request.getGeometry();
            path.queryParam("geometri", geometry.getWkt());
            path.queryParam("maks_avstand", request.getDistanceThreshold());
            if (geometry.getProjection() != Projection.UTM33) {
                path.queryParam("srid", geometry.getProjection().getSrid());
            }
        }
        else if(request.usesReflinkPosition()) {
            path.queryParam("start", request.getStartReflinkPosition());
            path.queryParam("slutt", request.getEndReflinkPosition());
        } else {
            Coordinates startCoordinates = request.getStartCoordinates();
            path.queryParam("start", startCoordinates);
            path.queryParam("slutt", request.getEndCoordinates());
            path.queryParam("maks_avstand", request.getDistanceThreshold());
            path.queryParam("omkrets", request.getCircumferenceAroundPoints());

            if(startCoordinates.getProjection() != Projection.UTM33) {
                path.queryParam("srid", startCoordinates.getProjection().getSrid());
            }
        }

        return getClient().target(path);
    }

    private UriBuilder endpoint() {
        return start().path("beta/vegnett/rute");
    }

    private Flux<RouteOnRoadNet> doRequest(WebTarget target) {
        return new AsyncArrayResult<>(target, rt(RouteParser::parseRoute)).get();
    }
}

@@ -1412,24 +1412,120 @@
 t.clients;%0A%0A
+import com.google.gson.JsonObject;%0Aimport no.vegvesen.nvdbapi.client.clients.util.JerseyHelper;%0A
 import no.ve
@@ -1835,44 +1835,8 @@
 ory;
-%0Aimport reactor.core.publisher.Flux;
 %0A%0Aim
@@ -1944,31 +1944,8 @@
 er;%0A
-import java.util.List;%0A
 impo
@@ -1969,104 +1969,8 @@
 cts;
-%0Aimport java.util.stream.Collectors;%0A%0Aimport static no.vegvesen.nvdbapi.client.gson.GsonUtil.rt;
 %0A%0Apu
@@ -2237,13 +2237,8 @@
 lic 
-List%3C
 Rout
@@ -2239,33 +2239,32 @@
 c RouteOnRoadNet
-%3E
  getRoutesOnRoad
@@ -2310,263 +2310,170 @@
     
-return getRoutesOnRoadnetAsync(request).toStream().collect(Collectors.toList()
+WebTarget target = getWebTarget(request
 );%0A    
-%7D%0A%0A
     
-public Flux%3CRouteOnRoadNet%3E getRoutesOnRoadnetAsync(RoadNetRouteRequest request) %7B%0A        WebTarget target = getWebTarget(request);%0A        return doRequest(targe
+JsonObject result = JerseyHelper.execute(target).getAsJsonObject();%0A        return  RouteParser.parseRoute(resul
 t);%0A
@@ -3941,159 +3941,7 @@
    %7D
-%0A%0A    private Flux%3CRouteOnRoadNet%3E doRequest(WebTarget target) %7B%0A        return new AsyncArrayResult%3C%3E(target, rt(RouteParser::parseRoute)).get();%0A    %7D
 %0A%7D%0A

Return only one result for route calculation