package com.github.paweladamski;

import com.github.paweladamski.condition.*;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import static com.github.paweladamski.Rule.NOT_FOUND;
import static org.hamcrest.Matchers.equalTo;

public class HttpClientMock extends CloseableHttpClient {

    private final List<Rule> rules = new ArrayList<>();
    private final String host;
    private final List<Request> requests = new ArrayList<>();

    public HttpClientMock() {
        this("");
    }

    public HttpClientMock(String host) {
        this.host = host;
    }

    public HttpClientMockBuilder onGet(String url) {
        return newRule("GET", url);
    }

    public HttpClientMockBuilder onPost(String url) {
        return newRule("POST", url);
    }

    public HttpClientMockBuilder onPut(String url) {
        return newRule("PUT", url);
    }

    public HttpClientMockBuilder onDelete(String url) {
        return newRule("DELETE", url);
    }

    public HttpClientMockBuilder onHead(String url) {
        return newRule("HEAD", url);
    }

    public HttpClientMockBuilder onOptions(String url) {
        return newRule("OPTIONS", url);
    }

    public HttpClientMockBuilder onPatch(String url) {
        return newRule("PATCH", url);
    }

    private HttpClientMockBuilder newRule(String method, String urlText) {
        UrlParser urlParser = new UrlParser();
        Rule r = new Rule();
        r.addCondition(new HttpMethodCondition(method));
        List<Condition> urlConditions = urlParser.parse(host+urlText);
        r.addConditions(urlConditions);
        return new HttpClientMockBuilder(r);
    }

    @Override
    protected CloseableHttpResponse doExecute(HttpHost httpHost, HttpRequest httpRequest, HttpContext httpContext) throws IOException {
        Request request = new Request(httpHost, httpRequest, httpContext);
        requests.add(request);
        Rule rule = rules.stream()
                .filter(r -> r.matches(httpHost, httpRequest, httpContext))
                .reduce((a, b) -> b)
                .orElse(NOT_FOUND);
        HttpResponse response = rule.nextResponse(request);
        return new HttpResponseProxy(response);
    }

    @Override
    public void close() throws IOException {

    }

    @Override
    public HttpParams getParams() {
        return null;
    }

    @Override
    public ClientConnectionManager getConnectionManager() {
        return null;
    }

    public HttpClientVerifyBuilder verify() {
        return new HttpClientVerifyBuilder(host, requests);
    }


}

@@ -72,9 +72,79 @@
 ion.
-*
+Condition;%0Aimport com.github.paweladamski.condition.HttpMethodCondition
 ;%0Aim
@@ -251,46 +251,8 @@
 se;%0A
-import org.apache.http.NameValuePair;%0A
 impo
@@ -312,61 +312,8 @@
 se;%0A
-import org.apache.http.client.utils.URLEncodedUtils;%0A
 impo
@@ -537,101 +537,8 @@
 on;%0A
-import java.net.MalformedURLException;%0Aimport java.net.URL;%0Aimport java.nio.charset.Charset;%0A
 impo
@@ -642,53 +642,8 @@
 UND;
-%0Aimport static org.hamcrest.Matchers.equalTo;
 %0A%0Apu
@@ -1944,9 +1944,11 @@
 host
-+
+ + 
 urlT
@@ -1989,24 +1989,46 @@
 onditions);%0A
+        rules.add(r);%0A
         retu
@@ -2979,14 +2979,13 @@
 s);%0A
+
     %7D%0A%0A
-%0A
 %7D%0A

Fix no rules bug