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

package com.hypixel.hytale.server.npc.commands;

import com.hypixel.hytale.server.core.command.system.arguments.system.Argument;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.ArchetypeChunk;
import javax.annotation.Nullable;
import java.util.UUID;
import com.hypixel.hytale.server.core.entity.UUIDComponent;
import com.hypixel.hytale.server.core.util.TargetUtil;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.server.npc.components.StepComponent;
import com.hypixel.hytale.server.core.entity.Frozen;
import com.hypixel.hytale.server.npc.entities.NPCEntity;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.universe.world.World;
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.system.OptionalArg;
import com.hypixel.hytale.server.core.command.system.arguments.types.EntityWrappedArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.command.system.arguments.system.FlagArg;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractWorldCommand;

public class NPCStepCommand extends AbstractWorldCommand
{
    @Nonnull
    private final FlagArg allArg;
    @Nonnull
    private final EntityWrappedArg entityArg;
    @Nonnull
    private final OptionalArg<Float> dtArg;
    
    public NPCStepCommand() {
        super("step", "server.commands.npc.step.desc");
        this.allArg = this.withFlagArg("all", "server.commands.npc.step.all");
        this.entityArg = this.withOptionalArg("entity", "server.commands.entity.entity.desc", ArgTypes.ENTITY_ID);
        this.dtArg = this.withOptionalArg("dt", "server.commands.npc.step.dt.desc", ArgTypes.FLOAT).addValidator((Validator<Float>)Validators.greaterThan((DataType)0.0f));
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nonnull final World world, @Nonnull final Store<EntityStore> store) {
        final float dt = this.dtArg.provided(context) ? this.dtArg.get(context) : (1.0f / world.getTps());
        if (((Argument<Arg, Boolean>)this.allArg).get(context)) {
            store.forEachEntityParallel(NPCEntity.getComponentType(), (index, archetypeChunk, commandBuffer) -> {
                commandBuffer.ensureComponent(archetypeChunk.getReferenceTo(index), Frozen.getComponentType());
                commandBuffer.addComponent(archetypeChunk.getReferenceTo(index), StepComponent.getComponentType(), new StepComponent(dt));
            });
            return;
        }
        final NPCEntity npc = this.getNPC(context, store);
        if (npc == null) {
            return;
        }
        final Ref<EntityStore> ref = npc.getReference();
        if (ref == null || !ref.isValid()) {
            return;
        }
        store.ensureComponent(ref, Frozen.getComponentType());
        store.addComponent(ref, StepComponent.getComponentType(), new StepComponent(dt));
    }
    
    @Nullable
    private NPCEntity getNPC(@Nonnull final CommandContext context, @Nonnull final Store<EntityStore> store) {
        Ref<EntityStore> ref;
        if (this.entityArg.provided(context)) {
            ref = this.entityArg.get(store, context);
        }
        else {
            if (!context.isPlayer()) {
                context.sendMessage(Message.translation("server.commands.errors.playerOrArg").param("option", "entity"));
                return null;
            }
            final Ref<EntityStore> playerRef = context.senderAsPlayerRef();
            if (playerRef == null || !playerRef.isValid()) {
                context.sendMessage(Message.translation("server.commands.errors.playerOrArg").param("option", "entity"));
                return null;
            }
            ref = TargetUtil.getTargetEntity(playerRef, store);
            if (ref == null) {
                context.sendMessage(Message.translation("server.commands.errors.no_entity_in_view").param("option", "entity"));
                return null;
            }
        }
        final NPCEntity npcComponent = store.getComponent(ref, NPCEntity.getComponentType());
        if (npcComponent != null) {
            return npcComponent;
        }
        final UUIDComponent uuidComponent = store.getComponent(ref, UUIDComponent.getComponentType());
        assert uuidComponent != null;
        final UUID uuid = uuidComponent.getUuid();
        context.sendMessage(Message.translation("server.commands.errors.not_npc").param("uuid", uuid.toString()));
        return null;
    }
}
