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

package com.hypixel.hytale.server.core.command.commands.world.entity;

import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.server.core.modules.entity.tracker.EntityTrackerSystems;
import javax.annotation.Nullable;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.RemoveReason;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.component.ComponentAccessor;
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.system.FlagArg;
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 class EntityRemoveCommand extends AbstractWorldCommand
{
    @Nonnull
    private static final Message MESSAGE_GENERAL_NO_ENTITY_IN_VIEW;
    @Nonnull
    private final EntityWrappedArg entityArg;
    @Nonnull
    private final FlagArg othersFlag;
    
    public EntityRemoveCommand() {
        super("remove", "server.commands.entity.remove.desc");
        this.entityArg = this.withOptionalArg("entity", "server.commands.entity.remove.entity.desc", ArgTypes.ENTITY_ID);
        this.othersFlag = this.withFlagArg("others", "server.commands.entity.remove.others.desc");
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nonnull final World world, @Nonnull final Store<EntityStore> store) {
        final Ref<EntityStore> entityRef = this.entityArg.get(store, context);
        if (entityRef == null || !entityRef.isValid()) {
            context.sendMessage(EntityRemoveCommand.MESSAGE_GENERAL_NO_ENTITY_IN_VIEW);
            return;
        }
        if (this.othersFlag.provided(context)) {
            store.forEachEntityParallel((index, archetypeChunk, commandBuffer) -> {
                if (!archetypeChunk.getArchetype().contains(Player.getComponentType())) {
                    if (!archetypeChunk.getReferenceTo(index).equals(entityRef)) {
                        commandBuffer.removeEntity(archetypeChunk.getReferenceTo(index), RemoveReason.REMOVE);
                    }
                }
            });
        }
        else {
            removeEntity(context.senderAsPlayerRef(), entityRef, store);
        }
    }
    
    public static void removeEntity(@Nullable final Ref<EntityStore> playerRef, @Nonnull final Ref<EntityStore> entityReference, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        if (playerRef != null && playerRef.isValid()) {
            final EntityTrackerSystems.EntityViewer entityViewer = componentAccessor.getComponent(playerRef, EntityTrackerSystems.EntityViewer.getComponentType());
            if (entityViewer != null && !entityViewer.visible.contains(entityReference)) {
                final PlayerRef playerRefComponent = componentAccessor.getComponent(playerRef, PlayerRef.getComponentType());
                assert playerRefComponent != null;
                playerRefComponent.sendMessage(Message.translation("server.general.entity.remove.unableToRemove").param("id", entityReference.getIndex()));
                return;
            }
        }
        componentAccessor.removeEntity(entityReference, EntityStore.REGISTRY.newHolder(), RemoveReason.REMOVE);
    }
    
    static {
        MESSAGE_GENERAL_NO_ENTITY_IN_VIEW = Message.translation("server.general.noEntityInView");
    }
}
