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

package com.hypixel.hytale.builtin.path.commands;

import com.hypixel.hytale.builtin.path.path.IPrefabPath;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.builtin.path.WorldPathData;
import com.hypixel.hytale.server.core.command.system.exceptions.GeneralCommandException;
import com.hypixel.hytale.builtin.buildertools.BuilderToolsPlugin;
import com.hypixel.hytale.server.core.entity.entities.Player;
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 javax.annotation.Nonnull;
import java.util.UUID;
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.AbstractPlayerCommand;

public class PrefabPathMergeCommand extends AbstractPlayerCommand
{
    public static final Message MESSAGE_COMMANDS_NPC_PATH_MERGE_NO_ACTIVE_PATH;
    @Nonnull
    private final RequiredArg<UUID> targetPathIdArg;
    
    public PrefabPathMergeCommand() {
        super("merge", "server.commands.npcpath.merge.desc");
        this.targetPathIdArg = this.withRequiredArg("pathName", "server.commands.npcpath.merge.pathName.desc", ArgTypes.UUID);
    }
    
    @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 Player playerComponent = store.getComponent(ref, Player.getComponentType());
        assert playerComponent != null;
        final UUID activePathId = BuilderToolsPlugin.getState(playerComponent, playerRef).getActivePrefabPath();
        if (activePathId == null) {
            throw new GeneralCommandException(PrefabPathMergeCommand.MESSAGE_COMMANDS_NPC_PATH_MERGE_NO_ACTIVE_PATH);
        }
        final UUID targetPathId = this.targetPathIdArg.get(context);
        final WorldPathData worldPathData = store.getResource(WorldPathData.getResourceType());
        final IPrefabPath activePath = worldPathData.getPrefabPath(0, activePathId, false);
        if (activePath == null || !activePath.isFullyLoaded()) {
            playerRef.sendMessage(Message.translation("server.npc.npcpath.pathMustBeLoaded").param("path", activePathId.toString()));
            return;
        }
        final IPrefabPath targetPath = worldPathData.getPrefabPath(0, targetPathId, false);
        if (targetPath == null || !targetPath.isFullyLoaded()) {
            playerRef.sendMessage(Message.translation("server.npc.npcpath.pathMustBeLoaded").param("path", targetPathId.toString()));
            return;
        }
        targetPath.mergeInto(activePath, targetPath.getWorldGenId(), store);
        worldPathData.removePrefabPath(0, targetPathId);
        playerRef.sendMessage(Message.translation("server.npc.npcpath.pathMergedInto").param("targetPathName", targetPathId.toString()).param("activePathName", activePathId.toString()));
    }
    
    static {
        MESSAGE_COMMANDS_NPC_PATH_MERGE_NO_ACTIVE_PATH = Message.translation("server.commands.npcpath.merge.noActivePath");
    }
}
