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

package com.hypixel.hytale.builtin.instances.command;

import java.util.concurrent.CompletableFuture;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.builtin.instances.InstancesPlugin;
import com.hypixel.hytale.math.vector.Transform;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.codec.validation.Validator;
import com.hypixel.hytale.builtin.instances.InstanceValidator;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgumentType;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgTypes;
import com.hypixel.hytale.math.vector.Vector3f;
import com.hypixel.hytale.server.core.command.system.arguments.system.DefaultArg;
import com.hypixel.hytale.server.core.command.system.arguments.types.RelativeDoublePosition;
import com.hypixel.hytale.server.core.command.system.arguments.system.OptionalArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.command.system.arguments.system.RequiredArg;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;

public class InstanceSpawnCommand extends AbstractPlayerCommand
{
    @Nonnull
    private final RequiredArg<String> instanceNameArg;
    @Nonnull
    private final OptionalArg<RelativeDoublePosition> positionArg;
    @Nonnull
    private final DefaultArg<Vector3f> rotationArg;
    
    public InstanceSpawnCommand() {
        super("spawn", "server.commands.instances.spawn.desc");
        this.instanceNameArg = this.withRequiredArg("instanceName", "server.commands.instances.spawn.arg.name", ArgTypes.STRING).addValidator((Validator<String>)new InstanceValidator());
        this.positionArg = this.withOptionalArg("position", "server.commands.instances.spawn.arg.position", ArgTypes.RELATIVE_POSITION);
        this.rotationArg = this.withDefaultArg("rotation", "server.commands.instances.spawn.arg.rotation", ArgTypes.ROTATION, Vector3f.FORWARD, "server.commands.instances.spawn.arg.rotation.default");
        this.addAliases("sp");
    }
    
    protected Vector3f getSpawnRotation(@Nonnull final Ref<EntityStore> ref, @Nonnull final CommandContext context, @Nonnull final DefaultArg<Vector3f> rotationArg, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        if (rotationArg.provided(context) || !context.isPlayer()) {
            return rotationArg.get(context);
        }
        final TransformComponent headRotationComponent = componentAccessor.getComponent(ref, TransformComponent.getComponentType());
        assert headRotationComponent != null;
        return headRotationComponent.getRotation().clone();
    }
    
    @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) {
        Vector3d position;
        if (!this.positionArg.provided(context)) {
            final TransformComponent transformComponent = store.getComponent(ref, TransformComponent.getComponentType());
            assert transformComponent != null;
            position = transformComponent.getPosition();
        }
        else {
            position = this.positionArg.get(context).getRelativePosition(context, world, store);
        }
        final Transform returnLocation = new Transform(position.clone(), this.getSpawnRotation(ref, context, this.rotationArg, store).clone());
        final String instanceName = this.instanceNameArg.get(context);
        final CompletableFuture<World> instanceWorld = InstancesPlugin.get().spawnInstance(instanceName, world, returnLocation);
        InstancesPlugin.teleportPlayerToLoadingInstance(ref, store, instanceWorld, null);
    }
}
