package com.avairebot.orion.requests;

import com.avairebot.orion.contracts.async.Future;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class Request extends Future {
    private final String url;
    private final RequestType type;

    private final Map<String, Object> parameters = new HashMap<>();
    private final Map<String, String> headers = new HashMap<>();

    public Request(String url) {
        this(url, RequestType.GET);
    }

    public Request(String url, RequestType type) {
        this.url = url;
        this.type = type;

        headers.put("User-Agent", "Mozilla/5.0");
    }

    public Request addHeader(String key, String value) {
        headers.put(key, value);
        return this;
    }

    public Request addParameter(String key, Object value) {
        parameters.put(key, value);
        return this;
    }

    @Override
    protected void handle(Consumer success, Consumer<Throwable> failure) {
        try {
            HttpURLConnection con = (HttpURLConnection) buildUrl().openConnection();

            con.setRequestMethod(type.name());

            for (Map.Entry<String, String> entry : headers.entrySet()) {
                con.setRequestProperty(entry.getKey(), entry.getValue());
            }

            String inputLine;
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
            StringBuffer buffer = new StringBuffer();

            while ((inputLine = bufferedReader.readLine()) != null) {
                buffer.append(inputLine);
            }
            bufferedReader.close();

            success.accept(new Response(buffer.toString()));
        } catch (Exception ex) {
            failure.accept(ex);
        }
    }

    private URL buildUrl() throws MalformedURLException {
        return new URL(url + (url.contains("?") ? "" : '?') + buildUrlParameters());
    }

    private String buildUrlParameters() {
        return parameters.entrySet().stream().map(item -> {
            try {
                return String.format("%s=%s", item.getKey(), URLEncoder.encode(item.getValue().toString(), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return String.format("%s=%s", item.getKey(), "invalid-format");
            }
        }).collect(Collectors.joining("&"));
    }
}

@@ -87,73 +87,37 @@
 re;%0A
-%0A
 import 
-java.io.BufferedReader;%0Aimport java.io.InputStreamReader;
+okhttp3.OkHttpClient;%0A
 %0Aimp
@@ -162,43 +162,8 @@
 on;%0A
-import java.net.HttpURLConnection;%0A
 impo
@@ -468,24 +468,115 @@
 Type type;%0A%0A
+    private final OkHttpClient client;%0A    private final okhttp3.Request.Builder builder;%0A%0A
     private 
@@ -871,24 +871,110 @@
 pe = type;%0A%0A
+        client = new OkHttpClient();%0A        builder = new okhttp3.Request.Builder();%0A
         head
@@ -1379,124 +1379,28 @@
     
-HttpURLConnection con = (HttpURLConnection) buildUrl().openConnection();%0A%0A            con.setRequestMethod(type.name
+builder.url(buildUrl
 ());
@@ -1494,30 +1494,25 @@
     
-con.setRequestProperty
+builder.addHeader
 (ent
@@ -1574,403 +1574,217 @@
     
-String inputLine;%0A            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));%0A            StringBuffer buffer = new StringBuffer();%0A%0A            while ((inputLine = bufferedReader.readLine()) != null) %7B%0A                buffer.append(inputLine);%0A            %7D%0A            bufferedReader.close();%0A%0A            success.accept(new Response(buffer.toS
+switch (type) %7B%0A                case GET:%0A                    builder.get();%0A                    break;%0A            %7D%0A%0A            success.accept(new Response(client.newCall(builder.build()).execute().body().s
 trin

Change url request client to use OkHTTP3