package stream.flarebot.flarebot.commands.moderation;

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.commands.Command;
import stream.flarebot.flarebot.commands.CommandType;
import stream.flarebot.flarebot.objects.GuildWrapper;
import stream.flarebot.flarebot.util.ColorUtils;
import stream.flarebot.flarebot.util.GeneralUtils;
import stream.flarebot.flarebot.util.MessageUtils;

public class LockChatCommand implements Command {

    @Override
    public void onCommand(User sender, GuildWrapper guild, TextChannel channel, Message message, String[] args, Member member) {
        String reason = null;
        TextChannel tc = channel;
        if (args.length > 0) {
            TextChannel tmp = GeneralUtils.getChannel(args[args.length - 1], guild);
            if (tmp != null) {
                tc = tmp;
                reason = MessageUtils.getMessage(args, 0, args.length - 1);
            } else
                reason = MessageUtils.getMessage(args, 0);
        }

        if (!guild.getGuild().getSelfMember().hasPermission(Permission.MANAGE_ROLES)) {
            MessageUtils.sendErrorMessage("I can't lock the chat due to lack of permissions! " +
                    "I need the `Manage Roles` permission", channel);
            return;
        }
        if (tc.getPermissionOverride(guild.getGuild().getPublicRole()) == null) {
            tc.createPermissionOverride(guild.getGuild().getPublicRole()).setDeny(Permission.MESSAGE_WRITE).queue();
            tc.createPermissionOverride(guild.getGuild().getSelfMember()).setAllow(Permission.MESSAGE_WRITE, Permission.MESSAGE_READ).queue();
            channel.sendMessage(new EmbedBuilder().setColor(ColorUtils.RED)
                    .setDescription("The chat has been locked by a staff member!" + (reason != null ? "\nReason: " + reason : ""))
                    .build()).queue();
        }
        if (tc.getPermissionOverride(guild.getGuild().getPublicRole()).getDenied().contains(Permission.MESSAGE_WRITE)) {
            tc.getPermissionOverride(guild.getGuild().getPublicRole()).getManager().grant(Permission.MESSAGE_WRITE).queue();
            tc.getPermissionOverride(guild.getGuild().getSelfMember()).delete().queue();
            channel.sendMessage(new EmbedBuilder().setColor(ColorUtils.GREEN)
                    .setDescription("The chat has been unlocked!" + (reason != null ? "\nReason: " + reason : ""))
                    .build()).queue();
        } else {
            tc.getPermissionOverride(guild.getGuild().getPublicRole()).getManager().deny(Permission.MESSAGE_WRITE).queue();
            tc.createPermissionOverride(guild.getGuild().getSelfMember()).setAllow(Permission.MESSAGE_WRITE, Permission.MESSAGE_READ).queue();
            channel.sendMessage(new EmbedBuilder().setColor(ColorUtils.RED)
                    .setDescription("The chat has been locked by a staff member!" + (reason != null ? "\nReason: " + reason : ""))
                    .build()).queue();
        }
    }

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

    @Override
    public String getDescription() {
        return "Locks the chat so only moderators can talk.";
    }

    @Override
    public String getUsage() {
        return "`{%}lockchat [reason] [channel]` - Locks the chat so only moderators can talk.";
    }

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

    @Override
    public String[] getAliases() {
        return new String[]{"chatlock", "lc", "lock"};
    }

    @Override
    public boolean isBetaTesterCommand() {
        return true;
    }
}

@@ -1055,16 +1055,58 @@
  = tmp;%0A
+                if (args.length %3E= 2)%0A    
         
@@ -1539,32 +1539,65 @@
 turn;%0A        %7D%0A
+        %0A        boolean locked;%0A
         if (tc.g
@@ -1654,24 +1654,150 @@
 e()) == null
+ %7C%7C %0A                tc.getPermissionOverride(guild.getGuild().getPublicRole()).getDenied().contains(Permission.MESSAGE_WRITE)
 ) %7B%0A        
@@ -2064,370 +2064,37 @@
     
-channel.sendMessage(new EmbedBuilder().setColor(ColorUtils.RED)%0A                    .setDescription(%22The chat has been locked by a staff member!%22 + (reason != null ? %22%5CnReason: %22 + reason : %22%22))%0A                    .build()).queue();%0A        %7D%0A        if (tc.getPermissionOverride(guild.getGuild().getPublicRole()).getDenied().contains(Permission.MESSAGE_WRITE))
+locked = true;%0A        %7D else
  %7B%0A 
@@ -2302,32 +2302,66 @@
 ().queue();%0A    
+        locked = false;%0A        %7D%0A
         channel.
@@ -2392,32 +2392,58 @@
 lder().setColor(
+locked ? ColorUtils.RED : 
 ColorUtils.GREEN
@@ -2484,567 +2484,209 @@
 ion(
-%22The chat has been unlocked!%22 + (reason != null ? %22%5CnReason: %22 + reason : %22%22))%0A                    .build()).queue();%0A        %7D else %7B%0A            tc.getPermissionOverride(guild.getGuild().getPublicRole()).getManager().deny(Permission.MESSAGE_WRITE).queue();%0A            tc.createPermissionOverride(guild.getGuild().getSelfMember()).setAllow(Permission.MESSAGE_WRITE, Permission.MESSAGE_READ).queue();%0A            channel.sendMessage(new EmbedBuilder().setColor(ColorUtils.RED)%0A                    .setDescription(%22The chat has been locked by a staff member!%22
+(tc.getIdLong() == channel.getIdLong() ? %22The chat%22 : tc.getAsMention()) + %22 has been %22 %0A                                    + (locked ? %22locked%22 : %22unlocked%22) + !%22 %0A                                   
  + (
@@ -2770,26 +2770,16 @@
 ueue();%0A
-        %7D%0A
     %7D%0A%0A 

Improve lockchat - shows the channel now and should have fixed the reason being empty with a channel and no reason