// 
// 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 java.util.UUID;
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 PermUserCommand extends AbstractCommandCollection
{
    public PermUserCommand() {
        super("user", "server.commands.perm.user.desc");
        this.addSubCommand(new PermUserListCommand());
        this.addSubCommand(new PermUserAddCommand());
        this.addSubCommand(new PermUserRemoveCommand());
        this.addSubCommand(new PermUserGroupCommand());
    }
    
    private static class PermUserListCommand extends CommandBase
    {
        @Nonnull
        private final RequiredArg<UUID> uuidArg;
        
        public PermUserListCommand() {
            super("list", "server.commands.perm.user.list.desc");
            this.uuidArg = this.withRequiredArg("uuid", "server.commands.perm.user.list.uuid.desc", ArgTypes.UUID);
        }
        
        @Override
        protected void executeSync(@Nonnull final CommandContext context) {
            final UUID uuid = this.uuidArg.get(context);
            for (final PermissionProvider permissionProvider : PermissionsModule.get().getProviders()) {
                final Message header = Message.raw(permissionProvider.getName());
                final Set<Message> userPermissions = permissionProvider.getUserPermissions(uuid).stream().map((Function<? super Object, ?>)Message::raw).collect((Collector<? super Object, ?, Set<Message>>)Collectors.toSet());
                context.sendMessage(MessageFormat.list(header, userPermissions));
            }
        }
    }
    
    private static class PermUserAddCommand extends CommandBase
    {
        @Nonnull
        private final RequiredArg<UUID> uuidArg;
        @Nonnull
        private final RequiredArg<List<String>> permissionsArg;
        
        public PermUserAddCommand() {
            super("add", "server.commands.perm.user.add.desc");
            this.uuidArg = this.withRequiredArg("uuid", "server.commands.perm.user.add.uuid.desc", ArgTypes.UUID);
            this.permissionsArg = this.withListRequiredArg("permissions", "server.commands.perm.user.add.permissions.desc", ArgTypes.STRING);
        }
        
        @Override
        protected void executeSync(@Nonnull final CommandContext context) {
            final UUID uuid = this.uuidArg.get(context);
            final HashSet<String> permissions = new HashSet<String>(this.permissionsArg.get(context));
            PermissionsModule.get().addUserPermission(uuid, permissions);
            context.sendMessage(Message.translation("server.commands.perm.permAddedToUser").param("uuid", uuid.toString()));
        }
    }
    
    private static class PermUserRemoveCommand extends CommandBase
    {
        @Nonnull
        private final RequiredArg<UUID> uuidArg;
        @Nonnull
        private final RequiredArg<List<String>> permissionsArg;
        
        public PermUserRemoveCommand() {
            super("remove", "server.commands.perm.user.remove.desc");
            this.uuidArg = this.withRequiredArg("uuid", "server.commands.perm.user.remove.uuid.desc", ArgTypes.UUID);
            this.permissionsArg = this.withListRequiredArg("permissions", "server.commands.perm.user.remove.permissions.desc", ArgTypes.STRING);
        }
        
        @Override
        protected void executeSync(@Nonnull final CommandContext context) {
            final UUID uuid = this.uuidArg.get(context);
            final HashSet<String> permissions = new HashSet<String>(this.permissionsArg.get(context));
            PermissionsModule.get().removeUserPermission(uuid, permissions);
            context.sendMessage(Message.translation("server.commands.perm.permRemovedFromUser").param("uuid", uuid.toString()));
        }
    }
    
    private static class PermUserGroupCommand extends AbstractCommandCollection
    {
        public PermUserGroupCommand() {
            super("group", "server.commands.perm.user.group.desc");
            this.addSubCommand(new PermUserGroupListCommand());
            this.addSubCommand(new PermUserGroupAddCommand());
            this.addSubCommand(new PermUserGroupRemoveCommand());
        }
        
        private static class PermUserGroupListCommand extends CommandBase
        {
            @Nonnull
            private final RequiredArg<UUID> uuidArg;
            
            public PermUserGroupListCommand() {
                super("list", "server.commands.perm.user.group.list.desc");
                this.uuidArg = this.withRequiredArg("uuid", "server.commands.perm.user.group.list.uuid.desc", ArgTypes.UUID);
            }
            
            @Override
            protected void executeSync(@Nonnull final CommandContext context) {
                final UUID uuid = this.uuidArg.get(context);
                for (final PermissionProvider permissionProvider : PermissionsModule.get().getProviders()) {
                    final Message header = Message.raw(permissionProvider.getName());
                    final Set<Message> groups = permissionProvider.getGroupsForUser(uuid).stream().map((Function<? super Object, ?>)Message::raw).collect((Collector<? super Object, ?, Set<Message>>)Collectors.toSet());
                    context.sendMessage(MessageFormat.list(header, groups));
                }
            }
        }
        
        private static class PermUserGroupAddCommand extends CommandBase
        {
            @Nonnull
            private final RequiredArg<UUID> uuidArg;
            @Nonnull
            private final RequiredArg<String> groupArg;
            
            public PermUserGroupAddCommand() {
                super("add", "server.commands.perm.user.group.add.desc");
                this.uuidArg = this.withRequiredArg("uuid", "server.commands.perm.user.group.add.uuid.desc", ArgTypes.UUID);
                this.groupArg = this.withRequiredArg("group", "server.commands.perm.user.group.add.group.desc", ArgTypes.STRING);
            }
            
            @Override
            protected void executeSync(@Nonnull final CommandContext context) {
                final UUID uuid = this.uuidArg.get(context);
                final String group = this.groupArg.get(context);
                PermissionsModule.get().addUserToGroup(uuid, group);
                context.sendMessage(Message.translation("server.commands.perm.userAddedToGroup").param("uuid", uuid.toString()).param("group", group));
            }
        }
        
        private static class PermUserGroupRemoveCommand extends CommandBase
        {
            @Nonnull
            private final RequiredArg<UUID> uuidArg;
            @Nonnull
            private final RequiredArg<String> groupArg;
            
            public PermUserGroupRemoveCommand() {
                super("remove", "server.commands.perm.user.group.remove.desc");
                this.uuidArg = this.withRequiredArg("uuid", "server.commands.perm.user.group.remove.uuid.desc", ArgTypes.UUID);
                this.groupArg = this.withRequiredArg("group", "server.commands.perm.user.group.remove.group.desc", ArgTypes.STRING);
            }
            
            @Override
            protected void executeSync(@Nonnull final CommandContext context) {
                final UUID uuid = this.uuidArg.get(context);
                final String group = this.groupArg.get(context);
                PermissionsModule.get().removeUserFromGroup(uuid, group);
                context.sendMessage(Message.translation("server.commands.perm.userRemovedFromGroup").param("uuid", uuid.toString()).param("group", group));
            }
        }
    }
}
