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

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

import com.hypixel.hytale.server.core.asset.type.model.config.Model;
import com.hypixel.hytale.component.Holder;
import com.hypixel.hytale.math.vector.Vector3f;
import com.hypixel.hytale.component.AddReason;
import com.hypixel.hytale.server.core.entity.nameplate.Nameplate;
import com.hypixel.hytale.server.core.modules.entity.component.PersistentModel;
import com.hypixel.hytale.server.core.modules.entity.component.ModelComponent;
import com.hypixel.hytale.server.core.modules.entity.component.HiddenFromAdventurePlayers;
import com.hypixel.hytale.server.core.entity.UUIDComponent;
import com.hypixel.hytale.server.spawning.suppression.component.SpawnSuppressionComponent;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgumentType;
import com.hypixel.hytale.server.core.command.system.arguments.system.RequiredArg;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;
import com.hypixel.hytale.server.core.universe.Universe;
import com.hypixel.hytale.server.spawning.SpawningPlugin;
import com.hypixel.hytale.logger.HytaleLogger;
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.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractWorldCommand;
import com.hypixel.hytale.server.spawning.suppression.SpawnSuppressorEntry;
import com.hypixel.hytale.server.spawning.suppression.component.ChunkSuppressionEntry;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import com.hypixel.hytale.server.npc.NPCPlugin;
import com.hypixel.hytale.server.spawning.suppression.component.SpawnSuppressionController;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.command.system.AbstractCommand;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.spawning.assets.spawnsuppression.SpawnSuppression;
import com.hypixel.hytale.server.core.command.system.arguments.types.AssetArgumentType;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractCommandCollection;

public class SpawnSuppressionCommand extends AbstractCommandCollection
{
    @Nonnull
    private static final AssetArgumentType<SpawnSuppression, ?> SPAWN_SUPPRESSION_ASSET_TYPE;
    
    public SpawnSuppressionCommand() {
        super("suppression", "server.commands.spawning.suppression.desc");
        this.addSubCommand(new Dump());
        this.addSubCommand(new DumpAll());
        this.addSubCommand(new Add());
    }
    
    @Nonnull
    private static String dumpWorld(@Nonnull final World world) {
        final SpawnSuppressionController spawnSuppressionController = world.getEntityStore().getStore().getResource(SpawnSuppressionController.getResourceType());
        final StringBuilder sb = new StringBuilder("World: ").append(world.getName()).append("\n  Spawn Suppressors:");
        spawnSuppressionController.getSpawnSuppressorMap().values().forEach(entry -> sb.append("\n    ").append(entry.getSuppressionId()).append(": ").append(entry.getPosition()));
        sb.append("\n  Chunk Annotations:");
        spawnSuppressionController.getChunkSuppressionMap().forEach((index, entry) -> {
            sb.append("\n    Chunk ").append(index).append(": ");
            final List<ChunkSuppressionEntry.SuppressionSpan> suppressionSpans = entry.getSuppressionSpans();
            suppressionSpans.forEach(span -> {
                sb.append("\n      From y=").append(span.getMinY()).append(" to y=").append(span.getMaxY());
                sb.append("\n      Suppressing Roles: [ ");
                if (span.getSuppressedRoles() != null) {
                    sb.append(span.getSuppressedRoles().stream().map(roleIndex -> NPCPlugin.get().getName(roleIndex)).collect((Collector<? super Object, ?, String>)Collectors.joining(", ")));
                }
                sb.append(" ]");
            });
            return;
        });
        return sb.toString();
    }
    
    static {
        SPAWN_SUPPRESSION_ASSET_TYPE = new AssetArgumentType<SpawnSuppression, Object>("server.commands.spawning.suppression.arg.suppression.name", SpawnSuppression.class, "server.commands.spawning.suppression.arg.suppression.usage");
    }
    
    private static class Dump extends AbstractWorldCommand
    {
        @Nonnull
        private static final Message MESSAGE_COMMANDS_SPAWNING_SUPPRESSION_DUMP_DUMPED;
        
        public Dump() {
            super("dump", "server.commands.spawning.suppression.dump.desc");
        }
        
        @Override
        protected void execute(@Nonnull final CommandContext context, @Nonnull final World world, @Nonnull final Store<EntityStore> store) {
            final String dump = SpawnSuppressionCommand.dumpWorld(world);
            context.sendMessage(Dump.MESSAGE_COMMANDS_SPAWNING_SUPPRESSION_DUMP_DUMPED);
            SpawningPlugin.get().getLogger().atInfo().log(dump);
        }
        
        static {
            MESSAGE_COMMANDS_SPAWNING_SUPPRESSION_DUMP_DUMPED = Message.translation("server.commands.spawning.suppression.dump.dumped");
        }
    }
    
    private static class DumpAll extends AbstractWorldCommand
    {
        @Nonnull
        private static final Message MESSAGE_COMMANDS_SPAWNING_SUPPRESSION_DUMP_DUMPED;
        
        public DumpAll() {
            super("dumpall", "server.commands.spawning.suppression.dump.all.desc");
        }
        
        @Override
        protected void execute(@Nonnull final CommandContext context, @Nonnull final World world, @Nonnull final Store<EntityStore> store) {
            Universe.get().getWorlds().values().forEach(w -> SpawningPlugin.get().getLogger().atInfo().log(SpawnSuppressionCommand.dumpWorld(w)));
            context.sendMessage(DumpAll.MESSAGE_COMMANDS_SPAWNING_SUPPRESSION_DUMP_DUMPED);
        }
        
        static {
            MESSAGE_COMMANDS_SPAWNING_SUPPRESSION_DUMP_DUMPED = Message.translation("server.commands.spawning.suppression.dump.dumped");
        }
    }
    
    private static class Add extends AbstractPlayerCommand
    {
        @Nonnull
        private final RequiredArg<SpawnSuppression> suppressionArg;
        
        public Add() {
            super("add", "server.commands.spawning.suppression.add.desc");
            this.suppressionArg = this.withRequiredArg("suppression", "server.commands.spawning.suppression.add.arg.suppression.desc", (ArgumentType<SpawnSuppression>)SpawnSuppressionCommand.SPAWN_SUPPRESSION_ASSET_TYPE);
        }
        
        @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 TransformComponent transformComponent = store.getComponent(ref, TransformComponent.getComponentType());
            assert transformComponent != null;
            final SpawnSuppression spawnSuppression = this.suppressionArg.get(context);
            final Vector3f rotation = transformComponent.getRotation();
            final Holder<EntityStore> holder = EntityStore.REGISTRY.newHolder();
            holder.addComponent(SpawnSuppressionComponent.getComponentType(), new SpawnSuppressionComponent(spawnSuppression.getId()));
            holder.addComponent(TransformComponent.getComponentType(), new TransformComponent(transformComponent.getPosition(), rotation));
            holder.ensureComponent(UUIDComponent.getComponentType());
            holder.ensureComponent(HiddenFromAdventurePlayers.getComponentType());
            final Model model = SpawningPlugin.get().getSpawnMarkerModel();
            holder.addComponent(ModelComponent.getComponentType(), new ModelComponent(model));
            holder.addComponent(PersistentModel.getComponentType(), new PersistentModel(model.toReference()));
            final Nameplate nameplate = new Nameplate("SpawnSuppression: " + String.valueOf(spawnSuppression));
            holder.addComponent(Nameplate.getComponentType(), nameplate);
            store.addEntity(holder, AddReason.SPAWN);
            context.sendMessage(Message.translation("server.commands.spawning.suppression.add.added").param("suppressionId", spawnSuppression.getId()));
        }
    }
}
