// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.server.core.permissions.commands;

import java.util.HashSet;
import java.util.List;
import java.util.Iterator;
import java.util.Collection;
import com.hypixel.hytale.server.core.util.message.MessageFormat;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.function.Function;
import java.util.Set;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.permissions.provider.PermissionProvider;
import com.hypixel.hytale.server.core.permissions.PermissionsModule;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgumentType;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgTypes;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.command.system.arguments.system.RequiredArg;
import com.hypixel.hytale.server.core.command.system.basecommands.CommandBase;
import com.hypixel.hytale.server.core.command.system.AbstractCommand;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractCommandCollection;

public class PermGroupCommand extends AbstractCommandCollection
{
    public PermGroupCommand() {
        super("group", "server.commands.perm.group.desc");
        this.addSubCommand(new PermGroupListCommand());
        this.addSubCommand(new PermGroupAddCommand());
        this.addSubCommand(new PermGroupRemoveCommand());
    }
    
    private static class PermGroupListCommand extends CommandBase
    {
        @Nonnull
        private final RequiredArg<String> groupArg;
        
        public PermGroupListCommand() {
            super("list", "server.commands.perm.group.list.desc");
            this.groupArg = this.withRequiredArg("group", "server.commands.perm.group.list.group.desc", ArgTypes.STRING);
        }
        
        @Override
        protected void executeSync(@Nonnull final CommandContext context) {
            final String group = this.groupArg.get(context);
            for (final PermissionProvider permissionProvider : PermissionsModule.get().getProviders()) {
                final Message header = Message.raw(permissionProvider.getName());
                final Set<Message> groupPermissions = permissionProvider.getGroupPermissions(group).stream().map((Function<? super Object, ?>)Message::raw).collect((Collector<? super Object, ?, Set<Message>>)Collectors.toSet());
                context.sendMessage(MessageFormat.list(header, groupPermissions));
            }
        }
    }
    
    private static class PermGroupAddCommand extends CommandBase
    {
        @Nonnull
        private final RequiredArg<String> groupArg;
        @Nonnull
        private final RequiredArg<List<String>> permissionsArg;
        
        public PermGroupAddCommand() {
            super("add", "server.commands.perm.group.add.desc");
            this.groupArg = this.withRequiredArg("group", "server.commands.perm.group.add.group.desc", ArgTypes.STRING);
            this.permissionsArg = this.withListRequiredArg("permissions", "server.commands.perm.group.add.permissions.desc", ArgTypes.STRING);
        }
        
        @Override
        protected void executeSync(@Nonnull final CommandContext context) {
            final String group = this.groupArg.get(context);
            final HashSet<String> permissions = new HashSet<String>(this.permissionsArg.get(context));
            PermissionsModule.get().addGroupPermission(group, permissions);
            context.sendMessage(Message.translation("server.commands.perm.addPermToGroup").param("group", group));
        }
    }
    
    private static class PermGroupRemoveCommand extends CommandBase
    {
        @Nonnull
        private final RequiredArg<String> groupArg;
        @Nonnull
        private final RequiredArg<List<String>> permissionsArg;
        
        public PermGroupRemoveCommand() {
            super("remove", "server.commands.perm.group.remove.desc");
            this.groupArg = this.withRequiredArg("group", "server.commands.perm.group.remove.group.desc", ArgTypes.STRING);
            this.permissionsArg = this.withListRequiredArg("permissions", "server.commands.perm.group.remove.permissions.desc", ArgTypes.STRING);
        }
        
        @Override
        protected void executeSync(@Nonnull final CommandContext context) {
            final String group = this.groupArg.get(context);
            final HashSet<String> permissions = new HashSet<String>(this.permissionsArg.get(context));
            PermissionsModule.get().removeGroupPermission(group, permissions);
            context.sendMessage(Message.translation("server.commands.perm.permRemovedFromGroup").param("group", group));
        }
    }
}
