// 
// 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 it.unimi.dsi.fastutil.Pair;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.server.npc.components.messaging.BeaconSupport;
import com.hypixel.hytale.server.npc.entities.NPCEntity;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Store;
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.command.system.arguments.types.EntityWrappedArg;
import com.hypixel.hytale.server.core.command.system.arguments.system.FlagArg;
import com.hypixel.hytale.server.core.command.system.arguments.system.OptionalArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.command.system.arguments.system.RequiredArg;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;

public class NPCMessageCommand extends AbstractPlayerCommand
{
    @Nonnull
    private final RequiredArg<String> messageArg;
    @Nonnull
    private final OptionalArg<Double> expirationTimeArg;
    @Nonnull
    private final FlagArg allArg;
    @Nonnull
    private final EntityWrappedArg entityArg;
    
    public NPCMessageCommand() {
        super("message", "server.commands.npc.message.desc");
        this.messageArg = this.withRequiredArg("message", "server.commands.npc.message.message.desc", ArgTypes.STRING);
        this.expirationTimeArg = this.withOptionalArg("expiration", "server.commands.npc.message.expiration", ArgTypes.DOUBLE);
        this.allArg = this.withFlagArg("all", "server.commands.npc.message.all");
        this.entityArg = this.withOptionalArg("entity", "server.commands.entity.entity.desc", ArgTypes.ENTITY_ID);
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nonnull final Store<EntityStore> store, @Nonnull final Ref<EntityStore> ref, @Nonnull final PlayerRef playerRef, @Nonnull final World world) {
        final String msg = this.messageArg.get(context);
        final double expiration = this.expirationTimeArg.provided(context) ? this.expirationTimeArg.get(context) : 1.0;
        if (((Argument<Arg, Boolean>)this.allArg).get(context)) {
            store.forEachEntityParallel(NPCEntity.getComponentType(), (index, archetypeChunk, commandBuffer) -> {
                final BeaconSupport beaconSupport = archetypeChunk.getComponent(index, BeaconSupport.getComponentType());
                if (beaconSupport != null) {
                    beaconSupport.postMessage(msg, ref, expiration);
                }
            });
            return;
        }
        final Pair<Ref<EntityStore>, NPCEntity> targetNpcPair = NPCCommandUtils.getTargetNpc(context, this.entityArg, store);
        if (targetNpcPair == null) {
            return;
        }
        final Ref<EntityStore> targetNpcRef = targetNpcPair.first();
        final BeaconSupport beaconSupportComponent = store.getComponent(targetNpcRef, BeaconSupport.getComponentType());
        if (beaconSupportComponent != null) {
            beaconSupportComponent.postMessage(msg, ref, expiration);
        }
    }
}
