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

package com.hypixel.hytale.builtin.teleport.commands.warp;

import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.server.core.universe.world.World;
import java.util.Map;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.RemoveReason;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.core.universe.Universe;
import com.hypixel.hytale.builtin.teleport.Warp;
import com.hypixel.hytale.builtin.teleport.TeleportPlugin;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.permissions.HytalePermissions;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgumentType;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgTypes;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.command.system.arguments.system.RequiredArg;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.CommandBase;

public class WarpRemoveCommand extends CommandBase
{
    private static final Message MESSAGE_COMMANDS_TELEPORT_WARP_NOT_LOADED;
    @Nonnull
    private final RequiredArg<String> nameArg;
    
    public WarpRemoveCommand() {
        super("remove", "server.commands.warp.remove.desc");
        this.nameArg = this.withRequiredArg("name", "server.commands.warp.remove.name.desc", ArgTypes.STRING);
        this.requirePermission(HytalePermissions.fromCommand("warp.remove"));
    }
    
    @Override
    protected void executeSync(@Nonnull final CommandContext context) {
        if (!TeleportPlugin.get().isWarpsLoaded()) {
            context.sendMessage(WarpRemoveCommand.MESSAGE_COMMANDS_TELEPORT_WARP_NOT_LOADED);
            return;
        }
        final Map<String, Warp> warps = TeleportPlugin.get().getWarps();
        final String warpName = this.nameArg.get(context).toLowerCase();
        final Warp old = warps.remove(warpName);
        if (old == null) {
            context.sendMessage(Message.translation("server.commands.teleport.warp.unknownWarp").param("name", warpName));
        }
        else {
            TeleportPlugin.get().saveWarps();
            context.sendMessage(Message.translation("server.commands.teleport.warp.removedWarp").param("name", warpName));
            final World targetWorld = Universe.get().getWorld(old.getWorld());
            if (targetWorld != null) {
                final ComponentType<EntityStore, TeleportPlugin.WarpComponent> warpComponentType = TeleportPlugin.WarpComponent.getComponentType();
                final Store<EntityStore> store = targetWorld.getEntityStore().getStore();
                targetWorld.execute(() -> store.forEachEntityParallel(warpComponentType, (index, archetypeChunk, commandBuffer) -> {
                    final TeleportPlugin.WarpComponent warpComponent = archetypeChunk.getComponent(index, warpComponentType);
                    if (warpComponent != null && warpComponent.warp().getId().equals(old.getId())) {
                        commandBuffer.removeEntity(archetypeChunk.getReferenceTo(index), RemoveReason.REMOVE);
                    }
                }));
            }
        }
    }
    
    static {
        MESSAGE_COMMANDS_TELEPORT_WARP_NOT_LOADED = Message.translation("server.commands.teleport.warp.notLoaded");
    }
}
