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

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

import com.hypixel.hytale.math.vector.Vector3d;
import java.util.concurrent.Executor;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.util.TargetUtil;
import com.hypixel.hytale.component.ComponentAccessor;
import it.unimi.dsi.fastutil.objects.ObjectLists;
import it.unimi.dsi.fastutil.objects.ObjectList;
import java.util.List;
import com.hypixel.hytale.server.core.modules.entity.EntityModule;
import com.hypixel.hytale.component.spatial.SpatialResource;
import com.hypixel.hytale.component.Ref;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import java.util.concurrent.CompletableFuture;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.codec.validation.Validator;
import com.hypixel.hytale.codec.validation.Validators;
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.command.system.arguments.types.EntityWrappedArg;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.command.system.arguments.system.OptionalArg;
import com.hypixel.hytale.server.core.Message;

public abstract class AbstractTargetEntityCommand extends AbstractAsyncCommand
{
    private static final Message MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD;
    private static final Message MESSAGE_GENERAL_NO_ENTITY_IN_VIEW;
    @Nonnull
    private final OptionalArg<World> worldArg;
    @Nonnull
    private final OptionalArg<Double> radiusArg;
    @Nonnull
    private final OptionalArg<PlayerRef> playerArg;
    @Nonnull
    private final EntityWrappedArg entityArg;
    
    public AbstractTargetEntityCommand(@Nonnull final String name, @Nonnull final String description) {
        super(name, description);
        this.worldArg = this.withOptionalArg("world", "server.commands.worldthread.arg.desc", ArgTypes.WORLD);
        this.radiusArg = this.withOptionalArg("radius", "server.commands.entity.radius.desc", ArgTypes.DOUBLE).addValidator((Validator<Double>)Validators.greaterThan((DataType)0.0));
        this.playerArg = this.withOptionalArg("player", "server.commands.argtype.player.desc", ArgTypes.PLAYER_REF);
        this.entityArg = this.withOptionalArg("entity", "server.commands.entity.entity.desc", ArgTypes.ENTITY_ID);
    }
    
    public AbstractTargetEntityCommand(@Nonnull final String name, @Nonnull final String description, final boolean requiresConfirmation) {
        super(name, description, requiresConfirmation);
        this.worldArg = this.withOptionalArg("world", "server.commands.worldthread.arg.desc", ArgTypes.WORLD);
        this.radiusArg = this.withOptionalArg("radius", "server.commands.entity.radius.desc", ArgTypes.DOUBLE).addValidator((Validator<Double>)Validators.greaterThan((DataType)0.0));
        this.playerArg = this.withOptionalArg("player", "server.commands.argtype.player.desc", ArgTypes.PLAYER_REF);
        this.entityArg = this.withOptionalArg("entity", "server.commands.entity.entity.desc", ArgTypes.ENTITY_ID);
    }
    
    public AbstractTargetEntityCommand(@Nonnull final String description) {
        super(description);
        this.worldArg = this.withOptionalArg("world", "server.commands.worldthread.arg.desc", ArgTypes.WORLD);
        this.radiusArg = this.withOptionalArg("radius", "server.commands.entity.radius.desc", ArgTypes.DOUBLE).addValidator((Validator<Double>)Validators.greaterThan((DataType)0.0));
        this.playerArg = this.withOptionalArg("player", "server.commands.argtype.player.desc", ArgTypes.PLAYER_REF);
        this.entityArg = this.withOptionalArg("entity", "server.commands.entity.entity.desc", ArgTypes.ENTITY_ID);
    }
    
