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

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

import java.util.Iterator;
import com.hypixel.hytale.protocol.BlockPosition;
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.Vector3i;
import com.hypixel.hytale.builtin.buildertools.BuilderToolsPlugin;
import com.hypixel.hytale.server.core.universe.PlayerRef;
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 PrefabSetAnchorInteraction extends SimpleInstantInteraction
{
    @Nonnull
    private static final Message MESSAGE_COMMANDS_EDIT_PREFAB_NOT_IN_EDIT_SESSION;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_EDIT_PREFAB_ANCHOR_ERROR_NO_ANCHOR_FOUND;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_EDIT_PREFAB_SELECT_ERROR_NO_PREFAB_FOUND;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_EDIT_PREFAB_ANCHOR_SUCCESS;
    @Nonnull
    public static final BuilderCodec<PrefabSetAnchorInteraction> 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 PlayerRef playerRefComponent = commandBuffer.getComponent(ref, PlayerRef.getComponentType());
        assert playerRefComponent != null;
        final UUID playerUuid = uuidComponent.getUuid();
        final PrefabEditSessionManager prefabEditSessionManager = BuilderToolsPlugin.get().getPrefabEditSessionManager();
        final PrefabEditSession prefabEditSession = prefabEditSessionManager.getPrefabEditSession(playerUuid);
        if (prefabEditSession == null) {
            playerRefComponent.sendMessage(PrefabSetAnchorInteraction.MESSAGE_COMMANDS_EDIT_PREFAB_NOT_IN_EDIT_SESSION);
            return;
        }
        PrefabEditingMetadata prefabEditingMetadata = null;
        final BlockPosition targetBlock = context.getTargetBlock();
        if (targetBlock == null) {
            playerRefComponent.sendMessage(PrefabSetAnchorInteraction.MESSAGE_COMMANDS_EDIT_PREFAB_ANCHOR_ERROR_NO_ANCHOR_FOUND);
            return;
        }
        final Vector3i targetBlockPos = new Vector3i(targetBlock.x, targetBlock.y, targetBlock.z);
        for (final PrefabEditingMetadata value : prefabEditSession.getLoadedPrefabMetadata().values()) {
            final boolean isWithinPrefab = value.isLocationWithinPrefabBoundingBox(targetBlockPos);
            if (isWithinPrefab) {
                prefabEditingMetadata = value;
                break;
            }
        }
        if (prefabEditingMetadata == null) {
            playerRefComponent.sendMessage(PrefabSetAnchorInteraction.MESSAGE_COMMANDS_EDIT_PREFAB_SELECT_ERROR_NO_PREFAB_FOUND);
            return;
        }
        prefabEditSession.setSelectedPrefab(ref, prefabEditingMetadata, commandBuffer);
        prefabEditingMetadata.setAnchorPoint(targetBlockPos, commandBuffer.getExternalData().getWorld());
        prefabEditingMetadata.sendAnchorHighlightingPacket(playerRefComponent.getPacketHandler());
        playerRefComponent.sendMessage(PrefabSetAnchorInteraction.MESSAGE_COMMANDS_EDIT_PREFAB_ANCHOR_SUCCESS);
    }
    
    static {
        MESSAGE_COMMANDS_EDIT_PREFAB_NOT_IN_EDIT_SESSION = Message.translation("server.commands.editprefab.notInEditSession");
        MESSAGE_COMMANDS_EDIT_PREFAB_ANCHOR_ERROR_NO_ANCHOR_FOUND = Message.translation("server.commands.editprefab.anchor.error.noAnchorFound");
        MESSAGE_COMMANDS_EDIT_PREFAB_SELECT_ERROR_NO_PREFAB_FOUND = Message.translation("server.commands.editprefab.select.error.noPrefabFound");
        MESSAGE_COMMANDS_EDIT_PREFAB_ANCHOR_SUCCESS = Message.translation("server.commands.editprefab.anchor.success");
        CODEC = BuilderCodec.builder(PrefabSetAnchorInteraction.class, PrefabSetAnchorInteraction::new, SimpleInstantInteraction.CODEC).documentation("Sets the prefab anchor.").build();
    }
}
