package com.nincraft.ninbot.command;

import com.nincraft.ninbot.util.MessageSenderHelper;
import com.nincraft.ninbot.util.Reference;
import lombok.val;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Role;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;

import java.util.*;

public class StatsCommand extends AbstractCommand {

    private List<String> roleBlacklist;

    public StatsCommand() {
        roleBlacklist = Reference.getRoleBlacklist();
        commandLength = 2;
        commandName = "stats";
        commandDescription = "Shows Ninbot stats";
    }

    @Override
    public void execute(MessageReceivedEvent event) {
        val server = event.getGuild();
        printRoleStats(event, server);
    }

    private void printRoleStats(MessageReceivedEvent event, Guild server) {
        Map<Role, Integer> roleMap = new HashMap<>();
        for (val member : server.getMembers()) {
            for (val role : member.getRoles()) {
                if (!roleBlacklist.contains(role.getName())) {
                    roleMap.merge(role, 1, (a, b) -> a + b);
                }
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        List<Role> roleList = new ArrayList(roleMap.keySet());
        Collections.sort(roleList);
        Collections.reverse(roleList);
        roleList.forEach(role -> {
            stringBuilder.append(role.getName());
            stringBuilder.append(": ");
            stringBuilder.append(roleMap.get(role));
            stringBuilder.append("\n");
        });
        MessageSenderHelper.sendMessage(event.getChannel(), stringBuilder.toString());
    }
}

@@ -320,9 +320,126 @@
 til.
-*
+Collections;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.stream.Collectors
 ;%0A%0Ap
@@ -1276,24 +1276,25 @@
       %7D%0A
+%0A
         
 StringBu
@@ -1289,130 +1289,152 @@
     
-StringBuilder stringBuilder = new StringBuilder();%0A        List%3CRole%3E roleList = new ArrayList(roleMap.keySe
+List%3CStat%3E statList = roleMap.keySet().stream().map(role -%3E new Stat(role.getName(), roleMap.get(role))).collect(Collectors.toLis
 t());%0A
+%0A
         
 Coll
@@ -1433,33 +1433,56 @@
     
-Collections.sort(roleList
+StringBuilder stringBuilder = new StringBuilder(
 );%0A 
@@ -1504,20 +1504,17 @@
 ons.
-reverse(role
+sort(stat
 List
@@ -1524,20 +1524,20 @@
         
-role
+stat
 List.for
@@ -1541,20 +1541,20 @@
 forEach(
-role
+stat
  -%3E %7B%0A  
@@ -1584,30 +1584,25 @@
 .append(
-role.getN
+stat.n
 ame
-()
 );%0A     
@@ -1669,33 +1669,27 @@
 .append(
-roleMap.get(role)
+stat.amount
 );%0A     
@@ -1827,11 +1827,544 @@
 );%0A    %7D
+%0A%0A    private class Stat implements Comparable %7B%0A        private String name;%0A        private int amount;%0A%0A        Stat(String name, int amount) %7B%0A            this.name = name;%0A            this.amount = amount;%0A        %7D%0A%0A%0A        @Override%0A        public int compareTo(Object o) %7B%0A            Stat stat = (Stat) o;%0A            if (stat.amount == this.amount) %7B%0A                return stat.name.compareTo(this.name);%0A            %7D else %7B%0A                return Integer.compare(stat.amount, this.amount);%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

Fix stats not ordering role subscriptions correctly