package com.bwfcwalshy.flarebot.commands.administrator;

import com.bwfcwalshy.flarebot.FlareBot;
import com.bwfcwalshy.flarebot.MessageUtils;
import com.bwfcwalshy.flarebot.commands.Command;
import com.bwfcwalshy.flarebot.commands.CommandType;
import sx.blah.discord.handle.obj.IChannel;
import sx.blah.discord.handle.obj.IMessage;
import sx.blah.discord.handle.obj.IUser;
import sx.blah.discord.util.DiscordException;
import sx.blah.discord.util.MissingPermissionsException;
import sx.blah.discord.util.RequestBuffer;

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

public class Purge implements Command {
    @Override
    public void onCommand(IUser sender, IChannel channel, IMessage message, String[] args) {
        if (args.length == 1 && args[0].matches("\\d+")) {
            int count = Math.min(Integer.parseInt(args[0]), 100);
            if (count < 2) {
                MessageUtils.sendMessage(MessageUtils
                        .getEmbed(sender).withDesc("Can't purge less than 2 messages!").build(), channel);
            }
            RequestBuffer.request(() -> {
                channel.getMessages().setCacheCapacity(count);
                boolean loaded = channel.getMessages().load(count);
                if (loaded) {
                    try {
                        List<IMessage> list = channel.getMessages().stream().limit(99)
                                .collect(Collectors.toList());
                        channel.getMessages().bulkDelete(list);
                        channel.getMessages().setCacheCapacity(0);
                        MessageUtils.sendMessage(MessageUtils
                                .getEmbed(sender).withDesc(":+1: Deleted!")
                                .appendField("Message Count: ", String.valueOf(list.size()), true).build(), channel);
                    } catch (DiscordException e) {
                        FlareBot.LOGGER.error("Could not bulk delete!", e);
                        MessageUtils.sendMessage(MessageUtils
                                .getEmbed(sender).withDesc("Could not bulk delete! Error occured!").build(), channel);
                    } catch (MissingPermissionsException e) {
                        MessageUtils.sendMessage(MessageUtils.getEmbed(sender)
                                        .withDesc("I do not have the `Manage Messages` permission!\n" + e.getMessage()).build(),
                                channel);
                    }
                } else MessageUtils.sendMessage(MessageUtils
                        .getEmbed(sender).withDesc("Could not load in messages!").build(), channel);
            });
        } else {
            MessageUtils.sendMessage(MessageUtils
                    .getEmbed(sender).withDesc("Bad arguments!\n" + getDescription()).build(), channel);
        }
    }

    @Override
    public String getCommand() {
        return "purge";
    }

    @Override
    public String getDescription() {
        return "Removes last X messages. Usage: `_purge MESSAGES`";
    }

    @Override
    public CommandType getType() {
        return CommandType.ADMINISTRATIVE;
    }

    @Override
    public String getPermission() {
        return "flarebot.purge";
    }

    @Override
    public String[] getAliases() {
        return new String[]{"clean"};
    }
}

@@ -510,24 +510,52 @@
 estBuffer;%0A%0A
+import java.util.ArrayList;%0A
 import java.
@@ -836,17 +836,8 @@
 t = 
-Math.min(
 Inte
@@ -861,14 +861,8 @@
 %5B0%5D)
-, 100)
 ;%0A  
@@ -1041,32 +1041,56 @@
 ld(), channel);%0A
+                return;%0A
             %7D%0A  
@@ -1229,185 +1229,494 @@
 d = 
-channel.getMessages().load(count);%0A                if (loaded) %7B%0A                    try %7B%0A                        List%3CIMessage%3E list = channel.getMessages().stream().limit(
+true;%0A                while (channel.getMessages().size() %3C count)%0A                    loaded &= channel.getMessages().load(Math.min(count, 100));%0A                if (loaded) %7B%0A                    try %7B%0A                        List%3CIMessage%3E list = channel.getMessages().stream().collect(Collectors.toList());%0A                        List%3CIMessage%3E toDelete = new ArrayList%3C%3E();%0A                        for (IMessage msg : list) %7B%0A                            if (toDelete.size() %3E 
 99)
+ %7B
 %0A   
@@ -1748,38 +1748,1119 @@
     
-.collect(Collectors.toList());
+RequestBuffer.request(() -%3E %7B%0A                                    try %7B%0A                                        channel.getMessages().bulkDelete(toDelete);%0A                                    %7D catch (DiscordException e) %7B%0A                                        FlareBot.LOGGER.error(%22Could not bulk delete!%22, e);%0A                                        MessageUtils.sendMessage(MessageUtils%0A                                                .getEmbed(sender).withDesc(%22Could not bulk delete! Error occured!%22).build(), channel);%0A                                    %7D catch (MissingPermissionsException e) %7B%0A                                        MessageUtils.sendMessage(MessageUtils.getEmbed(sender)%0A                                                        .withDesc(%22I do not have the %60Manage Messages%60 permission!%22).build(),%0A                                                channel);%0A                                    %7D%0A                                %7D);%0A                                toDelete.clear();%0A                            %7D%0A                            toDelete.add(msg);%0A                        %7D
 %0A   
@@ -2913,20 +2913,24 @@
 kDelete(
-list
+toDelete
 );%0A     
@@ -3366,32 +3366,99 @@
 k delete!%22, e);%0A
+                        channel.getMessages().setCacheCapacity(0);%0A
                 
@@ -3676,32 +3676,99 @@
 sException e) %7B%0A
+                        channel.getMessages().setCacheCapacity(0);%0A
                 
@@ -3932,28 +3932,9 @@
 ion!
-%5Cn%22 + e.getMessage()
+%22
 ).bu

Purge can now delete over 100 messages:tm: