/*
 * The MIT License
 *
 * Copyright (c) 2016 Marcelo "Ataxexe" Guimarães <ataxexe@devnull.tools>
 *
 * ----------------------------------------------------------------------
 * Permission  is hereby granted, free of charge, to any person obtaining
 * a  copy  of  this  software  and  associated  documentation files (the
 * "Software"),  to  deal  in the Software without restriction, including
 * without  limitation  the  rights to use, copy, modify, merge, publish,
 * distribute,  sublicense,  and/or  sell  copies of the Software, and to
 * permit  persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The  above  copyright  notice  and  this  permission  notice  shall be
 * included  in  all  copies  or  substantial  portions  of the Software.
 *                        -----------------------
 * THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY OF ANY KIND,
 * EXPRESS  OR  IMPLIED,  INCLUDING  BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN  NO  EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM,  DAMAGES  OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT  OR  OTHERWISE,  ARISING  FROM,  OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE   OR   THE   USE   OR   OTHER   DEALINGS  IN  THE  SOFTWARE.
 */

package tools.devnull.jenkins.plugins.buildnotifications;

import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * A class that represents a boteco message.
 */
public class BotecoMessage implements Message {

  private static final Logger LOGGER = Logger.getLogger(BotecoMessage.class.getName());

  private final String endpoint;

  private String content;
  private String title;
  private String url;
  private String urlTitle;

  public BotecoMessage(String eventId, String endpoint) {
    if (endpoint.endsWith("/")) {
      this.endpoint = endpoint + eventId;
    } else {
      this.endpoint = endpoint + "/" + eventId;
    }
  }

  @Override
  public void setContent(String content) {
    this.content = content;
  }

  @Override
  public void setTitle(String title) {
    this.title = title;
  }

  @Override
  public void setUrl(String url, String title) {
    this.url = url;
    this.urlTitle = title;
  }

  @Override
  public void highPriority() {

  }

  @Override
  public void lowPriority() {

  }

  @Override
  public void send() {
    HttpClient client = new HttpClient();
    PostMethod post = new PostMethod(endpoint);
    String message = String.format("[a]%s[/a]: %s [l]%s <%s>[/l]",
        title,
        content,
        urlTitle,
        url);
    post.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    Map<String, String> values = new HashMap<String, String>();
    values.put("text", message);
    try {
      post.setRequestEntity(new StringRequestEntity(JSONObject.fromObject(values).toString(),
          "application/json", "UTF-8"));
      client.executeMethod(post);
    } catch (IOException e) {
      LOGGER.severe("Error while sending notification: " + e.getMessage());
      e.printStackTrace();
    }
  }

}

@@ -2052,24 +2052,35 @@
  String 
-urlTitle
+priority = %22normal%22
 ;%0A%0A  pub
@@ -2533,35 +2533,8 @@
 rl;%0A
-    this.urlTitle = title;%0A
   %7D%0A
@@ -2569,33 +2569,60 @@
 ighPriority() %7B%0A
+    this.priority = %22high%22;
 %0A
-
   %7D%0A%0A  @Override
@@ -2652,17 +2652,43 @@
 ity() %7B%0A
+    this.priority = %22low%22;
 %0A
-
   %7D%0A%0A  @
@@ -2813,139 +2813,8 @@
 t);%0A
-    String message = String.format(%22%5Ba%5D%25s%5B/a%5D: %25s %5Bl%5D%25s %3C%25s%3E%5B/l%5D%22,%0A        title,%0A        content,%0A        urlTitle,%0A        url);%0A
     
@@ -2972,21 +2972,122 @@
 t(%22t
-ext%22, message
+itle%22, title);%0A    values.put(%22content%22, content);%0A    values.put(%22url%22, url);%0A    values.put(%22priority%22, priority
 );%0A 

update boteco integration