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

package com.hypixel.hytale.server.core.command.system.basecommands;

import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.universe.world.World;
import java.util.concurrent.Executor;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.server.core.permissions.PermissionHolder;
import com.hypixel.hytale.server.core.command.system.CommandUtil;
import com.hypixel.hytale.server.core.permissions.HytalePermissions;
import java.util.concurrent.CompletableFuture;
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 com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.server.core.command.system.arguments.system.OptionalArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;

public abstract class AbstractTargetPlayerCommand extends AbstractAsyncCommand
{
    @Nonnull
    private static final Message MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD;
    @Nonnull
    private final OptionalArg<PlayerRef> playerArg;
    static final /* synthetic */ boolean $assertionsDisabled;
    
    public AbstractTargetPlayerCommand(@Nonnull final String name, @Nonnull final String description) {
        super(name, description);
        this.playerArg = this.withOptionalArg("player", "server.commands.argtype.player.desc", ArgTypes.PLAYER_REF);
    }
    
    public AbstractTargetPlayerCommand(@Nonnull final String name, @Nonnull final String description, final boolean requiresConfirmation) {
        super(name, description, requiresConfirmation);
        this.playerArg = this.withOptionalArg("player", "server.commands.argtype.player.desc", ArgTypes.PLAYER_REF);
    }
    
    public AbstractTargetPlayerCommand(@Nonnull final String description) {
        super(description);
        this.playerArg = this.withOptionalArg("player", "server.commands.argtype.player.desc", ArgTypes.PLAYER_REF);
    }
    
    @Nonnull
    @Override
    protected final CompletableFuture<Void> executeAsync(@Nonnull final CommandContext context) {
        Ref<EntityStore> sourceRef;
        if (context.isPlayer()) {
            sourceRef = context.senderAsPlayerRef();
        }
        else {
            sourceRef = null;
        }
        Ref<EntityStore> targetRef;
        if (this.playerArg.provided(context)) {
            targetRef = this.playerArg.get(context).getReference();
            CommandUtil.requirePermission(context.sender(), HytalePermissions.fromCommand(this.getPermission() + ".other"));
        }
        else {
            if (!context.isPlayer()) {
                context.sendMessage(Message.translation("server.commands.errors.playerOrArg").param("option", "player"));
                return CompletableFuture.completedFuture((Void)null);
            }
            targetRef = context.senderAsPlayerRef();
        }
        if (targetRef == null || !targetRef.isValid()) {
            context.sendMessage(AbstractTargetPlayerCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD);
            return CompletableFuture.completedFuture((Void)null);
        }
        final Store<EntityStore> targetStore = targetRef.getStore();
        final World targetWorld = targetStore.getExternalData().getWorld();
        return this.runAsync(context, () -> {
            final PlayerRef playerRefComponent = targetStore.getComponent(targetRef, PlayerRef.getComponentType());
            if (!AbstractTargetPlayerCommand.$assertionsDisabled && playerRefComponent == null) {
                throw new AssertionError();
            }
            else {
                this.execute(context, sourceRef, targetRef, playerRefComponent, targetWorld, targetStore);
            }
        }, targetWorld);
    }
    
    protected abstract void execute(@Nonnull final CommandContext p0, @Nullable final Ref<EntityStore> p1, @Nonnull final Ref<EntityStore> p2, @Nonnull final PlayerRef p3, @Nonnull final World p4, @Nonnull final Store<EntityStore> p5);
    
    static {
        MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD = Message.translation("server.commands.errors.playerNotInWorld");
    }
}
