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

package com.hypixel.hytale.server.core.command.commands.player;

import com.hypixel.hytale.server.core.entity.entities.Player;
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.server.core.command.system.arguments.system.RequiredArg;
import com.hypixel.hytale.server.core.command.system.basecommands.CommandBase;
import com.hypixel.hytale.server.core.universe.world.chunk.WorldChunk;
import com.hypixel.hytale.math.Axis;
import com.hypixel.hytale.math.vector.Vector3i;
import com.hypixel.hytale.math.vector.Vector3f;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.math.util.ChunkUtil;
import com.hypixel.hytale.math.util.MathUtil;
import com.hypixel.hytale.server.core.modules.entity.component.HeadRotation;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import javax.annotation.Nullable;
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.AbstractCommand;
import com.hypixel.hytale.server.core.permissions.HytalePermissions;
import com.hypixel.hytale.protocol.GameMode;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;

public class WhereAmICommand extends AbstractPlayerCommand
{
    @Nonnull
    private static final Message MESSAGE_COMMANDS_WHERE_AM_I_CHUNK_NOT_LOADED;
    
    public WhereAmICommand() {
        super("whereami", "server.commands.whereami.desc");
        this.setPermissionGroup(GameMode.Creative);
        this.requirePermission(HytalePermissions.fromCommand("whereami.self"));
        this.addUsageVariant(new WhereAmIOtherCommand());
    }
    
    @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) {
        sendLocationInfo(context, store, ref, world, null);
    }
    
    private static void sendLocationInfo(@Nonnull final CommandContext context, @Nonnull final Store<EntityStore> store, @Nonnull final Ref<EntityStore> ref, @Nonnull final World world, @Nullable final String targetUsername) {
        final TransformComponent transformComponent = store.getComponent(ref, TransformComponent.getComponentType());
        assert transformComponent != null;
        final HeadRotation headRotationComponent = store.getComponent(ref, HeadRotation.getComponentType());
        assert headRotationComponent != null;
        final Vector3d position = transformComponent.getPosition();
        final Vector3f headRotation = headRotationComponent.getRotation();
        final Vector3i axisDirection = headRotationComponent.getAxisDirection();
        final Axis axis = headRotationComponent.getAxis();
        final Vector3d direction = headRotationComponent.getDirection();
        final int chunkX = MathUtil.floor(position.getX()) >> 5;
        final int chunkY = MathUtil.floor(position.getY()) >> 5;
        final int chunkZ = MathUtil.floor(position.getZ()) >> 5;
        final long chunkIndex = ChunkUtil.indexChunk(chunkX, chunkZ);
        final WorldChunk playerChunk = world.getChunkIfInMemory(chunkIndex);
        final String headerKey = (targetUsername != null) ? "server.commands.whereami.header.other" : "server.commands.whereami.header";
        final Message message = Message.translation(headerKey).param("username", targetUsername).param("world", world.getName()).param("chunkX", chunkX).param("chunkY", chunkY).param("chunkZ", chunkZ).param("posX", position.getX()).param("posY", position.getY()).param("posZ", position.getZ()).param("yaw", headRotation.getYaw()).param("pitch", headRotation.getPitch()).param("roll", headRotation.getRoll()).param("direction", direction.toString()).param("axisDirection", axisDirection.toString()).param("axis", axis.toString());
        if (playerChunk == null) {
            message.insert(WhereAmICommand.MESSAGE_COMMANDS_WHERE_AM_I_CHUNK_NOT_LOADED);
        }
        else {
            message.insert(Message.translation("server.commands.whereami.needsSaving").param("needsSaving", Boolean.toString(playerChunk.getNeedsSaving())));
        }
        context.sendMessage(message);
    }
    
    static {
        MESSAGE_COMMANDS_WHERE_AM_I_CHUNK_NOT_LOADED = Message.translation("server.commands.whereami.chunkNotLoaded");
    }
    
    private static class WhereAmIOtherCommand extends CommandBase
    {
        private static final Message MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD;
        @Nonnull
        private final RequiredArg<PlayerRef> playerArg;
        static final /* synthetic */ boolean $assertionsDisabled;
        
        WhereAmIOtherCommand() {
            super("server.commands.whereami.other.desc");
            this.playerArg = this.withRequiredArg("player", "server.commands.argtype.player.desc", ArgTypes.PLAYER_REF);
            this.setPermissionGroup(GameMode.Creative);
            this.requirePermission(HytalePermissions.fromCommand("whereami.other"));
        }
        
        @Override
        protected void executeSync(@Nonnull final CommandContext context) {
            final PlayerRef targetPlayerRef = this.playerArg.get(context);
            final Ref<EntityStore> ref = targetPlayerRef.getReference();
            if (ref == null || !ref.isValid()) {
                context.sendMessage(WhereAmIOtherCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD);
                return;
            }
            final Store<EntityStore> store = ref.getStore();
            final World world = store.getExternalData().getWorld();
            world.execute(() -> {
                final Player playerComponent = store.getComponent(ref, Player.getComponentType());
                if (playerComponent == null) {
                    context.sendMessage(WhereAmIOtherCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD);
                }
                else {
                    final PlayerRef playerRefComponent = store.getComponent(ref, PlayerRef.getComponentType());
                    if (!WhereAmIOtherCommand.$assertionsDisabled && playerRefComponent == null) {
                        throw new AssertionError();
                    }
                    else {
                        WhereAmICommand.sendLocationInfo(context, store, ref, world, playerRefComponent.getUsername());
                    }
                }
            });
        }
        
        static {
            MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD = Message.translation("server.commands.errors.playerNotInWorld");
        }
    }
}
