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;

import java.util.Arrays;

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 : Arrays.copyOfRange(e.getCommits(), Math.max(e.getCommits().length - 6, 0), e.getCommits().length - 1)) {
            eb.addField("Commit:", "[" +
                    commit.getId().substring(0, 7) + "](" +
                    commit.getUrl() + ")\n Branch `" +
                    e.getRef().substring(e.getRef().lastIndexOf('/') + 1) + "` " + "```" +
                    commit.getMessage().substring(0, Math.min(commit.getMessage().length(), 80) - 1) +
                    (commit.getMessage().length() > 80 ? "..." : "")
                    + "```", false);

            if (commit.getAdded().length > 0) {
                StringBuilder sb = new StringBuilder();
                sb.append("```md\n");
                int i = 1;
                for (String filePath : Arrays.copyOfRange(commit.getAdded(), Math.max(commit.getAdded().length - 6, 0), commit.getAdded().length - 1)) {
                    String file = filePath.substring(filePath.lastIndexOf("/") + 1);
                    sb.append("* ").append(file).append("\n");
                }

                if (commit.getAdded().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 : Arrays.copyOfRange(commit.getRemoved(), Math.max(commit.getRemoved().length - 6, 0), commit.getRemoved().length - 1)) {
                    String file = filePath.substring(filePath.lastIndexOf("/") + 1);
                    sb.append("* ").append(file).append("\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 : Arrays.copyOfRange(commit.getModified(), Math.max(commit.getModified().length - 6, 0), commit.getModified().length - 1)) {
                    String file = filePath.substring(filePath.lastIndexOf("/") + 1);
                    sb.append("* ").append(file).append("\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();
    }
}

@@ -725,28 +725,26 @@
     
-for (
 Commit
+%5B%5D
  commit
- :
+s =
  Arr
@@ -828,37 +828,71 @@
 Commits().length
- - 1)
+);%0A        for (Commit commit : commits
 ) %7B%0A            
@@ -1482,34 +1482,8 @@
 %22);%0A
-                int i = 1;
 %0A   
@@ -1495,38 +1495,32 @@
         
-for (
 String
- filePath :
+%5B%5D added =
  Arrays.
@@ -1614,32 +1614,78 @@
 ed().length - 1)
+;%0A                for (String filePath : added
 ) %7B%0A            
@@ -1971,94 +1971,56 @@
     
-String added = sb.toString() + %22%60%60%60%22;%0A                eb.addField(%22Added files%22, added
+eb.addField(%22Added files%22, sb.toString() + %22%60%60%60%22
 , fa
@@ -2188,34 +2188,8 @@
 %22);%0A
-                int i = 1;
 %0A   
@@ -2201,38 +2201,34 @@
         
-for (
 String
- filePath :
+%5B%5D removed =
  Arrays.
@@ -2323,37 +2323,81 @@
 Removed().length
- - 1)
+);%0A                for (String filePath : removed
 ) %7B%0A            
@@ -2669,64 +2669,8 @@
  %7D%0A%0A
-                String removed = sb.toString() + %22%60%60%60%22;%0A
     
@@ -2710,23 +2710,37 @@
 files%22, 
-removed
+sb.toString() + %22%60%60%60%22
 , false)
@@ -2905,34 +2905,8 @@
 %22);%0A
-                int i = 1;
 %0A   
@@ -2918,38 +2918,35 @@
         
-for (
 String
- filePath :
+%5B%5D modified =
  Arrays.
@@ -3052,21 +3052,66 @@
 ).length
- - 1)
+);%0A                for (String filePath : modified
 ) %7B%0A    
@@ -3392,65 +3392,8 @@
  %7D%0A%0A
-                String modified = sb.toString() + %22%60%60%60%22;%0A
     
@@ -3434,24 +3434,37 @@
 files%22, 
-modified
+sb.toString() + %22%60%60%60%22
 , false)

Fix github