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

package com.hypixel.hytale.server.core.universe.world.commands.block;

import com.hypixel.hytale.server.core.command.system.arguments.system.Argument;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.BlockType;
import com.hypixel.hytale.common.util.CompletableFutureUtil;
import java.util.concurrent.Executor;
import com.hypixel.hytale.server.core.modules.debug.DebugUtils;
import com.hypixel.hytale.math.vector.Vector3f;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import com.hypixel.hytale.server.core.blocktype.component.BlockPhysics;
import com.hypixel.hytale.math.util.ChunkUtil;
import com.hypixel.hytale.math.util.MathUtil;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
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.protocol.GameMode;
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 BlockInspectPhysicsCommand extends AbstractPlayerCommand
{
    @Nonnull
    private static final Message MESSAGE_COMMANDS_BLOCK_INSPECT_PHYS_NO_BLOCKS;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_BLOCK_INSPECT_PHYS_DONE;
    private final FlagArg ALL;
    
    public BlockInspectPhysicsCommand() {
        super("inspectphys", "server.commands.block.inspectphys.desc");
        this.ALL = this.withFlagArg("all", "");
        this.setPermissionGroup(null);
    }
    
    @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 TransformComponent transformComponent = store.getComponent(ref, TransformComponent.getComponentType());
        assert transformComponent != null;
        final Vector3d position = transformComponent.getPosition();
        final boolean all = ((Argument<Arg, Boolean>)this.ALL).get(context);
        final int x = MathUtil.floor(position.getX());
        final int z = MathUtil.floor(position.getZ());
        final int y = MathUtil.floor(position.getY());
        final int chunkX = ChunkUtil.chunkCoordinate(x);
        final int chunkY = ChunkUtil.chunkCoordinate(y);
        final int chunkZ = ChunkUtil.chunkCoordinate(z);
        CompletableFutureUtil._catch(world.getChunkStore().getChunkSectionReferenceAsync(chunkX, chunkY, chunkZ).thenAcceptAsync(chunk -> {
            final Store<ChunkStore> chunkStore = chunk.getStore();
            final BlockPhysics blockPhysics = chunkStore.getComponent(chunk, BlockPhysics.getComponentType());
            if (blockPhysics == null) {
                playerRef.sendMessage(BlockInspectPhysicsCommand.MESSAGE_COMMANDS_BLOCK_INSPECT_PHYS_NO_BLOCKS);
            }
            else {
                final Vector3d offset = new Vector3d(ChunkUtil.minBlock(chunkX), ChunkUtil.minBlock(chunkY), ChunkUtil.minBlock(chunkZ));
                for (int idx = 0; idx < 32768; ++idx) {
                    final int supportValue = blockPhysics.get(idx);
                    if (supportValue != 0 || all) {
                        final int bx = ChunkUtil.xFromIndex(idx);
                        final int by = ChunkUtil.yFromIndex(idx);
                        final int bz = ChunkUtil.zFromIndex(idx);
                        final Vector3d pos = new Vector3d(bx, by, bz);
                        pos.add(0.5, 0.5, 0.5);
                        pos.add(offset);
                        Vector3f colour;
                        if (supportValue == 15) {
                            colour = new Vector3f(0.0f, 1.0f, 0.0f);
                        }
                        else {
                            final BlockType block = world.getBlockType(pos.toVector3i());
                            if (!block.hasSupport()) {
                                if (supportValue == 0) {
                                    continue;
                                }
                                else {
                                    colour = new Vector3f(1.0f, 1.0f, 0.0f);
                                }
                            }
                            else if (block.getMaxSupportDistance() != 0) {
                                final float len = supportValue / (float)block.getMaxSupportDistance();
                                colour = new Vector3f(len, 0.0f, 1.0f - len);
                            }
                            else {
                                colour = new Vector3f(0.0f, 1.0f, 1.0f);
                            }
                        }
                        DebugUtils.addCube(chunkStore.getExternalData().getWorld(), pos, colour, 1.05, 30.0f);
                    }
                }
                playerRef.sendMessage(BlockInspectPhysicsCommand.MESSAGE_COMMANDS_BLOCK_INSPECT_PHYS_DONE);
            }
        }, (Executor)world));
    }
    
    static {
        MESSAGE_COMMANDS_BLOCK_INSPECT_PHYS_NO_BLOCKS = Message.translation("server.commands.block.inspectphys.noblocks");
        MESSAGE_COMMANDS_BLOCK_INSPECT_PHYS_DONE = Message.translation("server.commands.block.inspectphys.done");
    }
}
