package com.enlivenhq.slack;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;

public class SlackWrapper
{
    protected String slackUrl;

    protected String username;

    protected String channel;

    public String send(String project, String build, String statusText, String statusColor) throws IOException
    {
        String formattedPayload = "payload={text: \"" + project + " #" + build + " " + statusText + "\",attachments: [{fallback: \"" + project + " #" + build + " " + statusText + "\",text: \"" + project + " #" + build + " " + statusText + "\",pretext: \"Build Status\",color: \"" + statusColor + "\",fields: [{title: \"Project\",value: \"" + project + "\",short: false},{title: \"Build\",value: \"" + build + "\",short: true},{title: \"Status\",value: \"" + statusText + "\",short: false}]}],channel: \"" + this.getChannel() + "\",username: \"" + this.getUsername() + "\"}";

        URL url = new URL(this.getSlackUrl());
        HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();

        httpsURLConnection.setRequestMethod("POST");
        httpsURLConnection.setRequestProperty("User-Agent", "Enliven");
        httpsURLConnection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
        httpsURLConnection.setDoOutput(true);

        DataOutputStream dataOutputStream = new DataOutputStream(
                httpsURLConnection.getOutputStream()
        );

        dataOutputStream.writeBytes(formattedPayload);
        dataOutputStream.flush();
        dataOutputStream.close();

        InputStream inputStream;
        String responseBody = "";

        try {
            inputStream = httpsURLConnection.getInputStream();
        }
        catch (IOException e) {
            responseBody = e.getMessage() + ": ";
            inputStream = httpsURLConnection.getErrorStream();
            throw new IOException(getResponseBody(inputStream, responseBody));
        }

        return getResponseBody(inputStream, responseBody);
    }

    private String getResponseBody(InputStream inputStream, String responseBody) throws IOException {
        String line;

        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(inputStream)
        );

        while ((line = bufferedReader.readLine()) != null) {
            responseBody += line + "\n";
        }

        bufferedReader.close();
        return responseBody;
    }


    public void setSlackUrl(String slackUrl)
    {
        this.slackUrl = slackUrl;
    }

    public String getSlackUrl()
    {
        return this.slackUrl;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public String getUsername()
    {
        return this.username;
    }

    public void setChannel(String channel)
    {
        this.channel = channel;
    }

    public String getChannel()
    {
        return this.channel;
    }
}

@@ -363,48 +363,21 @@
 ing 
-formattedPayload = %22payload=%7Btext: %5C%22%22 +
+payloadText =
  pro
@@ -418,163 +418,553 @@
 Text
- + %22%5C%22,attachments: %5B%7Bfallback: %5C%22%22 + project + %22 #%22 + build + %22 %22 + statusText + %22%5C%22,
+;%0A        String attachmentProject = %22%7B%5C%22title%5C%22:%5C%22Project%5C%22,%5C%22value%5C%22:%5C%22%22 + project + %22%5C%22,%5C%22short%5C%22: false%7D%22;%0A        String attachmentBuild = %22%7B%5C%22title%5C%22:%5C%22Build%5C%22,%5C%22value%5C%22:%5C%22%22 + build + %22%5C%22,%5C%22short%5C%22: true%7D%22;%0A        String attachmentStatus = %22%7B%5C%22title%5C%22:%5C%22Status%5C%22,%5C%22value%5C%22:%5C%22%22 + statusText + %22%5C%22,%5C%22short%5C%22: false%7D%22;%0A%0A        String formattedPayload = %22payload=%7B%22 +%0A            %22%5C%22
 text
+%5C%22
 :
- 
 %5C%22%22 + p
-roject + %22 #%22 + build + %22 %22 + statusText + %22%5C%22,
+ayloadText + %22%5C%22,%22 +%0A            %22%5C%22attachments%5C%22: %5B%7B%22 +%0A                %22%5C%22fallback%5C%22:%5C%22%22 + payloadText + %22%5C%22,%22 +%0A                %22%5C%22
 pretext
+%5C%22
 :
- 
 %5C%22Bu
@@ -980,15 +980,39 @@
 s%5C%22,
+%22 +%0A                %22%5C%22
 color
+%5C%22
 :
- 
 %5C%22%22 
@@ -1035,209 +1035,242 @@
 %22%5C%22,
+%22 +%0A                %22%5C%22
 fields
+%5C%22
 : %5B
-%7Btitle: %5C%22Project%5C%22,value: %5C%22%22 + project + %22%5C%22,short: false%7D,%7Btitle: %5C%22Build%5C%22,value: %5C%22%22 + build + %22%5C%22,short: true%7D,%7Btitle: %5C%22Status%5C%22,value: %5C%22%22 + statusText + %22%5C%22,short: false%7D%5D%7D%5D,
+%22 +%0A                    attachmentProject + %22,%22 +%0A                    attachmentBuild + %22,%22 +%0A                    attachmentStatus +%0A                %22%5D%22 +%0A            %22%7D%5D,%22 +%0A            %22%5C%22
 channel
+%5C%22
 :
- 
 %5C%22%22 
@@ -1295,24 +1295,43 @@
 ) + %22%5C%22,
+%22 +%0A            %22%5C%22
 username
 : %5C%22%22 + 
@@ -1322,18 +1322,19 @@
 username
+%5C%22
 :
- 
 %5C%22%22 + th
@@ -1354,16 +1354,29 @@
 e() + %22%5C
+%22%22 +%0A        
 %22%7D%22;%0A%0A  

fix invalid JSON in payload and add some formatting