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;
                if (args.length >= 2)
                    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;
        }
        
        boolean locked;
        if (tc.getPermissionOverride(guild.getGuild().getPublicRole()) == null || 
                tc.getPermissionOverride(guild.getGuild().getPublicRole()).getDenied().contains(Permission.MESSAGE_WRITE)) {
            tc.createPermissionOverride(guild.getGuild().getPublicRole()).setDeny(Permission.MESSAGE_WRITE).queue();
            tc.createPermissionOverride(guild.getGuild().getSelfMember()).setAllow(Permission.MESSAGE_WRITE, Permission.MESSAGE_READ).queue();
            locked = true;
        } else {
            tc.getPermissionOverride(guild.getGuild().getPublicRole()).getManager().grant(Permission.MESSAGE_WRITE).queue();
            tc.getPermissionOverride(guild.getGuild().getSelfMember()).delete().queue();
            locked = false;
        }
        channel.sendMessage(new EmbedBuilder().setColor(locked ? ColorUtils.RED : ColorUtils.GREEN)
                    .setDescription((tc.getIdLong() == channel.getIdLong() ? "The chat" : tc.getAsMention()) + " has been " 
                                    + (locked ? "locked" : "unlocked") + !" 
                                    + (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;
    }
}

@@ -2336,32 +2336,87 @@
 alse;%0A        %7D%0A
+        if (tc.getIdLong() != channel.getIdLong())%0A    
         channel.
@@ -2539,131 +2539,372 @@
 ion(
-(
 tc.get
-IdL
+AsMenti
 on
-g
 () 
-== channel.getIdLong() ? %22The chat%22 : tc.getAsMention()) + %22 has been %22 %0A                                  
++ %22 has been %22 + (locked ? %22locked%22 : %22unlocked%22) + %22!%22)%0A                    .build()).queue();%0A        if (tc.hasPermission(guild.getGuild().getSelfMember(), Permission.MESSAGE_WRITE))%0A            channel.sendMessage(new EmbedBuilder().setColor(locked ? ColorUtils.RED : ColorUtils.GREEN)%0A                    .setDescription(%22The chat has been %22
   + 
@@ -2938,16 +2938,35 @@
 ked%22) + 
+%22 by a staff member
 !%22 %0A    

Improve lockchat a bit more