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

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

import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.util.TargetUtil;
import com.hypixel.hytale.math.vector.Vector3i;
import java.util.Iterator;
import java.util.UUID;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.builtin.buildertools.BuilderToolsPlugin;
import com.hypixel.hytale.server.core.entity.UUIDComponent;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.modules.interaction.interaction.CooldownHandler;
import com.hypixel.hytale.server.core.entity.InteractionContext;
import com.hypixel.hytale.protocol.InteractionType;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.modules.interaction.interaction.config.SimpleInstantInteraction;

public class PrefabSelectionInteraction extends SimpleInstantInteraction
{
    @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;
    private static final float ENTITY_TARGET_RADIUS = 50.0f;
    @Nonnull
    public static final BuilderCodec<PrefabSelectionInteraction> CODEC;
    
    @Override
    protected void firstRun(@Nonnull final InteractionType type, @Nonnull final InteractionContext context, @Nonnull final CooldownHandler cooldownHandler) {
        final CommandBuffer<EntityStore> commandBuffer = context.getCommandBuffer();
        assert commandBuffer != null;
        final Ref<EntityStore> ref = context.getEntity();
        final Player playerComponent = commandBuffer.getComponent(ref, Player.getComponentType());
        if (playerComponent == null) {
            return;
        }
        if (type != InteractionType.Primary && type != InteractionType.Secondary) {
            return;
        }
        final UUIDComponent uuidComponent = commandBuffer.getComponent(ref, UUIDComponent.getComponentType());
        assert uuidComponent != null;
        final UUID uuid = uuidComponent.getUuid();
        final PrefabEditSessionManager prefabEditSessionManager = BuilderToolsPlugin.get().getPrefabEditSessionManager();
        final PrefabEditSession prefabEditSession = prefabEditSessionManager.getPrefabEditSession(uuid);
        if (prefabEditSession == null) {
            playerComponent.sendMessage(PrefabSelectionInteraction.MESSAGE_COMMANDS_EDIT_PREFAB_NOT_IN_EDIT_SESSION);
            return;
        }
        final TransformComponent transformComponent = commandBuffer.getComponent(ref, TransformComponent.getComponentType());
        assert transformComponent != null;
        final Vector3d playerPosition = transformComponent.getPosition();
        PrefabEditingMetadata prefabEditingMetadata = null;
        if (type == InteractionType.Secondary) {
            final Vector3d playerLocation = playerPosition.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, commandBuffer);
            if (targetLocation == null) {
                playerComponent.sendMessage(PrefabSelectionInteraction.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) {
            playerComponent.sendMessage(PrefabSelectionInteraction.MESSAGE_COMMANDS_EDIT_PREFAB_SELECT_ERROR_NO_PREFAB_FOUND);
            return;
        }
        prefabEditSession.setSelectedPrefab(ref, prefabEditingMetadata, commandBuffer);
    }
    
    @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, 50.0f, 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");
        CODEC = BuilderCodec.builder(PrefabSelectionInteraction.class, PrefabSelectionInteraction::new, SimpleInstantInteraction.CODEC).documentation("Interaction that handles the selection functionally for the prefab selection tool.").build();
    }
}
