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

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

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.server.npc.entities.NPCEntity;
import com.hypixel.hytale.component.Ref;
import it.unimi.dsi.fastutil.Pair;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.command.system.arguments.types.EntityWrappedArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.command.system.CommandContext;

public class NPCCommandUtils
{
    @Nullable
    public static Pair<Ref<EntityStore>, NPCEntity> getTargetNpc(@Nonnull final CommandContext context, @Nonnull final EntityWrappedArg arg, @Nonnull final Store<EntityStore> store) {
        Ref<EntityStore> ref;
        if (arg.provided(context)) {
            ref = arg.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;
            }
        }
        if (ref == null || !ref.isValid()) {
            return null;
        }
        final NPCEntity npcComponent = store.getComponent(ref, NPCEntity.getComponentType());
        if (npcComponent != null) {
            return Pair.of(ref, 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;
    }
}
