package io.sponges.dubtrack4j.internal.request;

import io.sponges.dubtrack4j.DubtrackAPIImpl;
import io.sponges.dubtrack4j.util.Logger;
import okhttp3.*;

import java.io.IOException;
import java.util.Map;

public final class Requester {

    public static final String USER_AGENT = "Mozilla/5.0 Dubtrack4J (github.com/sponges/dubtrack4j)";
    private static final RequestBody EMPTY_BODY = RequestBody.create(null, new byte[0]);

    private final DubtrackAPIImpl dubtrack;
    private final OkHttpClient client;

    public Requester(DubtrackAPIImpl dubtrack) {
        this.dubtrack = dubtrack;
        this.client = dubtrack.getHttpClient();
    }

    public Response get(String url) throws IOException {
        Request.Builder builder = new Request.Builder()
                .url(url)
                .get();

        builder = appendCookies(builder);
        return request(builder.build());
    }

    public Response post(String url) throws IOException {
        Request.Builder builder = new Request.Builder()
                .url(url)
                .post(EMPTY_BODY);

        builder = appendCookies(builder);
        return request(builder.build());
    }

    public Response post(String url, Map<String, String> data) throws IOException {
        FormBody.Builder builder = new FormBody.Builder();

        for (Map.Entry<String, String> entry : data.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();

            builder.addEncoded(key, value);
        }

        RequestBody body = builder.build();

        Request.Builder b = new Request.Builder()
                .url(url)
                .addHeader("Content-Type", body.contentType().type())
                .addHeader("Content-Length", String.valueOf(body.contentLength()))
                .post(body);

        b = appendCookies(b);
        return request(b.build());
    }

    public Response post(String url, Map<String, String> data, Map<String, String> headers) throws IOException {
        FormBody.Builder builder = new FormBody.Builder();

        for (Map.Entry<String, String> entry : data.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();

            builder.addEncoded(key, value);
        }

        RequestBody body = builder.build();

        Request.Builder b = new Request.Builder()
                .url(url)
                .addHeader("Content-Type", body.contentType().type())
                .addHeader("Content-Length", String.valueOf(body.contentLength()))
                .post(body);

        for (Map.Entry<String, String> entry : headers.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();

            b.addHeader(key, value);
        }

        b = appendCookies(b);
        return request(b.build());
    }

    private Response request(Request request) throws IOException {
        Response response = client.newCall(request).execute();
        Logger.debug("Got " + response.code() + " for " + request.method().toUpperCase() + " " + request.url());
        return response;
    }

    private Request.Builder appendCookies(Request.Builder builder) {
        String token = dubtrack.getAccount().getToken();

        builder.addHeader("User-Agent", USER_AGENT);

        if (token == null) {
            return builder;
        } else {
            builder.addHeader("Cookie", "connect.sid=" + token);
            return builder;
        }
    }

}

@@ -1262,634 +1262,35 @@
     
-FormBody.Builder builder = new FormBody.Builder();%0A%0A        for (Map.Entry%3CString, String%3E entry : data.entrySet()) %7B%0A            String key = entry.getKey();%0A            String value = entry.getValue();%0A%0A            builder.addEncoded(key, value);%0A        %7D%0A%0A        RequestBody body = builder.build();%0A%0A        Request.Builder b = new Request.Builder()%0A                .url(url)%0A                .addHeader(%22Content-Type%22, body.contentType().type())%0A                .addHeader(%22Content-Length%22, String.valueOf(body.contentLength()))%0A                .post(body);%0A%0A        b = appendCookies(b);%0A        return request(b.build()
+return post(url, data, null
 );%0A 
@@ -1976,24 +1976,59 @@
 ost(body);%0A%0A
+        if (headers != null) %7B%0A    
         for 
@@ -2076,32 +2076,36 @@
 s.entrySet()) %7B%0A
+    
             Stri
@@ -2133,32 +2133,36 @@
 ();%0A            
+    
 String value = e
@@ -2191,16 +2191,20 @@
         
+   
+ 
 b.addHea
@@ -2212,32 +2212,46 @@
 er(key, value);%0A
+            %7D%0A
         %7D%0A%0A     

Simplify Requester#post methods