package com.culqi.util;

import com.culqi.Culqi;
import com.culqi.model.Config;
import okhttp3.*;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by culqi on 1/16/17.
 */
public class ResponseHelper {

    public ResponseHelper(){}

    Config config = new Config();

    OkHttpClient client = new OkHttpClient.Builder().connectTimeout(180, TimeUnit.SECONDS)
                          .readTimeout(180, TimeUnit.SECONDS).build();

    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    public String list(String url, String params) throws Exception {

        HttpUrl.Builder builder = new HttpUrl.Builder();

        builder.scheme("https").host(Config.DOMAIN).addPathSegment("v2/" + url);

        if (params != null) {

            HashMap<String, Object> map = new HashMap<String, Object>();

            String[] pairs = params.replace("{", "").replace("}", "").split(",");

            for (int i = 0; i < pairs.length; i++) {
                String pair = pairs[i];
                String[] keyValue = pair.split(":");
                map.put(keyValue[0], keyValue[1]);
            }

            Iterator it = map.entrySet().iterator();

            while (it.hasNext()) {
                Map.Entry pair = (Map.Entry) it.next();
                builder.addQueryParameter(pair.getKey().toString(), pair.getValue().toString());
                it.remove();
            }

        }

        HttpUrl urlquery = builder.build();

        String queryParams = (urlquery.url().getQuery() != null) ? "?"+urlquery.url().getQuery().replace("%22","") : "";

        String finalURL = Config.API_BASE+"/"+url+queryParams;

        Request request = new Request.Builder()
                .url(finalURL)
                .header("Authorization","Bearer " + Culqi.secret_key)
                .build();

        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    public String create(String url, String jsonData) throws Exception {
        String api_key = url.contains("tokens")?  Culqi.public_key: Culqi.secret_key;
        RequestBody body = RequestBody.create(JSON, jsonData);
        Request request = new Request.Builder()
                .url(config.API_BASE+url)
                .header("Authorization","Bearer " + api_key)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    public String update(String url, String jsonData, String id) throws Exception {
        RequestBody body = RequestBody.create(JSON, jsonData);
        Request request = new Request.Builder()
                .url(config.API_BASE+url+id)
                .header("Authorization","Bearer " + Culqi.secret_key)
                .patch(body)
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    public String get_or_delete(String url, String id, boolean delete) throws Exception {
        Request.Builder builder = new Request.Builder();
        builder.url(config.API_BASE+url+id);
        builder.header("Authorization","Bearer " + Culqi.secret_key);

        if(delete){
            builder.delete();
        }

        Request request = builder.build();

        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    public String capture(String url, String id) throws Exception {
        RequestBody body = RequestBody.create(JSON, "");
        Request.Builder builder = new Request.Builder();
        builder.url(config.API_BASE+url+id+"/capture/");
        builder.header("Authorization","Bearer " + Culqi.secret_key);
        builder.post(body);
        Request request = builder.build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

}

@@ -1183,21 +1183,55 @@
 e%5B0%5D
-, keyValue%5B1%5D
+.replace(%22%5C%22%22,%22%22), keyValue%5B1%5D.replace(%22%5C%22%22,%22%22)
 );%0A 
@@ -1260,140 +1260,64 @@
     
-Iterator it = map.e
+for (Map.E
 ntry
-Set().iterator();%0A%0A            while (it.hasNext()) %7B%0A                Map.Entry pair = (Map.E
+%3CString, Object%3E entry : map.e
 ntry
-) it.next();
+Set()) %7B
 %0A   
@@ -1355,20 +1355,21 @@
 rameter(
-pair
+entry
 .getKey(
@@ -1373,25 +1373,15 @@
 ey()
-.toString(), pair
+, entry
 .get
@@ -1405,37 +1405,8 @@
 ));%0A
-                it.remove();%0A
     
@@ -1476,194 +1476,8 @@
 );%0A%0A
-        String queryParams = (urlquery.url().getQuery() != null) ? %22?%22+urlquery.url().getQuery().replace(%22%2522%22,%22%22) : %22%22;%0A%0A        String finalURL = Config.API_BASE+%22/%22+url+queryParams;%0A%0A
     
@@ -1545,16 +1545,16 @@
 url(
-finalURL
+urlquery
 )%0A  
@@ -1744,32 +1744,33 @@
 ody().string();%0A
+%0A
     %7D%0A%0A    publi

improve list of data ResponseHelper.java