// 
// 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 com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.math.vector.Vector3i;
import com.hypixel.hytale.server.core.prefab.selection.standard.BlockSelection;
import com.hypixel.hytale.builtin.buildertools.prefabeditor.PrefabEditingMetadata;
import com.hypixel.hytale.builtin.buildertools.prefabeditor.PrefabEditSession;
import com.hypixel.hytale.builtin.buildertools.prefabeditor.PrefabEditSessionManager;
import java.util.UUID;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.builtin.buildertools.BuilderToolsPlugin;
import com.hypixel.hytale.server.core.entity.entities.Player;
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 javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.command.system.arguments.system.FlagArg;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;

public class PrefabEditUpdateBoxCommand extends AbstractPlayerCommand
{
    @Nonnull
    private final FlagArg confirmAnchorDeletionArg;
    
    public PrefabEditUpdateBoxCommand() {
        super("setBox", "server.commands.editprefab.setbox.desc");
        this.confirmAnchorDeletionArg = this.withFlagArg("confirm", "server.commands.editprefab.setbox.confirm.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 Player playerComponent = store.getComponent(ref, Player.getComponentType());
        assert playerComponent != null;
        final UUID playerUUID = playerRef.getUuid();
        final PrefabEditSessionManager prefabEditSessionManager = BuilderToolsPlugin.get().getPrefabEditSessionManager();
        final PrefabEditSession prefabEditSession = prefabEditSessionManager.getPrefabEditSession(playerUUID);
        if (prefabEditSession == null) {
            context.sendMessage(Message.translation("server.commands.editprefab.notInEditSession"));
            return;
        }
        final PrefabEditingMetadata selectedPrefab = prefabEditSession.getSelectedPrefab(playerUUID);
        if (selectedPrefab == null) {
            context.sendMessage(Message.translation("server.commands.editprefab.noPrefabSelected"));
            return;
        }
        boolean didMoveAnchor = false;
        final BlockSelection currSelection = BuilderToolsPlugin.getState(playerComponent, playerRef).getSelection();
        if (currSelection != null && !this.isLocationWithinSelection(selectedPrefab.getAnchorEntityPosition(), currSelection)) {
            if (!((Argument<Arg, Boolean>)this.confirmAnchorDeletionArg).get(context)) {
                context.sendMessage(Message.translation("server.commands.editprefab.setbox.anchorOutsideNewSelection"));
                return;
            }
            didMoveAnchor = true;
            selectedPrefab.setAnchorPoint(currSelection.getSelectionMin(), world);
            selectedPrefab.sendAnchorHighlightingPacket(playerRef.getPacketHandler());
        }
        final boolean finalDidMoveAnchor = didMoveAnchor;
        BuilderToolsPlugin.addToQueue(playerComponent, playerRef, (r, s, componentAccessor) -> {
            final BlockSelection selection = s.getSelection();
            if (selection == null) {
                context.sendMessage(Message.translation("server.commands.editprefab.noSelection"));
            }
            else {
                final Vector3i selectionMin = selection.getSelectionMin();
                final Vector3i selectionMax = selection.getSelectionMax();
                prefabEditSession.updatePrefabBounds(selectedPrefab.getUuid(), selectionMin, selectionMax);
                context.sendMessage(Message.translation("server.commands.editprefab.setbox.success"));
                if (finalDidMoveAnchor) {
                    context.sendMessage(Message.translation("server.commands.editprefab.setbox.success.movedAnchor"));
                }
            }
        });
    }
    
    public boolean isLocationWithinSelection(@Nonnull final Vector3i location, @Nonnull final BlockSelection selection) {
        final Vector3i selectionMin = selection.getSelectionMin();
        final Vector3i selectionMax = selection.getSelectionMax();
        return location.x >= selectionMin.x && location.x <= selectionMax.x && location.y >= selectionMin.y && location.y <= selectionMax.y && location.z >= selectionMin.z && location.z <= selectionMax.z;
    }
}
