package com.example.ssteeve.dpd_android;

import android.support.annotation.Nullable;

import org.json.JSONObject;

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

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by ssteeve on 11/1/16.
 */
public class DPDRequest {

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

    static void makeRequest(String endpoint,
                                  @Nullable Map<String, Object> params,
                                  String  method,
                                  @Nullable String jsonBody,
                                  @Nullable Dictionary<String, Object> requestHeader,
                            @Nullable Class mappableObject, RequestCallBack requestCallBack) {

        String url = DPDConstants.sRootUrl + endpoint;
        if (params != null && params.size() > 0) {
            String paramString = new JSONObject(params).toString();
            url = url + "?" + paramString;
        }


        Request.Builder requestBuilder = new Request.Builder();
        requestBuilder.addHeader("Content-Type", "application/json");
        requestBuilder.addHeader("Accept-Encoding", "gzip");

        if (DPDCredentials.getInstance().getAccessToken() != null)
            requestBuilder.addHeader("accessToken", DPDCredentials.getInstance().getAccessToken());

        if (DPDCredentials.getInstance().getSessionId() != null)
            requestBuilder.addHeader("Cookie", DPDCredentials.getInstance().getSessionId());

        if (requestHeader != null && requestHeader.size() > 0) {
            for (Enumeration<String> key = requestHeader.keys(); requestHeader.keys().hasMoreElements();) {
                requestBuilder.addHeader(key.toString(), requestHeader.get(key).toString());
            }
        }


        Request request;
        if (method == HTTPMethod.POST || method == HTTPMethod.PUT) {
            RequestBody body = RequestBody.create(JSON, jsonBody);
            request = requestBuilder
                    .url(url)
                    .post(body)
                    .build();
        } else {
            request = requestBuilder
                    .url(url)
                    .build();
        }

        new BackendOperation(DPDConstants.sRootUrl, request, requestBuilder, requestCallBack);
    }
}
@@ -2053,41 +2053,277 @@
     
-if (method == HTTPMethod.POST %7C%7C 
+RequestBody body = RequestBody.create(JSON, jsonBody != null ? jsonBody : %22%22);%0A%0A        if (method == HTTPMethod.POST) %7B%0A%0A            request = requestBuilder%0A                    .url(url)%0A                    .post(body)%0A                    .build();%0A        %7D else if(
 meth
@@ -2362,62 +2362,173 @@
     
-R
+r
 equest
-Body body
  = 
-R
+r
 equestB
-ody.create(JSON, jsonBody);
+uilder%0A                    .url(url)%0A                    .put(body)%0A                    .build();%0A        %7D else if (method == HTTPMethod.DELETE) %7B
 %0A   
@@ -2604,36 +2604,38 @@
                .
-post
+delete
 (body)%0A         

fix for request builder when deleting an object and updating an object.