package stream.flarebot.flarebot.github;

import com.arsenarsen.githubwebhooks4j.events.EventListener;
import com.arsenarsen.githubwebhooks4j.events.PushEvent;
import com.arsenarsen.githubwebhooks4j.objects.Commit;
import net.dv8tion.jda.core.EmbedBuilder;
import stream.flarebot.flarebot.FlareBot;
import stream.flarebot.flarebot.util.MessageUtils;

public class GithubListener implements EventListener<PushEvent> {

    @Override
    public void handle(PushEvent e) {
        if (!FlareBot.getInstance().isReady())
            return;
        EmbedBuilder eb = MessageUtils.getEmbed();
        eb.setAuthor(e.getSender().getLogin(), e.getSender().getProfile(), e.getSender().getAvatarUrl());
        for (Commit commit : e.getCommits()) {
            eb.addField("Commit:", "[" +
                    commit.getId().substring(0, 7) + "](" +
                    commit.getUrl() + ")\n Branch `" +
                    e.getRef().substring(e.getRef().lastIndexOf('/') + 1) + "` " + "```" +
                    commit.getMessage() + "```", false);

            if (commit.getAdded().length > 0) {
                StringBuilder sb = new StringBuilder();
                sb.append("```md\n");
                int i = 1;
                for (String filePath : commit.getAdded()) {
                    if (i++ <= 5) {
                        String file = filePath.substring(filePath.lastIndexOf("/") + 1);
                        sb.append("* " + file + "\n\n");
                    }
                }

                if (commit.getRemoved().length >= 5) {
                    sb.append("...\n");
                }

                String added = sb.toString() + "```";
                eb.addField("Added files", added, false);
            }

            if (commit.getRemoved().length > 0) {
                StringBuilder sb = new StringBuilder();
                sb.append("```md\n");
                int i = 1;
                for (String filePath : commit.getRemoved()) {
                    if (i++ <= 5) {
                        String file = filePath.substring(filePath.lastIndexOf("/") + 1);
                        sb.append("* " + file + "\n\n");
                    }
                }

                if (commit.getRemoved().length >= 5) {
                    sb.append("...\n");
                }

                String removed = sb.toString() + "```";
                eb.addField("Removed files", removed, false);
            }

            if (commit.getModified().length > 0) {
                StringBuilder sb = new StringBuilder();
                sb.append("```md\n");
                int i = 1;
                for (String filePath : commit.getModified()) {
                    if (i++ <= 5) {
                        String file = filePath.substring(filePath.lastIndexOf("/") + 1);
                        sb.append("* " + file + "\n\n");
                    }
                }

                if (commit.getModified().length >= 5) {
                    sb.append("...\n");
                }

                String modified = sb.toString() + "```";
                eb.addField("Modified files", modified, false);
            }
        }
        FlareBot.getInstance().getChannelByID("229236239201468417").sendMessage(eb.build()).queue();
    }
}

@@ -344,16 +344,42 @@
 Utils;%0A%0A
+import java.util.Arrays;%0A%0A
 public c
@@ -746,21 +746,108 @@
 t : 
-e.getCommits(
+Arrays.copyOfRange(e.getCommits(), Math.max(e.getCommits().length - 6, 0), e.getCommits().length - 1
 )) %7B
@@ -1133,16 +1133,168 @@
 essage()
+.substring(0, Math.min(commit.getMessage().length(), 80) - 1) +%0A                    (commit.getMessage().length() %3E 80 ? %22...%22 : %22%22)%0A                   
  + %22%60%60%60%22
@@ -1516,69 +1516,122 @@
 h : 
-commit.getAdded()) %7B%0A                    if (i++ %3C= 5) %7B%0A    
+Arrays.copyOfRange(commit.getAdded(), Math.max(commit.getAdded().length - 6, 0), commit.getAdded().length - 1)) %7B%0A
     
@@ -1723,36 +1723,32 @@
                 
-    
 sb.append(%22* %22 +
@@ -1749,48 +1749,36 @@
 %22* %22
- + file + %22%5Cn%5Cn%22);%0A                    %7D
+).append(file).append(%22%5Cn%22);
 %0A   
@@ -1815,37 +1815,35 @@
   if (commit.get
-Remov
+Add
 ed().length %3E= 5
@@ -2246,71 +2246,128 @@
 h : 
-commit.getRemoved()) %7B%0A                    if (i++ %3C= 5) %7B%0A    
+Arrays.copyOfRange(commit.getRemoved(), Math.max(commit.getRemoved().length - 6, 0), commit.getRemoved().length - 1)) %7B%0A
     
@@ -2459,36 +2459,32 @@
                 
-    
 sb.append(%22* %22 +
@@ -2485,48 +2485,36 @@
 %22* %22
- + file + %22%5Cn%5Cn%22);%0A                    %7D
+).append(file).append(%22%5Cn%22);
 %0A   
@@ -2987,16 +2987,35 @@
 ePath : 
+Arrays.copyOfRange(
 commit.g
@@ -3030,52 +3030,92 @@
 ed()
-) %7B%0A                    if (i++ %3C= 5) %7B%0A    
+, Math.max(commit.getModified().length - 6, 0), commit.getModified().length - 1)) %7B%0A
     
@@ -3207,36 +3207,32 @@
                 
-    
 sb.append(%22* %22 +
@@ -3233,48 +3233,36 @@
 %22* %22
- + file + %22%5Cn%5Cn%22);%0A                    %7D
+).append(file).append(%22%5Cn%22);
 %0A   

make better