package stream.flarebot.flarebot.commands.administrator;

import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.entities.User;
import stream.flarebot.flarebot.FlareBot;
import stream.flarebot.flarebot.commands.Command;
import stream.flarebot.flarebot.commands.CommandType;
import stream.flarebot.flarebot.commands.FlareBotManager;
import stream.flarebot.flarebot.mod.Punishment;
import stream.flarebot.flarebot.util.MessageUtils;

import java.awt.*;
import java.util.EnumSet;

public class BanCommand implements Command {

    @Override
    public void onCommand(User sender, TextChannel channel, Message message, String[] args, Member member) {
        if (args.length >= 1) {
            if (channel.getGuild().getSelfMember().hasPermission(channel, Permission.BAN_MEMBERS)) {
                User user = MessageUtils.getUser(args[0]);
                if (user == null) {
                    channel.sendMessage(new EmbedBuilder()
                            .setDescription("We cannot find that user! Try their ID if you didn't already.")
                            .setColor(Color.red).build()).queue();
                    return;
                }
                // TODO: When reasons are out for JDA add them here!!!
                String reason = null;
                if (args.length >= 2)
                    reason = MessageUtils.getMessage(args, 1);
                FlareBotManager.getInstance().getAutoModConfig(channel.getGuild().getId())
                        .postToModLog(channel, user, sender, Punishment.EPunishment.BAN, reason);
                channel.sendMessage(new EmbedBuilder()
                        .setDescription("The ban hammer has been struck on " + user.getName() + " \uD83D\uDD28")
                        .setImage(channel.getGuild().getId().equals(FlareBot.OFFICIAL_GUILD) ?
                                "https://cdn.discordapp.com/attachments/226785954537406464/309414200344707084/logo-no-background.png" : null)
                        .build()).queue();
                channel.getGuild().getController().ban(channel.getGuild().getMember(user), 7 /*, reason*/).queue();
            } else {
                channel.sendMessage(new EmbedBuilder()
                        .setDescription("We can't ban users! Make sure we have the `Ban Members` permission!")
                        .setColor(Color.red).build()).queue();
            }
        } else {
            MessageUtils.getUsage(this, channel).queue();
        }
    }

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

    @Override
    public String getDescription() {
        return "Ban a user";
    }

    @Override
    public String getUsage() {
        return "`{%}ban <user> <reason>` - Ban a user with a reason";
    }

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

    @Override
    public EnumSet<Permission> getDiscordPermission() {
        return EnumSet.of(Permission.BAN_MEMBERS);
    }
}

@@ -313,24 +313,84 @@
 ities.User;%0A
+import net.dv8tion.jda.core.exceptions.PermissionException;%0A
 import strea
@@ -1804,16 +1804,162 @@
 eason);%0A
+                try %7B%0A                    channel.getGuild().getController().ban(channel.getGuild().getMember(user), 7 /*, reason*/).queue();%0A    
         
@@ -2021,32 +2021,36 @@
                 
+  
+  
 .setDescription(
@@ -2142,24 +2142,28 @@
             
+    
 .setImage(ch
@@ -2253,16 +2253,20 @@
         
+ 
+   
 %22https:/
@@ -2391,16 +2391,20 @@
         
+   
+ 
 .build()
@@ -2434,107 +2434,233 @@
     
-channel.getGuild().getController().ban(channel.getGuild().getMember(user), 7 /*, reason*/).queue();
+%7D catch (PermissionException e) %7B%0A                    MessageUtils.sendErrorMessage(String.format(%22Cannot ban player **%25s#%25s**! I do not have permission!%22, user.getName(), user.getDiscriminator()), channel);%0A                %7D
 %0A   

Fix ban exception