package chat.rocket.android.api;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

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

import bolts.Task;
import chat.rocket.android.realm_helper.RealmHelper;

public class PushHelper extends MethodCallHelper {
  public PushHelper(Context context, String serverConfigId) {
    super(context, serverConfigId);
  }

  public PushHelper(RealmHelper realmHelper,
                    DDPClientWrapper ddpClient) {
    super(realmHelper, ddpClient);
  }

  public Task<Void> pushUpdate(@NonNull String pushId, @NonNull String token,
                               @Nullable String userId) {
    return call("raix:push-update", TIMEOUT_MS, () -> {
      JSONObject param = new PushUpdate(pushId, token, userId).toJson();
      return new JSONArray().put(param);
    }).onSuccessTask(task -> Task.forResult(null));
  }

  public Task<Void> pushSetUser(String pushId) {
    return call("raix:push-setuser", TIMEOUT_MS, () -> new JSONArray().put(pushId))
        .onSuccessTask(task -> Task.forResult(null));
  }

  private static class PushUpdate {

    private String pushId;
    private String gcmToken;
    private String userId;

    PushUpdate(@NonNull String pushId, @NonNull String gcmToken, @Nullable String userId) {
      this.pushId = pushId;
      this.gcmToken = gcmToken;
      this.userId = userId;
    }

    JSONObject toJson() throws JSONException {
      JSONObject param = new JSONObject();
      param.put("id", pushId);
      param.put("appName", "main");
      param.put("userId", userId != null ? userId : JSONObject.NULL);
      param.put("metadata", new JSONObject());

      JSONObject tokenParam = new JSONObject();
      tokenParam.put("gcm", gcmToken);

      param.put("token", tokenParam);

      return param;
    }
  }
}

@@ -783,56 +783,53 @@
 _MS,
- () -%3E %7B%0A      JSONObject param = new PushUpdate
+%0A        () -%3E new JSONArray().put(new Params
 (pus
@@ -860,57 +860,19 @@
 on()
-;
+))
 %0A      
-return new JSONArray().put(param);%0A    %7D)
+  
 .onS
@@ -1134,25 +1134,21 @@
  class P
-ushUpdate
+arams
  %7B%0A%0A    
@@ -1235,18 +1235,21 @@
     
-PushUpdate
+public Params
 (@No
@@ -1417,20 +1417,27 @@
 %0A    %7D%0A%0A
-
     
+public 
 JSONObje

rename PushHelper::PushUpdate to PushHelper::Params