package com.nincraft.ninbot.components.moderation;

import com.nincraft.ninbot.components.command.AbstractCommand;
import com.nincraft.ninbot.components.common.MessageAction;
import com.nincraft.ninbot.components.common.RolePermission;
import com.nincraft.ninbot.components.config.ConfigConstants;
import lombok.val;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.managers.ChannelManager;
import org.springframework.stereotype.Component;

@Component
public class ArchiveChannelCommand extends AbstractCommand {

    public ArchiveChannelCommand() {
        name = "archive";
        length = 2;
        checkExactLength = false;
        permissionLevel = RolePermission.MODS;
    }

    @Override
    protected MessageAction executeCommand(MessageReceivedEvent event) {
        MessageAction messageAction = new MessageAction(event);
        val message = event.getMessage().getContentStripped();
        TextChannel textChannel = getChannelFromMessage(message, event);
        //what are you doin
        if (textChannel == null) {
            messageAction.addUnsuccessfulReaction();
            return messageAction;
        }
        val guild = event.getGuild();

        String archiveCategoryId = getCategoryIdMovingTo(event, guild);
        if (archiveCategoryId == null) {
            messageAction.addUnsuccessfulReaction();
            return messageAction;
        }
        moveChannelToCategory(archiveCategoryId, textChannel, guild).queue(
                aVoidSuccess -> MessageAction.successfulReaction(event.getMessage()),
                aVoidFailure -> MessageAction.unsuccessfulReaction(event.getMessage()));
        return messageAction;
    }

    String getCategoryIdMovingTo(MessageReceivedEvent event, Guild guild) {
        String archiveCategoryId;
        val categoryIdOptional = configService.getSingleValueByName(event.getGuild()
                .getId(), ConfigConstants.ARCHIVE_CATEGORY_ID);
        //an archive channel has been already configured
        if (categoryIdOptional.isPresent()) {
            archiveCategoryId = categoryIdOptional.get();
        } else {
            val categories = guild.getCategoriesByName("archive", true);
            if (!categories.isEmpty()) {
                archiveCategoryId = categories.get(0).getId();
            } else {
                archiveCategoryId = null;
            }
        }
        return archiveCategoryId;
    }

    private TextChannel getChannelFromMessage(String message, MessageReceivedEvent event) {
        val commandLength = getCommandLength(message);
        TextChannel textChannel = null;
        //this channel
        if (commandLength == 2) {
            textChannel = event.getTextChannel();
        }
        //some named channel
        else if (commandLength == 3) {
            val mentionedChannels = event.getMessage().getMentionedChannels();
            if (mentionedChannels.isEmpty()) {
                textChannel = event.getJDA().getTextChannelsByName(getSubcommand(message, 2), true).get(0);
            } else {
                return mentionedChannels.get(0);
            }
        }
        return textChannel;
    }

    private ChannelManager moveChannelToCategory(String archiveCategoryId, TextChannel textChannel, Guild guild) {
        val archiveCategory = guild.getCategoryById(archiveCategoryId);
        return textChannel.getManager().setParent(archiveCategory);
    }
}

@@ -1620,59 +1620,78 @@
  -%3E 
-MessageAction.successfulReact
+updateChannelPermiss
 ion
+s
 (event
-.getMessage()
+, textChannel.getId(), archiveCategoryId
 ),%0A 
@@ -1778,16 +1778,16 @@
 ge()));%0A
-
         
@@ -1811,24 +1811,457 @@
 ion;%0A    %7D%0A%0A
+    void updateChannelPermissions(MessageReceivedEvent event, String textChannelId, String targetCategoryId) %7B%0A        val targetCategory = event.getJDA().getCategoryById(targetCategoryId);%0A        event.getJDA()%0A                .getTextChannelById(textChannelId)%0A                .getManager()%0A                .sync(targetCategory)%0A                .queue(aVoidSuccess -%3E MessageAction.successfulReaction(event.getMessage()));%0A    %7D%0A%0A
     String g

Fix archive/unarchive command permission sync (#370)