package com.worldline.openxcmanagers.sdk;

import com.worldline.openxcmanagers.sdk.service.OpenXCService;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class ApiClient {

    private static ApiClient INSTANCE;
    private final ApiClientProvider clientProvider;
    private RestAdapter.LogLevel logLevel;
    private RestAdapter.Log log;

    public static ApiClient init(ApiClientProvider clientProvider) {
        if (clientProvider == null) {
            throw new IllegalArgumentException("ApiClientProvider can not be null");
        }

        if (INSTANCE == null) {
            INSTANCE = new ApiClient(clientProvider);
        }
        return INSTANCE;
    }

    public static ApiClient getInstance() {
        if (INSTANCE == null) {
            throw new IllegalArgumentException("ApiClient should be initialized before call getInstance()");
        }
        return INSTANCE;
    }

    private ApiClient(ApiClientProvider apiClientProvider) {
        clientProvider = apiClientProvider;
    }

    public void getData(Callback<OpenXCResponse> callback) {
        RestAdapter restAdapter = createRestAdapter();
        restAdapter.create(OpenXCService.class).getData(callback);
    }

    public void postData(String key, String value, Callback<Response> callback) {
        RestAdapter restAdapter = createRestAdapter();
        restAdapter.create(OpenXCService.class).postData(key, value, callback);
    }

    public void customMessage(String name, String value, String event, Callback<Response> callback) {
        RestAdapter restAdapter = createRestAdapter();
        restAdapter.create(OpenXCService.class).customMessage(name, value, event, callback);
    }

    private RestAdapter createRestAdapter() {
        RestAdapter.Builder builder = new RestAdapter.Builder();
        builder.setEndpoint(clientProvider);

        if (logLevel != null && log != null) {
            builder.setLogLevel(logLevel);
            builder.setLog(log);
        }

        return builder.build();
    }

    public void setLog(RestAdapter.LogLevel logLevel, RestAdapter.Log log) {
        this.logLevel = logLevel;
        this.log = log;
    }
}

@@ -412,16 +412,53 @@
 Log log;
+%0A    private boolean waitForResponse;
 %0A%0A    pu
@@ -1174,32 +1174,68 @@
 se%3E callback) %7B%0A
+        if (!waitForResponse) %7B%0A    
         RestAdap
@@ -1273,32 +1273,36 @@
 pter();%0A        
+  
+  
 restAdapter.crea
@@ -1336,32 +1336,42 @@
 Data(callback);%0A
+        %7D%0A
     %7D%0A%0A    publi
@@ -1435,32 +1435,64 @@
 se%3E callback) %7B%0A
+        waitForResponse = true;%0A
         RestAdap
@@ -1599,16 +1599,33 @@
  value, 
+new WaitCallback(
 callback
@@ -1617,32 +1617,33 @@
 llback(callback)
+)
 ;%0A    %7D%0A%0A    pub
@@ -1729,32 +1729,64 @@
 se%3E callback) %7B%0A
+        waitForResponse = true;%0A
         RestAdap
@@ -1906,16 +1906,33 @@
  event, 
+new WaitCallback(
 callback
@@ -1928,24 +1928,25 @@
 ck(callback)
+)
 ;%0A    %7D%0A%0A   
@@ -2379,24 +2379,24 @@
 = logLevel;%0A
-
         this
@@ -2412,11 +2412,679 @@
 g;%0A    %7D
+%0A%0A   private class WaitCallback implements Callback%3CResponse%3E %7B%0A       private final Callback%3CResponse%3E callback;%0A%0A       public WaitCallback(Callback%3CResponse%3E callback) %7B%0A           this.callback = callback;%0A       %7D%0A%0A       @Override%0A        public void success(Response response, Response response2) %7B%0A            waitForResponse = false;%0A           if (callback != null) %7B%0A               callback.success(response, response2);%0A           %7D%0A        %7D%0A%0A        @Override%0A        public void failure(RetrofitError error) %7B%0A            waitForResponse = false;%0A            if (callback != null) %7B%0A                callback.failure(error);%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

Fix for weird behavior on set data