package com.afrozaar.wordpress.wpapi.v2.request;

import com.afrozaar.wordpress.wpapi.v2.Client;

import com.google.common.collect.ImmutableMap;

import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.List;
import java.util.Map;

public abstract class Request {
    public static final String POSTS = "/posts";
    public static final String POST = "/posts/{id}";
    public static final String POST_TERMS = "/posts/{postId}/{taxonomy}";
    public static final String POST_TERM = "/posts/{postId}/{taxonomy}/{termId}";
    public static final String POST_TAGS = "/tags?post={postId}";

    public static final String METAS = "/posts/{postId}/meta";
    public static final String META = "/posts/{postId}/meta/{metaId}";
    public static final String META_POST_DELETE = "/posts/{postId}/meta/{metaId}/delete";
    public static final String TAXONOMIES = "/taxonomies";
    public static final String TAXONOMY = "/taxonomies/{slug}";
    public static final String TERMS = "/terms/{taxonomySlug}";
    public static final String TERM = "/terms/{taxonomySlug}/{termId}";
    public static final String MEDIAS = "/media";
    public static final String MEDIA = "/media/{mediaId}";
    public static final String PAGES = "/pages";
    public static final String PAGE = "/pages/{pageId}";
    public static final String USERS = "/users";
    public static final String USERS_WITH_CONTEXT = "/users?context={context}";
    public static final String USER = "/users/{userId}";
    public static final String TAGS = "/tags";
    public static final String TAG = "/tags/{tagId}";
    public static final String CATEGORIES = "/categories";
    public static final String CATEGORY = "/categories/{categoryId}";
    public static final String QP_CONTEXT = "context";
    public static final String QP_ORDER_BY = "orderby";
    public static final String QP_ORDER = "order";
    private static final String QP_REST_ROUTE = "rest_route";
    private static final String WP_JSON = "/wp-json";

    final String uri;
    final Map<String, List<String>> params;

    public Request(String uri, Map<String, List<String>> params) {
        this.params = params;
        this.uri = uri;
    }

    public static Request of(String uri) {
        return of(uri, ImmutableMap.of());
    }

    public static Request of(String uri, Map<String, List<String>> params) {
        return new Request(uri, params) {
        };
    }

    public UriComponentsBuilder usingClient(Client client) {
        return forHost(client, Client.CONTEXT);
    }

    public UriComponentsBuilder forHost(Client client, String context0) {

        final String context = (client.permalinkEndpoint ? context0 : context0.replace(WP_JSON, "")) + this.uri;
        // context = "/wp-json/wp/v2"

        final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(client.baseUrl + (client.permalinkEndpoint ? context : ""));
        if (!client.permalinkEndpoint) {
            builder.queryParam(QP_REST_ROUTE, context);
        }
        params.forEach((key, values) -> builder.queryParam(key, values.toArray()));
        return builder;
    }

    public static URI fromLink(String apply) {
        return UriComponentsBuilder.fromHttpUrl(apply).build().toUri();
    }

    public String asRequestUrl(Client client) {
        return usingClient(client).build().toUri().toASCIIString();
    }
}

@@ -39,24 +39,74 @@
 2.request;%0A%0A
+import static java.util.stream.Collectors.toMap;%0A%0A
 import com.a
@@ -269,16 +269,71 @@
 et.URI;%0A
+import java.util.Arrays;%0Aimport java.util.Collections;%0A
 import j
@@ -2736,16 +2736,237 @@
 xt0) %7B%0A%0A
+        final String theUri = this.uri.contains(%22?%22)%0A                ? this.uri.substring(0, this.uri.indexOf(%22?%22))%0A                : this.uri;%0A%0A        final Map%3CString, String%3E paramsFromUri = paramsFromUri(this.uri);%0A%0A
         
@@ -3066,53 +3066,13 @@
 + th
-is.uri;%0A        // context = %22/wp-json/wp/v2%22
+eUri;
 %0A%0A  
@@ -3204,24 +3204,25 @@
 ext : %22%22));%0A
+%0A
         if (
@@ -3316,16 +3316,122 @@
       %7D%0A
+        if (!paramsFromUri.isEmpty()) %7B%0A            paramsFromUri.forEach(builder::queryParam);%0A        %7D%0A
         
@@ -3703,32 +3703,32 @@
 lient client) %7B%0A
-
         return u
@@ -3784,11 +3784,506 @@
 );%0A    %7D
+%0A%0A    private Map%3CString, String%3E paramsFromUri(String uri) %7B%0A        //%22/tags?post=%7BpostId%7D%22%0A%0A        if (uri.contains(%22?%22)) %7B%0A            return Arrays.stream(uri.split(%22%5C%5C?%22))%0A                    .filter(it -%3E it.contains(%22=%22))%0A                    .flatMap(it -%3E Arrays.stream(it.split(%22&%22)))%0A                    .map(it -%3E it.split(%22=%22, 2))%0A                    .collect(toMap(entry -%3E entry%5B0%5D, entry -%3E entry%5B1%5D));%0A        %7D else %7B%0A            return Collections.emptyMap();%0A        %7D%0A    %7D
 %0A%7D%0A

Determine the correct context and query parameters from a context URI. This resolves #37