package com.nincraft.ninbot.components.info;

import com.nincraft.ninbot.components.command.AbstractCommand;
import com.nincraft.ninbot.util.MessageUtils;
import lombok.val;
import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.MessageBuilder;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.MessageChannel;
import net.dv8tion.jda.core.entities.Role;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class ListCommand extends AbstractCommand {

    private List<String> roleBlackList;

    public ListCommand(List<String> roleBlackList) {
        this.roleBlackList = roleBlackList;
        length = 2;
        name = "list";
        description = "Displays available games for subscribing";
    }

    @Override
    public void executeCommand(MessageReceivedEvent event) {
        val channel = event.getChannel();
        val content = event.getMessage().getContentStripped().split(" ");
        if (content.length > 2) {
            listUsersInSubscription(content[2], event.getGuild(), event.getChannel());
        } else {
            listSubscriptions(event.getGuild(), channel);
        }
    }

    private void listUsersInSubscription(String roleName, Guild guild, MessageChannel channel) {
        val role = guild.getRolesByName(roleName, true);
        if (!role.isEmpty()) {
            val users = guild.getMembersWithRoles(role);
            List<String> userNames = users.stream().map(Member::getEffectiveName).sorted().collect(Collectors.toList());
            MessageUtils.sendMessage(channel, "Users in %s subscription", roleName);
            MessageUtils.sendMessage(channel, userNames.toString());
        } else {
            MessageUtils.sendMessage(channel, "No subscription %s found", roleName);
        }
    }

    private void listSubscriptions(Guild guild, MessageChannel channel) {
        val roleList = guild.getRoles();
        List<String> roleNameList = roleList.stream().map(Role::getName).collect(Collectors.toList());
        roleNameList.removeAll(roleBlackList);
        MessageBuilder messageBuilder = new MessageBuilder();
        EmbedBuilder embedBuilder = new EmbedBuilder();
        embedBuilder.setTitle("Available subscriptions");
        roleNameList.stream().map(roleName -> roleName + "\n").forEach(embedBuilder::appendDescription);
        messageBuilder.setEmbed(embedBuilder.build());
        MessageUtils.sendMessage(channel, messageBuilder.build());
    }
}

@@ -294,154 +294,9 @@
 ies.
-Guild;%0Aimport net.dv8tion.jda.core.entities.Member;%0Aimport net.dv8tion.jda.core.entities.MessageChannel;%0Aimport net.dv8tion.jda.core.entities.Role
+*
 ;%0Aim
@@ -1094,16 +1094,36 @@
 hannel()
+, event.getMessage()
 );%0A     
@@ -1294,32 +1294,61 @@
 eChannel channel
+,%0A            Message message
 ) %7B%0A        val 
@@ -1624,47 +1624,175 @@
 sage
-Utils.sendMessage(channel, %22Users in %25s
+Builder messageBuilder = new MessageBuilder();%0A            EmbedBuilder embedBuilder = new EmbedBuilder();%0A            embedBuilder.setTitle(%22Users in %22 + roleName + %22
  sub
@@ -1801,26 +1801,16 @@
 ription%22
-, roleName
 );%0A     
@@ -1820,42 +1820,39 @@
     
-MessageUtils.sendMessage(channel, 
+embedBuilder.appendDescription(
 user
@@ -1882,98 +1882,204 @@
     
-%7D else %7B%0A            MessageUtils.sendMessage(channel, %22No subscription %25s found%22, roleNam
+    messageBuilder.setEmbed(embedBuilder.build());%0A            MessageUtils.sendMessage(channel, messageBuilder.build());%0A        %7D else %7B%0A            MessageUtils.reactUnsuccessfulResponse(messag
 e);%0A

Refactor list command