    @Nonnull
    @Override
    protected final CompletableFuture<Void> executeAsync(@Nonnull final CommandContext context) {
        World world;
        if (this.worldArg.provided(context)) {
            world = this.worldArg.get(context);
        }
        else {
            if (!context.isPlayer()) {
                context.sendMessage(Message.translation("server.commands.errors.playerOrArg").param("option", "world"));
                return CompletableFuture.completedFuture((Void)null);
            }
            final Ref<EntityStore> ref = context.senderAsPlayerRef();
            if (ref == null || !ref.isValid()) {
                context.sendMessage(AbstractTargetEntityCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD);
                return CompletableFuture.completedFuture((Void)null);
            }
            world = ref.getStore().getExternalData().getWorld();
        }
        final Store<EntityStore> store = world.getEntityStore().getStore();
        return this.runAsync(context, () -> {
            ObjectList<Ref<EntityStore>> entitiesToOperateOn;
            if (this.radiusArg.provided(context)) {
                if (!context.isPlayer()) {
                    context.sendMessage(Message.translation("server.commands.errors.playerOrArg").param("option", "radius"));
                    return;
                }
                else {
                    final Ref<EntityStore> playerRef = context.senderAsPlayerRef();
                    if (playerRef == null || !playerRef.isValid()) {
                        context.sendMessage(AbstractTargetEntityCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD);
                        return;
                    }
                    else {
                        final TransformComponent transformComponent = store.getComponent(playerRef, TransformComponent.getComponentType());
                        if (transformComponent == null) {
                            context.sendMessage(AbstractTargetEntityCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD);
                            return;
                        }
                        else {
                            final double radius = this.radiusArg.get(context);
                            final Vector3d position = transformComponent.getPosition();
                            entitiesToOperateOn = new ObjectArrayList<Ref<EntityStore>>();
                            final ObjectList<Ref<EntityStore>> results = SpatialResource.getThreadLocalReferenceList();
                            final SpatialResource<Ref<EntityStore>, EntityStore> entitySpatialResource = store.getResource(EntityModule.get().getEntitySpatialResourceType());
                            entitySpatialResource.getSpatialStructure().collect(position, radius, results);
                            entitiesToOperateOn.addAll(results);
                            final SpatialResource<Ref<EntityStore>, EntityStore> playerSpatialResource = store.getResource(EntityModule.get().getPlayerSpatialResourceType());
                            playerSpatialResource.getSpatialStructure().collect(position, radius, results);
                            entitiesToOperateOn.addAll(results);
                        }
                    }
                }
            }
            else if (this.playerArg.provided(context)) {
                final PlayerRef targetPlayerRef = this.playerArg.get(context);
                final Ref<EntityStore> targetRef = targetPlayerRef.getReference();
                if (targetRef == null || !targetRef.isValid()) {
                    context.sendMessage(AbstractTargetEntityCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD);
                    return;
                }
                else {
                    entitiesToOperateOn = ObjectLists.singleton(targetRef);
                }
            }
            else if (this.entityArg.provided(context)) {
                final Ref<EntityStore> entityRef = this.entityArg.get(store, context);
                if (entityRef == null || !entityRef.isValid()) {
                    context.sendMessage(AbstractTargetEntityCommand.MESSAGE_GENERAL_NO_ENTITY_IN_VIEW);
                    return;
                }
                else {
                    entitiesToOperateOn = ObjectLists.singleton(entityRef);
                }
            }
            else if (context.isPlayer()) {
                final Ref<EntityStore> playerRef2 = context.senderAsPlayerRef();
                if (playerRef2 == null || !playerRef2.isValid()) {
                    context.sendMessage(AbstractTargetEntityCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD);
                    return;
                }
                else {
                    final Ref<EntityStore> entityRef2 = TargetUtil.getTargetEntity(playerRef2, store);
                    if (entityRef2 == null) {
                        context.sendMessage(AbstractTargetEntityCommand.MESSAGE_GENERAL_NO_ENTITY_IN_VIEW);
                        return;
                    }
                    else {
                        entitiesToOperateOn = ObjectLists.singleton(entityRef2);
                    }
                }
            }
            else {
                context.sendMessage(Message.translation("server.commands.errors.playerOrArg").param("option", "entity"));
                return;
            }
            this.execute(context, entitiesToOperateOn, world, store);
        }, world);
    }
    
    protected abstract void execute(@Nonnull final CommandContext p0, @Nonnull final ObjectList<Ref<EntityStore>> p1, @Nonnull final World p2, @Nonnull final Store<EntityStore> p3);
    
    static {
        MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD = Message.translation("server.commands.errors.playerNotInWorld");
        MESSAGE_GENERAL_NO_ENTITY_IN_VIEW = Message.translation("server.general.noEntityInView");
    }
}
