/*
 * This file is part of Nucleus, licensed under the MIT License (MIT). See the LICENSE.txt file
 * at the root of this project for more details.
 */
package io.github.nucleuspowered.nucleus.argumentparsers;

import static org.spongepowered.api.util.SpongeApiTranslationHelper.t;

import com.google.common.collect.ImmutableList;
import io.github.nucleuspowered.nucleus.internal.traits.PermissionTrait;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.ArgumentParseException;
import org.spongepowered.api.command.args.CommandArgs;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.args.CommandElement;
import org.spongepowered.api.text.Text;

import java.util.List;

import javax.annotation.Nullable;

public class NucleusRequirePermissionArgument extends CommandElement implements PermissionTrait {

    private final CommandElement wrapped;
    private final String permission;

    public NucleusRequirePermissionArgument(CommandElement wrapped, String permission) {
        super(wrapped.getKey());
        this.wrapped = wrapped;
        this.permission = permission;
    }

    @Nullable
    @Override
    public Text getKey() {
        return this.wrapped.getKey();
    }

    @Nullable
    @Override
    protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
        return null;
    }

    @Override
    public void parse(CommandSource source, CommandArgs args, CommandContext context) throws ArgumentParseException {
        if (!hasPermission(source, this.permission)) {
            Text key = this.wrapped.getKey();
            throw args.createError(t("You do not have permission to use the %s argument", key != null ? key : t("unknown")));
        }
        this.wrapped.parse(source, args, context);
    }

    @Override
    public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) {
        if (!hasPermission(src, this.permission)) {
            return ImmutableList.of();
        }
        return this.wrapped.complete(src, args, context);
    }
}

@@ -324,16 +324,93 @@
 leList;%0A
+import io.github.nucleuspowered.nucleus.argumentparsers.util.WrappedElement;%0A
 import i
@@ -917,22 +917,22 @@
 extends 
-Comman
+Wrappe
 dElement
@@ -966,50 +966,8 @@
  %7B%0A%0A
-    private final CommandElement wrapped;%0A
     
@@ -1114,50 +1114,9 @@
 pped
-.getKey());%0A        this.wrapped = wrapped
+)
 ;%0A  
@@ -1162,108 +1162,8 @@
  %7D%0A%0A
-    @Nullable%0A    @Override%0A    public Text getKey() %7B%0A        return this.wrapped.getKey();%0A    %7D%0A%0A
     
@@ -1528,29 +1528,16 @@
 t key = 
-this.wrapped.
 getKey()
@@ -1682,28 +1682,35 @@
         
-this.wrapped
+getWrappedElement()
 .parse(s
@@ -1969,20 +1969,27 @@
 urn 
-this.wrapped
+getWrappedElement()
 .com

Use the wrapped element.