package it.near.sdk.Communication;

import android.content.Context;
import android.preference.PreferenceActivity;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.ResponseHandlerInterface;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.HttpEntity;
import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.auth.AuthenticationException;
import cz.msebera.android.httpclient.entity.StringEntity;
import cz.msebera.android.httpclient.message.BasicHeader;
import cz.msebera.android.httpclient.message.BasicHttpResponse;
import it.near.sdk.GlobalConfig;
import it.near.sdk.R;

/**
 * @author cattaneostefano.
 */
public class NearAsyncHttpClient extends AsyncHttpClient {

    public NearAsyncHttpClient() {
        super();
    }

    public RequestHandle nearGet(Context context, String url, ResponseHandlerInterface responseHandler) throws AuthenticationException {
        return super.get(context,
                buildUrl(context, url),
                getHeaders(context),
                null,
                responseHandler);
    }

    public RequestHandle nearPost(Context context, String url, String requestBody, ResponseHandlerInterface responseHandler) throws AuthenticationException, UnsupportedEncodingException {
        HttpEntity body = new StringEntity(requestBody);
        return super.post(context,
                buildUrl(context, url),
                getHeaders(context),
                body,
                Constants.Headers.jsonApiHeader,
                responseHandler);
    }

    public RequestHandle nearPut(Context context, String url, String requestBody, ResponseHandlerInterface responseHandler) throws UnsupportedEncodingException, AuthenticationException {
        HttpEntity body = new StringEntity(requestBody);
        return super.put(context,
                buildUrl(context, url),
                getHeaders(context),
                body,
                Constants.Headers.jsonApiHeader,
                responseHandler);
    }

    public RequestHandle nearMock(Context context, String url, JsonHttpResponseHandler responseHandlerInterface, int mockResId){
        String mockedString = context.getApplicationContext().getResources().getString(mockResId);
        JSONObject mockedResponse = null;
        try {
            mockedResponse = new JSONObject(mockedString);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        responseHandlerInterface.onSuccess(200, null, mockedResponse);
        return null;
    }

    public RequestHandle nearMock(Context context, String url, String requestBody, JsonHttpResponseHandler responseHandlerInterface, int mockResId){
        return nearMock(context, url, responseHandlerInterface, mockResId);
    }

    /**
 * Return headers for HTTP calls
 * @return a map of headers
 */

   /* @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map headers = new HashMap();
        headers.put(Constants.Headers.accessToken, "bearer " + GlobalConfig.getInstance(mContext).getApiKey());
        headers.put(Constants.Headers.contentType, Constants.Headers.jsonApiHeader);
        headers.put(Constants.Headers.accept, Constants.Headers.jsonApiHeader);
        return headers;
    }*/



    private Header[] getHeaders(Context context) throws AuthenticationException {
        return new Header[]{
                new BasicHeader(Constants.Headers.accessToken, "bearer " + GlobalConfig.getInstance(context).getApiKey()),
                new BasicHeader(Constants.Headers.contentType, Constants.Headers.jsonApiHeader),
                new BasicHeader(Constants.Headers.accept, Constants.Headers.jsonApiHeader)
        };
    }

    private String buildUrl(Context context, String relativeUrl){
        String baseUrl = context.getResources().getString(R.string.API_BASE_URL) + relativeUrl;

        return baseUrl.replace("%5B", "[").replace("%5D", "]");
    }

}

@@ -61,16 +61,42 @@
 ontext;%0A
+import android.os.Looper;%0A
 import a
@@ -336,16 +336,62 @@
 terface;
+%0Aimport com.loopj.android.http.SyncHttpClient;
 %0A%0Aimport
@@ -1011,34 +1011,83 @@
 ent 
-extends AsyncHttpClient %7B%0A
+%7B%0A%0A    public static AsyncHttpClient syncHttpClient = new SyncHttpClient();
 %0A   
@@ -1094,20 +1094,23 @@
  public 
-Near
+static 
 AsyncHtt
@@ -1120,35 +1120,50 @@
 ient
-() %7B%0A        super();%0A    %7D
+ asyncHttpClient = new AsyncHttpClient();%0A
 %0A%0A  
@@ -1163,32 +1163,39 @@
 );%0A%0A%0A    public 
+static 
 RequestHandle ne
@@ -1319,21 +1319,27 @@
  return 
-super
+getClient()
 .get(con
@@ -1481,35 +1481,215 @@
 r);%0A    %7D%0A%0A    p
-ubl
+rivate static AsyncHttpClient getClient() %7B%0A        if (Looper.myLooper() == null)%7B%0A            return syncHttpClient;%0A        %7D%0A        return asyncHttpClient;%0A    %7D%0A%0A    public stat
 ic RequestHandle
@@ -1924,21 +1924,27 @@
  return 
-super
+getClient()
 .post(co
@@ -2142,32 +2142,39 @@
   %7D%0A%0A    public 
+static 
 RequestHandle ne
@@ -2409,13 +2409,19 @@
 urn 
-super
+getClient()
 .put
@@ -3893,16 +3893,23 @@
 private 
+static 
 Header%5B%5D
@@ -4340,16 +4340,23 @@
 private 
+static 
 String b

Use sync/async http client based on thread context, everything is static