// 
// 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.Ref;
import com.hypixel.hytale.component.query.Query;
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.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 javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractWorldCommand;

public class NPCThawCommand extends AbstractWorldCommand
{
    @Nonnull
    private static final Message MESSAGE_COMMANDS_NPC_THAW_ALL;
    @Nonnull
    private final FlagArg allArg;
    @Nonnull
    private final EntityWrappedArg entityArg;
    
    public NPCThawCommand() {
        super("thaw", "server.commands.npc.thaw.desc");
        this.allArg = this.withFlagArg("all", "server.commands.npc.thaw.arg.all");
        this.entityArg = this.withOptionalArg("entity", "server.commands.entity.entity.desc", ArgTypes.ENTITY_ID);
        this.addAliases("unfreeze");
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nonnull final World world, @Nonnull final Store<EntityStore> store) {
        if (((Argument<Arg, Boolean>)this.allArg).get(context)) {
            store.forEachEntityParallel(NPCEntity.getComponentType(), (index, archetypeChunk, commandBuffer) -> commandBuffer.tryRemoveComponent(archetypeChunk.getReferenceTo(index), Frozen.getComponentType()));
            context.sendMessage(NPCThawCommand.MESSAGE_COMMANDS_NPC_THAW_ALL);
            return;
        }
        final Pair<Ref<EntityStore>, NPCEntity> targetNpcPair = NPCCommandUtils.getTargetNpc(context, this.entityArg, store);
        if (targetNpcPair == null) {
            return;
        }
        final Ref<EntityStore> targetNpcRef = targetNpcPair.first();
        final NPCEntity targetNpcComponent = targetNpcPair.second();
        store.tryRemoveComponent(targetNpcRef, Frozen.getComponentType());
        context.sendMessage(Message.translation("server.commands.npc.thaw.npc").param("role", targetNpcComponent.getRoleName()));
    }
    
    static {
        MESSAGE_COMMANDS_NPC_THAW_ALL = Message.translation("server.commands.npc.thaw.all");
    }
}
