// Copyright (C) 2012 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.client.changes;

import com.google.gerrit.client.rpc.NativeString;
import com.google.gerrit.client.rpc.RestApi;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwtjsonrpc.common.AsyncCallback;

/**
 * A collection of static methods which work on the Gerrit REST API for specific
 * changes.
 */
public class ChangeApi {
  /** Abandon the change, ending its review. */
  public static void abandon(int id, String msg, AsyncCallback<ChangeInfo> cb) {
    Input input = Input.create();
    input.message(emptyToNull(msg));
    api(id, "abandon").data(input).post(cb);
  }

  /** Create a new change that reverts the delta caused by this change. */
  public static void revert(int id, String msg, AsyncCallback<ChangeInfo> cb) {
    Input input = Input.create();
    input.message(emptyToNull(msg));
    api(id, "revert").data(input).post(cb);
  }

  /** Update the topic of a change. */
  public static void topic(int id, String topic, String msg, AsyncCallback<String> cb) {
    Input input = Input.create();
    input.topic(emptyToNull(topic));
    input.message(emptyToNull(msg));
    api(id, "topic").data(input).put(NativeString.unwrap(cb));
  }

  private static class Input extends JavaScriptObject {
    final native void topic(String t) /*-{ if(t)this.topic=t; }-*/;
    final native void message(String m) /*-{ if(m)this.message=m; }-*/;

    static Input create() {
      return (Input) JavaScriptObject.createObject();
    }

    protected Input() {
    }
  }

  private static RestApi api(int id, String action) {
    // TODO Switch to triplet project~branch~id format in URI.
    return new RestApi("/changes/" + id + "/" + action);
  }

  private static String emptyToNull(String str) {
    return str == null || str.isEmpty() ? null : str;
  }
}

@@ -1615,32 +1615,171 @@
 k%3CString%3E cb) %7B%0A
+    RestApi call = api(id, %22topic%22);%0A    topic = emptyToNull(topic);%0A    msg = emptyToNull(msg);%0A    if (topic != null %7C%7C msg != null) %7B%0A  
     Input input 
@@ -1788,32 +1788,34 @@
 Input.create();%0A
+  
     input.topic(
@@ -1818,29 +1818,18 @@
 pic(
-emptyToNull(
 topic)
-)
 ;%0A
+  
     
@@ -1846,47 +1846,24 @@
 age(
-emptyToNull(
 msg)
-)
 ;%0A    
-api(id, %22topic%22)
+  call
 .dat
@@ -1901,16 +1901,79 @@
 p(cb));%0A
+    %7D else %7B%0A      call.delete(NativeString.unwrap(cb));%0A    %7D%0A
   %7D%0A%0A  p

Use DELETE to clear a change's topic