/*
 * Copyright (C) 2016-2019 Dennis Neufeld
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package space.npstr.wolfia.commands.debug;

import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Icon;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.sharding.ShardManager;
import space.npstr.wolfia.commands.BaseCommand;
import space.npstr.wolfia.commands.CommandContext;
import space.npstr.wolfia.commands.GuildCommandContext;
import space.npstr.wolfia.domain.Command;
import space.npstr.wolfia.domain.room.ManagedPrivateRoom;
import space.npstr.wolfia.domain.room.PrivateRoom;
import space.npstr.wolfia.domain.room.PrivateRoomQueue;
import space.npstr.wolfia.domain.room.PrivateRoomService;
import space.npstr.wolfia.utils.discord.RestActions;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Optional;

import static java.util.Objects.requireNonNull;

/**
 * Created by napster on 11.06.17.
 * <p>
 * //this command will register a guild for use as a place to provide private communications, like wolfchat
 */
@Command
public class RegisterPrivateRoomCommand implements BaseCommand {

    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(RegisterPrivateRoomCommand.class);

    private final PrivateRoomService privateRoomService;
    private final PrivateRoomQueue privateRoomQueue;

    public RegisterPrivateRoomCommand(PrivateRoomService privateRoomService, PrivateRoomQueue privateRoomQueue) {
        this.privateRoomService = privateRoomService;
        this.privateRoomQueue = privateRoomQueue;
    }

    @Override
    public String getTrigger() {
        return "register";
    }

    @Nonnull
    @Override
    public String help() {
        return "Register a guild as a private room.";
    }

    @Override
    public synchronized boolean execute(@Nonnull final CommandContext commandContext) {

        final GuildCommandContext context = commandContext.requireGuild();
        if (context == null) {
            return false;
        }

        //make sure we have admin rights
        if (!context.guild.getSelfMember().hasPermission(Permission.ADMINISTRATOR)) {
            context.replyWithMention("gimme admin perms first.");
            return false;
        }

        //set up the looks
        //- give the server a name
        context.guild.getManager().setName("Wolfia Private Server").queue(null, RestActions.defaultOnFail());
        //- give the server a logo
        try {
            context.guild.getManager().setIcon(Icon.from(getClass().getResourceAsStream("/img/popcorn_mafia_guy.png"))).queue(null, RestActions.defaultOnFail());
        } catch (final IOException e) {
            log.error("Could not set icon for guild {}", context.guild.getIdLong(), e);
            return false;
        }

        //set up rights:
        //- deny creating invites
        //- deny reading messages
        context.guild.getPublicRole().getManager().revokePermissions(Permission.CREATE_INSTANT_INVITE, Permission.MESSAGE_READ).queue(null, RestActions.defaultOnFail());
        //- delete #general
        for (final TextChannel tc : context.guild.getTextChannels()) {
            tc.delete().reason("Preparing private guild for usage").complete();
        }


        //register it
        Optional<PrivateRoom> registered = this.privateRoomService.guild(context.guild.getIdLong()).register();
        if (registered.isEmpty()) {
            context.replyWithMention("Looks like this guild is already registered as a private room.");
            return false;
        }

        ManagedPrivateRoom managedPrivateRoom = this.privateRoomQueue.add(registered.get());
        ShardManager shardManager = context.getJda().getShardManager();
        requireNonNull(shardManager).addEventListener(managedPrivateRoom);
        String name = "Wolfia Private Server #" + managedPrivateRoom.getNumber();
        context.guild.getManager().setName(name)
                .queue(null, RestActions.defaultOnFail());
        return true;
    }
}

@@ -757,16 +757,104 @@
 debug;%0A%0A
+import java.io.IOException;%0Aimport java.util.Optional;%0Aimport javax.annotation.Nonnull;%0A
 import n
@@ -979,58 +979,8 @@
 el;%0A
-import net.dv8tion.jda.api.sharding.ShardManager;%0A
 impo
@@ -1453,146 +1453,8 @@
 s;%0A%0A
-import javax.annotation.Nonnull;%0Aimport java.io.IOException;%0Aimport java.util.Optional;%0A%0Aimport static java.util.Objects.requireNonNull;%0A%0A
 /**%0A
@@ -4218,155 +4218,8 @@
 ));%0A
-        ShardManager shardManager = context.getJda().getShardManager();%0A        requireNonNull(shardManager).addEventListener(managedPrivateRoom);%0A
     

Fix registering private rooms