package xyz.pushpad;

import org.json.simple.JSONObject;

/**
 * <p>You can include action buttons inside your notifications. In this way, a notification can have multiple links or actions.</p>
 * <img src="https://pushpad.xyz/assets/action_buttons-a1397c39c36f4ea02a6d9d00132fe3b1be697c138f0db9b11a8310ab558a1719.png" alt="Action buttons">
 * <p>When you send a notification you can add any number of action buttons. Each action button has the following fields:</p>
 * <ul>
 *  <li><b>Title</b>: the label displayed to the user for the action button.</li>
 *  <li><b>Link</b> (optional): the page that opens up when the user clicks the button. If you leave it blank, the main link of the notification will be used.</li>
 *  <li><b>Icon</b> (optional): the URL of a small icon for the action button. An alternative to icons is to include emoticons or other symbols inside the title.</li>
 *  <li><b>Action</b> (optional): an ID for the action that can be used to trigger a Javascript callback. If a Javascript action with that ID is defined, it will be executed. Otherwise the action ID will be ignored and the link will be used instead.</li>
 * </ul> 
 * @see <a href="https://pushpad.xyz/docs/action_buttons">https://pushpad.xyz/docs/action_buttons</a>
 */
public class ActionButton {

  /**
   * Creates a new ActionButtonBuilder.
   *
   * @param title
   *          the title of the ActionButton
   * @return the ActionButtonBuilder
   */
  public static ActionButtonBuilder newBuilder(String title) {
    return new ActionButtonBuilder(title);
  }

  /** The title. */
  private final String title;

  /** The link. */
  private String targetUrl;

  /** The icon. */
  private String icon;

  /** The action. */
  private String action;

  /**
   * Instantiates a new action button.
   *
   * @param title
   *          the title
   * @throws IllegalArgumentException
   *           if the new title exceeds the maximum length of 20 characters
   */
  public ActionButton(String title) throws IllegalArgumentException {
    if (title.length()>20){
      throw new IllegalArgumentException("Maximum length of the title is 20 characters");
    }
    this.title = title;
  }

  /**
   * Sets the target url.
   *
   * @param targetUrl
   *          the new target url
   */
  public void setTargetUrl(String targetUrl) {
    this.targetUrl = targetUrl;
  }

  /**
   * Sets the icon url.
   *
   * @param icon
   *          the new icon url
   */
  public void setIcon(String icon) {
    this.icon = icon;
  }

  /**
   * Sets the action.
   *
   * @param action
   *          the new action
   * @throws IllegalArgumentException
   *           if the new action exceeds the maximum length of 50 characters
   */
  public void setAction(String action) throws IllegalArgumentException {
    if (action.length() > 50) {
      throw new IllegalArgumentException("Maximum length of the action is 50 characters");
    }
    this.action = action;
  }

  /**
   * Converts the ActionButton to a JSONObject.
   *
   * @return the JSONObject
   */
  @SuppressWarnings("unchecked")
  public JSONObject toJson() {
    JSONObject result = new JSONObject();
    result.put("title", title);
    result.put("target_url", targetUrl);
    result.put("icon", icon);
    result.put("action", action);
    return result;
  }

  /**
   * ActionButtonBuilders can be used to chain the setter methods of an ActionButton.
   */
  public static class ActionButtonBuilder {

    /** The underlying action button. */
    private ActionButton actionButton;

    /**
     * Instantiates a new ActionButtonBuilder.
     *
     * @param title
     *          the title of the ActionButton
     */
    private ActionButtonBuilder(String title) {
      actionButton = new ActionButton(title);
    }

    /**
     * Sets the target url.
     *
     * @param targetUrl
     *          the new target url
     * @return the ActionButtonBuilder
     */
    public ActionButtonBuilder setTargetUrl(String targetUrl) {
      actionButton.setTargetUrl(targetUrl);
      return this;
    }

