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

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

import java.util.UUID;
import com.hypixel.hytale.server.core.entity.UUIDComponent;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.util.TargetUtil;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.component.Ref;
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.server.core.command.system.arguments.types.ArgTypes;
import com.hypixel.hytale.server.core.command.system.arguments.types.EntityWrappedArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractWorldCommand;

public abstract class NPCWorldCommandBase extends AbstractWorldCommand
{
    @Nonnull
    protected static final Message MESSAGE_COMMANDS_ERRORS_PLAYER_OR_ARG;
    @Nonnull
    protected static final Message MESSAGE_COMMANDS_ERRORS_NO_ENTITY_IN_VIEW;
    @Nonnull
    protected final EntityWrappedArg entityArg;
    
    public NPCWorldCommandBase(@Nonnull final String name, @Nonnull final String description) {
        super(name, description);
        this.entityArg = this.withOptionalArg("entity", "server.commands.entity.entity.desc", ArgTypes.ENTITY_ID);
    }
    
    public NPCWorldCommandBase(@Nonnull final String name, @Nonnull final String description, final boolean requiresConfirmation) {
        super(name, description, requiresConfirmation);
        this.entityArg = this.withOptionalArg("entity", "server.commands.entity.entity.desc", ArgTypes.ENTITY_ID);
    }
    
    public NPCWorldCommandBase(@Nonnull final String description) {
        super(description);
        this.entityArg = this.withOptionalArg("entity", "server.commands.entity.entity.desc", ArgTypes.ENTITY_ID);
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nonnull final World world, @Nonnull final Store<EntityStore> store) {
        final NPCEntity npc = this.getNPC(context, store);
        if (npc == null) {
            return;
        }
        final Ref<EntityStore> ref = npc.getReference();
        assert ref != null;
        assert ref.isValid();
        this.execute(context, npc, world, store, ref);
    }
    
    protected abstract void execute(@Nonnull final CommandContext p0, @Nonnull final NPCEntity p1, @Nonnull final World p2, @Nonnull final Store<EntityStore> p3, @Nonnull final Ref<EntityStore> p4);
    
    @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(NPCWorldCommandBase.MESSAGE_COMMANDS_ERRORS_PLAYER_OR_ARG);
                return null;
            }
            final Ref<EntityStore> playerRef = context.senderAsPlayerRef();
            if (playerRef == null || !playerRef.isValid()) {
                context.sendMessage(NPCWorldCommandBase.MESSAGE_COMMANDS_ERRORS_PLAYER_OR_ARG);
                return null;
            }
            ref = TargetUtil.getTargetEntity(playerRef, store);
            if (ref == null) {
                context.sendMessage(NPCWorldCommandBase.MESSAGE_COMMANDS_ERRORS_NO_ENTITY_IN_VIEW);
                return null;
            }
        }
        if (ref == null) {
            return null;
        }
        return ensureIsNPC(context, store, ref);
    }
    
    @Nullable
    protected static NPCEntity ensureIsNPC(@Nonnull final CommandContext context, @Nonnull final Store<EntityStore> store, final Ref<EntityStore> ref) {
        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;
    }
    
    static {
        MESSAGE_COMMANDS_ERRORS_PLAYER_OR_ARG = Message.translation("server.commands.errors.playerOrArg").param("option", "entity");
        MESSAGE_COMMANDS_ERRORS_NO_ENTITY_IN_VIEW = Message.translation("server.commands.errors.no_entity_in_view").param("option", "entity");
    }
}
