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;
        }
        if (tc.getIdLong() != channel.getIdLong())
            channel.sendMessage(new EmbedBuilder().setColor(locked ? ColorUtils.RED : ColorUtils.GREEN)
                    .setDescription(tc.getAsMention() + " has been " + (locked ? "locked" : "unlocked") + "!")
                    .build()).queue();
        if (guild.getGuild().getSelfMember().hasPermission(tc, Permission.MESSAGE_WRITE))
            channel.sendMessage(new EmbedBuilder().setColor(locked ? ColorUtils.RED : ColorUtils.GREEN)
                    .setDescription("The chat has been "  + (locked ? "locked" : "unlocked") + " 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;
    }
}

@@ -310,24 +310,81 @@
 ities.User;%0A
+import net.dv8tion.jda.core.entities.PermissionOverride;%0A
 import strea
@@ -1641,28 +1641,57 @@
 ed;%0A        
-if (
+PermissionOverride everyoneOvr = 
 tc.getPermis
@@ -1740,16 +1740,41 @@
 e())
+;%0A        if (everyoneOvr
  == null
  %7C%7C 
@@ -1773,14 +1773,10 @@
 null
- %7C%7C %0A 
+)%0A
     
@@ -1779,33 +1779,33 @@
             
-   tc.get
+tc.create
 PermissionOv
@@ -1845,16 +1845,53 @@
 Role()).
+complete();%0A        if (!everyoneOvr.
 getDenie
@@ -1948,77 +1948,243 @@
     
-tc.createPermissionOverride(guild.getGuild().getPublicRole
+everyoneOvr.getManager().deny(Permission.MESSAGE_WRITE).queue();%0A            if (tc.getPermissionOverride(guild.getGuild().getSelfMember()) == null)%0A                tc.createPermissionOverride(guild.getGuild().getSelfMember
 ()).set
-Deny
+Allow
 (Per
@@ -2208,40 +2208,83 @@
 RITE
-).queue();%0A            tc.create
+, Permission.MESSAGE_READ).queue();%0A            else%0A                tc.get
 Perm
@@ -2328,32 +2328,42 @@
 lfMember()).
-setAllow
+getManager().grant
 (Permission.
@@ -2471,66 +2471,19 @@
     
-tc.getPermissi
+every
 on
+e
 Ove
-rride(guild.getGuild().getPublicRole())
 .get
@@ -2525,32 +2525,120 @@
 WRITE).queue();%0A
+            if (tc.getPermissionOverride(guild.getGuild().getSelfMember()) != null)%0A    
             tc.g

Should fix any potential errors