    /**
     * Sets the icon url.
     *
     * @param icon
     *          the new icon url
     * @return the ActionButtonBuilder
     */
    public ActionButtonBuilder setIcon(String icon) {
      actionButton.setIcon(icon);
      return this;
    }

    /**
     * Sets the action.
     *
     * @param action
     *          the new action
     * @return the ActionButtonBuilder
     */
    public ActionButtonBuilder setAction(String action) {
      actionButton.setAction(action);
      return this;
    }

    /**
     * Returns the underlying Actionbutton.
     *
     * @return the ActionButton
     */
    public ActionButton build() {
      return actionButton;
    }
  }
}

@@ -1285,275 +1285,8 @@
  %7B%0A%0A
-  /**%0A   * Creates a new ActionButtonBuilder.%0A   *%0A   * @param title%0A   *          the title of the ActionButton%0A   * @return the ActionButtonBuilder%0A   */%0A  public static ActionButtonBuilder newBuilder(String title) %7B%0A    return new ActionButtonBuilder(title);%0A  %7D%0A%0A
   /*
@@ -1963,21 +1963,16 @@
 argetUrl
-%0A   *
         
@@ -1992,39 +1992,88 @@
 get url%0A   *
-/%0A  public void
+ @return a reference to this object.%0A   */%0A  public ActionButton
  setTargetUr
@@ -2126,16 +2126,33 @@
 getUrl;%0A
+    return this;%0A
   %7D%0A%0A  /
@@ -2231,39 +2231,88 @@
 con url%0A   *
-/%0A  public void
+ @return a reference to this object.%0A   */%0A  public ActionButton
  setIcon(Str
@@ -2341,24 +2341,41 @@
 con = icon;%0A
+    return this;%0A
   %7D%0A%0A  /**%0A 
@@ -2444,24 +2444,65 @@
  new action%0A
+   * @return a reference to this object.%0A
    * @throws
@@ -2623,12 +2623,20 @@
 lic 
-void
+ActionButton
  set
@@ -2846,16 +2846,33 @@
 action;%0A
+    return this;%0A
   %7D%0A%0A  /
@@ -3230,1436 +3230,7 @@
 %0A  %7D
-%0A%0A  /**%0A   * ActionButtonBuilders can be used to chain the setter methods of an ActionButton.%0A   */%0A  public static class ActionButtonBuilder %7B%0A%0A    /** The underlying action button. */%0A    private ActionButton actionButton;%0A%0A    /**%0A     * Instantiates a new ActionButtonBuilder.%0A     *%0A     * @param title%0A     *          the title of the ActionButton%0A     */%0A    private ActionButtonBuilder(String title) %7B%0A      actionButton = new ActionButton(title);%0A    %7D%0A%0A    /**%0A     * Sets the target url.%0A     *%0A     * @param targetUrl%0A     *          the new target url%0A     * @return the ActionButtonBuilder%0A     */%0A    public ActionButtonBuilder setTargetUrl(String targetUrl) %7B%0A      actionButton.setTargetUrl(targetUrl);%0A      return this;%0A    %7D%0A%0A    /**%0A     * Sets the icon url.%0A     *%0A     * @param icon%0A     *          the new icon url%0A     * @return the ActionButtonBuilder%0A     */%0A    public ActionButtonBuilder setIcon(String icon) %7B%0A      actionButton.setIcon(icon);%0A      return this;%0A    %7D%0A%0A    /**%0A     * Sets the action.%0A     *%0A     * @param action%0A     *          the new action%0A     * @return the ActionButtonBuilder%0A     */%0A    public ActionButtonBuilder setAction(String action) %7B%0A      actionButton.setAction(action);%0A      return this;%0A    %7D%0A%0A    /**%0A     * Returns the underlying Actionbutton.%0A     *%0A     * @return the ActionButton%0A     */%0A    public ActionButton build() %7B%0A      return actionButton;%0A    %7D%0A  %7D
 %0A%7D%0A

remove ActionButtonBuilder