package net.gpedro.integrations.slack;

import com.google.gson.JsonObject;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class SlackApi {

    private String service;

    public SlackApi(String service) {
        if (service == null) {
            throw new IllegalArgumentException(
                    "Missing WebHook URL Configuration @ SlackApi");
        } else
            if (!service.startsWith("https://hooks.slack.com/services/")) {
                throw new IllegalArgumentException(
                        "Invalid Service URL. WebHook URL Format: https://hooks.slack.com/services/{id_1}/{id_2}/{token}");
            }

        this.service = service;
    }

    /**
     * Prepare Message and Send to request
     * 
     * @param message
     */
    public void call(SlackMessage message) {
        if (message != null) {
            this.send(message.prepare());
        }
    }

    /**
     * Send request to WebService
     * 
     * @param message
     * @return String response
     */
    private String send(JsonObject message) {
        URL url;
        HttpURLConnection connection = null;
        try {
            // Create connection
            url = new URL(this.service);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(5000);
            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            String payload = "payload="
                    + URLEncoder.encode(message.toString(), "UTF-8");

            // Send request
            DataOutputStream wr = new DataOutputStream(
                    connection.getOutputStream());
            wr.writeBytes(payload);
            wr.flush();
            wr.close();

            // Get Response
            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuffer response = new StringBuffer();
            while ((line = rd.readLine()) != null) {
                response.append(line);
                response.append('\r');
            }

            rd.close();
            return response.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
    }
}

@@ -534,28 +534,16 @@
   %7D else
-%0A           
  if (!se
@@ -603,36 +603,32 @@
 ) %7B%0A            
-    
 throw new Illega
@@ -667,20 +667,16 @@
         
- 
-   
 %22Invalid
@@ -767,36 +767,32 @@
 ken%7D%22);%0A        
-    
 %7D%0A%0A        this.
@@ -856,53 +856,21 @@
 and 
-S
+s
 end to 
-request%0A     * %0A     * @param message
+Slack
 %0A   
@@ -1013,119 +1013,8 @@
  %7D%0A%0A
-    /**%0A     * Send request to WebService%0A     * %0A     * @param message%0A     * @return String response%0A     */%0A
     
@@ -2038,18 +2038,19 @@
 StringBu
-ff
+ild
 er respo
@@ -2067,18 +2067,19 @@
 StringBu
-ff
+ild
 er();%0A  
@@ -2202,17 +2202,17 @@
 ppend('%5C
-r
+n
 ');%0A    

Tidy up SlackApi