package io.rover;

import android.os.AsyncTask;
import android.util.Log;

import org.json.JSONObject;

import io.rover.model.Message;
import io.rover.network.HttpResponse;
import io.rover.network.JsonApiPayloadProvider;
import io.rover.network.NetworkTask;

/**
 * Created by chrisrecalis on 2016-11-29.
 */

public class PatchMessageTask extends AsyncTask<Message, Void, Boolean> {

    public interface Callback {
        void onSuccess();
        void onFailure();
    }

    private Callback mCallback;

    public void setCallback(Callback callback) {
        mCallback = callback;
    }

    @Override
    protected Boolean doInBackground(Message... messages) {

        Log.i("Async", "here");
        if (messages.length == 0)
            return false;

        Message message = messages[0];


        NetworkTask networkTask = Router.getPatchMessageNetworkTask(message.getId());

        if (networkTask == null) {
            return false;
        }

        JsonApiPayloadProvider.JsonApiObjectSerializer serializer = new ObjectSerializer(message, null);
        NetworkTask.PayloadProvider payloadProvider = new JsonApiPayloadProvider(serializer);

        networkTask.setPayloadProvider(payloadProvider);

        HttpResponse response = networkTask.run();

        if (response != null) {
            response.close();
        }

        return response != null && response.isSuccessful();
    }

    @Override
    protected void onPostExecute(Boolean success) {
        if (mCallback != null) {
            if (success) {
                mCallback.onSuccess();
            } else {
                mCallback.onFailure();
            }
        }
    }
}

@@ -661,25 +661,24 @@
 ages) %7B%0A
-%0A
         
 Log.i(%22A
@@ -673,31 +673,8 @@
     
-Log.i(%22Async%22, %22here%22);
 %0A   

remove debug statement