package stream.flarebot.flarebot.util;

import net.dv8tion.jda.core.EmbedBuilder;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.json.JSONObject;
import stream.flarebot.flarebot.FlareBot;

import java.io.IOException;

public class GitHubUtils {

    public static EmbedBuilder getEmbedForPR(String prNum) {
        JSONObject obj;
        try {
            Response res = WebUtils.get("https://api.github.com/repos/FlareBot/FlareBot/pulls/" + prNum);
            ResponseBody body = res.body();

            if (body != null) {
                obj = new JSONObject(body.string());
                body.close();
            } else {
                res.close();
                FlareBot.LOGGER.error("GitHub returned an empty response - Code " + res.code());
                return null;
            }
            res.close();
        } catch (IOException e) {
            FlareBot.LOGGER.error("Error getting the PR info! " + e.getMessage(), e);
            return null;
        }

        String body = obj.getString("body");
        String[] array = body.split("\r\n\r\n");

        EmbedBuilder embed = new EmbedBuilder();
        boolean hasTitle = false;
        for (String anArray : array) {
            String value = anArray.replaceAll("\n\\* ", "\n\u2022 ");
            String header = value.replace("## ", "").substring(0, value.indexOf("\n") - 4).replace("\n", "");

            value = value.replace("## " + header, "");

            if (!hasTitle) {
                embed.setTitle(header, null);
                embed.setDescription(value);
                hasTitle = true;
                continue;
            }

            if (value.length() > 1024) {
                embed.addField(header, value.substring(0, value.substring(0, 1024).lastIndexOf("\n")), false);
                value = value.substring(value.substring(0, 1024).lastIndexOf("\n") + 1);
                header += " - Continued";
            }

            embed.addField(header, value, false);
        }

        return embed;
    }
}

@@ -227,16 +227,41 @@
 ception;
+%0Aimport java.util.Arrays;
 %0A%0Apublic
@@ -1125,82 +1125,317 @@
     
-EmbedBuilder embed = new EmbedBuilder();%0A        boolean hasTitle = false;
+String title = array%5B0%5D.split(%22%5Cr%5Cn%22)%5B0%5D.replace(%22### %22, %22%22);%0A        String description = array%5B0%5D.split(%22%5Cr%5Cn%22)%5B1%5D;%0A%0A        EmbedBuilder embed = new EmbedBuilder();%0A        embed.setTitle(title, null);%0A        embed.setDescription(description);%0A%0A        array = Arrays.copyOfRange(array, 1, array.length);%0A
 %0A   
@@ -1711,202 +1711,8 @@
 );%0A%0A
-            if (!hasTitle) %7B%0A                embed.setTitle(header, null);%0A                embed.setDescription(value);%0A                hasTitle = true;%0A                continue;%0A            %7D%0A%0A
     

Fix up stuff