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

package com.hypixel.hytale.builtin.buildertools.prefabeditor.commands;

import com.hypixel.hytale.server.core.command.system.arguments.system.Argument;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.util.TargetUtil;
import com.hypixel.hytale.math.vector.Vector3i;
import java.util.Iterator;
import com.hypixel.hytale.builtin.buildertools.prefabeditor.PrefabEditSession;
import com.hypixel.hytale.builtin.buildertools.prefabeditor.PrefabEditSessionManager;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.builtin.buildertools.prefabeditor.PrefabEditingMetadata;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.builtin.buildertools.BuilderToolsPlugin;
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.system.FlagArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;

public class PrefabEditSelectCommand extends AbstractPlayerCommand
{
    @Nonnull
    private static final Message MESSAGE_COMMANDS_EDIT_PREFAB_SELECT_ERROR_NO_TARGET_FOUND;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_EDIT_PREFAB_SELECT_ERROR_NO_PREFAB_FOUND;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_EDIT_PREFAB_NOT_IN_EDIT_SESSION;
    @Nonnull
    private final FlagArg nearestArg;
    
    public PrefabEditSelectCommand() {
        super("select", "server.commands.editprefab.select.desc");
        this.nearestArg = this.withFlagArg("nearest", "server.commands.editprefab.select.nearest.desc");
    }
    
    @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 PrefabEditSessionManager prefabEditSessionManager = BuilderToolsPlugin.get().getPrefabEditSessionManager();
        final PrefabEditSession prefabEditSession = prefabEditSessionManager.getPrefabEditSession(playerRef.getUuid());
        if (prefabEditSession == null) {
            context.sendMessage(PrefabEditSelectCommand.MESSAGE_COMMANDS_EDIT_PREFAB_NOT_IN_EDIT_SESSION);
            return;
        }
        PrefabEditingMetadata prefabEditingMetadata = null;
        if (((Argument<Arg, Boolean>)this.nearestArg).get(context)) {
            final TransformComponent transformComponent = store.getComponent(ref, TransformComponent.getComponentType());
            assert transformComponent != null;
            final Vector3d playerLocation = transformComponent.getPosition().clone();
            playerLocation.setY(0.0);
            double distance = 2.147483647E9;
            for (final PrefabEditingMetadata value : prefabEditSession.getLoadedPrefabMetadata().values()) {
                final Vector3d centerPoint = new Vector3d((value.getMaxPoint().x + value.getMinPoint().x) / 2.0, 0.0, (value.getMaxPoint().z + value.getMinPoint().z) / 2.0);
                final double distanceTo = centerPoint.distanceTo(playerLocation);
                if (distance > distanceTo) {
                    distance = distanceTo;
                    prefabEditingMetadata = value;
                }
            }
        }
        else {
            final Vector3i targetLocation = getTargetLocation(ref, store);
            if (targetLocation == null) {
                context.sendMessage(PrefabEditSelectCommand.MESSAGE_COMMANDS_EDIT_PREFAB_SELECT_ERROR_NO_TARGET_FOUND);
                return;
            }
            for (final PrefabEditingMetadata value2 : prefabEditSession.getLoadedPrefabMetadata().values()) {
                final boolean isWithinPrefab = value2.isLocationWithinPrefabBoundingBox(targetLocation);
                if (isWithinPrefab) {
                    prefabEditingMetadata = value2;
                    break;
                }
            }
        }
        if (prefabEditingMetadata == null) {
            context.sendMessage(PrefabEditSelectCommand.MESSAGE_COMMANDS_EDIT_PREFAB_SELECT_ERROR_NO_PREFAB_FOUND);
            return;
        }
        final Player playerComponent = store.getComponent(ref, Player.getComponentType());
        assert playerComponent != null;
        prefabEditSession.setSelectedPrefab(ref, prefabEditingMetadata, store);
    }
    
    @Nullable
    private static Vector3i getTargetLocation(@Nonnull final Ref<EntityStore> ref, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        final Vector3i targetBlock = TargetUtil.getTargetBlock(ref, 200.0, componentAccessor);
        if (targetBlock != null) {
            return targetBlock;
        }
        final Ref<EntityStore> targetEntityRef = TargetUtil.getTargetEntity(ref, componentAccessor);
        if (targetEntityRef == null || !targetEntityRef.isValid()) {
            return null;
        }
        final TransformComponent entityTransformComponent = componentAccessor.getComponent(targetEntityRef, TransformComponent.getComponentType());
        if (entityTransformComponent == null) {
            return null;
        }
        return entityTransformComponent.getPosition().toVector3i();
    }
    
    static {
        MESSAGE_COMMANDS_EDIT_PREFAB_SELECT_ERROR_NO_TARGET_FOUND = Message.translation("server.commands.editprefab.select.error.noTargetFound");
        MESSAGE_COMMANDS_EDIT_PREFAB_SELECT_ERROR_NO_PREFAB_FOUND = Message.translation("server.commands.editprefab.select.error.noPrefabFound");
        MESSAGE_COMMANDS_EDIT_PREFAB_NOT_IN_EDIT_SESSION = Message.translation("server.commands.editprefab.notInEditSession");
    }
}
