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 Slack
     */
    public void call(SlackMessage message) {
        if (message != null) {
            this.send(message.prepare());
        }
    }

    private String send(JsonObject message) {
        HttpURLConnection connection = null;
        try {
            // Create connection
            final URL url = new URL(this.service);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(5000);
            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

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

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

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

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

@@ -321,62 +321,389 @@
 ate 
-String service;%0A%0A    public SlackApi(String service) %7B
+static final String POST = %22POST%22;%0A    private static final String PAYLOAD = %22payload=%22;%0A    private static final String UTF_8 = %22UTF-8%22;%0A%0A    private final String service;%0A    private final int timeout;%0A%0A    public SlackApi(String service) %7B%0A        this(service, 5000);%0A    %7D%0A%0A    public SlackApi(String service,%0A                    int timeout) %7B%0A        this.timeout = timeout;
 %0A   
@@ -1632,22 +1632,20 @@
 tMethod(
-%22
 POST
-%22
 );%0A     
@@ -1680,20 +1680,23 @@
 Timeout(
-5000
+timeout
 );%0A     
@@ -1853,26 +1853,23 @@
 yload = 
-%22payload=%22
+PAYLOAD
 %0A       
@@ -1921,23 +1921,21 @@
 ring(), 
-%22
 UTF
--8%22
+_8
 );%0A%0A    

Make timeout configurable