// 
// 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.server.core.entity.Entity;
import com.hypixel.hytale.builtin.buildertools.BuilderToolsPlugin;
import com.hypixel.hytale.builtin.path.WorldPathData;
import com.hypixel.hytale.builtin.path.entities.PatrolPathMarkerEntity;
import com.hypixel.hytale.server.core.entity.EntityUtils;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.util.TargetUtil;
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 java.util.UUID;
import com.hypixel.hytale.server.core.command.system.arguments.system.OptionalArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;

public class PrefabPathEditCommand extends AbstractPlayerCommand
{
    @Nonnull
    private static final Message MESSAGE_COMMANDS_NPC_PATH_EDIT_NO_ENTITY_IN_VIEW;
    @Nonnull
    private final OptionalArg<UUID> pathIdArg;
    
    public PrefabPathEditCommand() {
        super("edit", "server.commands.npcpath.edit.desc");
        this.pathIdArg = this.withOptionalArg("pathId", "server.commands.npcpath.edit.pathId.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;
        UUID pathId;
        if (this.pathIdArg.provided(context)) {
            pathId = this.pathIdArg.get(context);
        }
        else {
            final Ref<EntityStore> entityRef = TargetUtil.getTargetEntity(ref, store);
            if (entityRef == null || !entityRef.isValid()) {
                return;
            }
            final Entity entity = EntityUtils.getEntity(ref, store);
            if (!(entity instanceof PatrolPathMarkerEntity)) {
                context.sendMessage(PrefabPathEditCommand.MESSAGE_COMMANDS_NPC_PATH_EDIT_NO_ENTITY_IN_VIEW);
                return;
            }
            final PatrolPathMarkerEntity pathMarkerEntity = (PatrolPathMarkerEntity)entity;
            pathId = pathMarkerEntity.getPathId();
        }
        final WorldPathData worldPathData = store.getResource(WorldPathData.getResourceType());
        final IPrefabPath path = worldPathData.getPrefabPath(0, pathId, false);
        if (path == null) {
            context.sendMessage(Message.translation("server.npc.npcpath.pathMustBeLoaded").param("path", pathId.toString()));
            return;
        }
        BuilderToolsPlugin.getState(playerComponent, playerRef).setActivePrefabPath(pathId);
        context.sendMessage(Message.translation("server.npc.npcpath.editingPath").param("path", pathId.toString()));
    }
    
    static {
        MESSAGE_COMMANDS_NPC_PATH_EDIT_NO_ENTITY_IN_VIEW = Message.translation("server.commands.npcpath.edit.noEntityInView");
    }
}
