package it.near.sdk.communication;

import android.net.Uri;
import android.support.annotation.NonNull;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.auth.AuthenticationException;
import it.near.sdk.GlobalConfig;
import it.near.sdk.logging.NearLog;
import it.near.sdk.operation.NearItUserProfile;

/**
 * @author federico.boschini
 */

public class OptOutAPI {

    private static final String TAG = "OptOutAPI";
    private static final String PLUGIN_NAME = "congrego";
    private static final String PROFILE_RES_TYPE = "profiles";
    private static final String OPTOUT_RES_TYPE = "optout";

    private final NearAsyncHttpClient httpClient;
    private final GlobalConfig globalConfig;

    public OptOutAPI(NearAsyncHttpClient httpClient, GlobalConfig globalConfig) {
        this.httpClient = httpClient;
        this.globalConfig = globalConfig;
    }

    public void optOut(@NonNull final OptOutNotifier optOutNotifier) {

        String profileId = globalConfig.getProfileId();

        if (profileId == null) {
            optOutNotifier.onFailure("profile id is null, can't opt-out");
            return;
        }

        Uri url = Uri.parse(Constants.API.PLUGINS_ROOT).buildUpon()
                .appendPath(PLUGIN_NAME)
                .appendPath(PROFILE_RES_TYPE)
                .appendPath(profileId)
                .appendPath(OPTOUT_RES_TYPE).build();

        try {
            httpClient.nearDelete(url.toString(), new NearJsonHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                    NearLog.d(TAG, "Profile opted out: " + response.toString());
                    optOutNotifier.onSuccess();
                    globalConfig.setOptOut();
                }

                @Override
                public void onFailureUnique(int statusCode, Header[] headers, Throwable throwable, String responseString) {
                    optOutNotifier.onFailure("network error: " + statusCode);
                }
            });
        } catch (AuthenticationException | UnsupportedEncodingException e) {
            optOutNotifier.onFailure("error: impossible to send request");
        }

    }

}

@@ -699,16 +699,73 @@
 tout%22;%0A%0A
+    private static final int OPTOUT_SUCCESS_CODE = 202;%0A%0A
     priv
@@ -1860,30 +1860,30 @@
         
-o
+O
 ptOut
-Notifier
+API.this
 .onSucce
@@ -1889,54 +1889,22 @@
 ess(
-);%0A                    globalConfig.setOptOut(
+optOutNotifier
 );%0A 
@@ -2067,24 +2067,183 @@
 seString) %7B%0A
+                    if(statusCode == OPTOUT_SUCCESS_CODE) %7B%0A                        OptOutAPI.this.onSuccess(optOutNotifier);%0A                    %7D else %7B%0A    
             
@@ -2308,16 +2308,38 @@
 sCode);%0A
+                    %7D%0A
         
@@ -2534,10 +2534,147 @@
 %0A    %7D%0A%0A
+    private void onSuccess(OptOutNotifier optOutNotifier) %7B%0A        optOutNotifier.onSuccess();%0A        globalConfig.setOptOut();%0A    %7D%0A%0A
 %7D%0A

manage success with code 202 and empty body