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

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

import com.hypixel.hytale.math.shape.Box;
import com.hypixel.hytale.assetstore.map.IndexedLookupTableAssetMap;
import com.hypixel.hytale.assetstore.map.BlockTypeAssetMap;
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.server.core.util.FillerBlockUtil;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.server.core.asset.type.blockhitbox.BlockBoundingBoxes;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.BlockType;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import com.hypixel.hytale.server.core.universe.world.chunk.section.BlockSection;
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 javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;

public class BlockInspectFillerCommand extends AbstractPlayerCommand
{
    @Nonnull
    private static final Message MESSAGE_COMMANDS_BLOCK_INSPECT_FILLER_DONE;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_BLOCK_INSPECT_FILLER_NO_BLOCKS;
    
    public BlockInspectFillerCommand() {
        super("inspectfiller", "server.commands.block.inspectfiller.desc");
        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 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 BlockSection blockSection = chunkStore.getComponent(chunk, BlockSection.getComponentType());
            if (blockSection == null) {
                playerRef.sendMessage(BlockInspectFillerCommand.MESSAGE_COMMANDS_BLOCK_INSPECT_FILLER_NO_BLOCKS);
            }
            else {
                final BlockTypeAssetMap<String, BlockType> blockTypeMap = BlockType.getAssetMap();
                final IndexedLookupTableAssetMap<String, BlockBoundingBoxes> hitboxMap = BlockBoundingBoxes.getAssetMap();
                final Vector3d offset = new Vector3d(ChunkUtil.minBlock(chunkX), ChunkUtil.minBlock(chunkY), ChunkUtil.minBlock(chunkZ));
                for (int idx = 0; idx < 32768; ++idx) {
                    final int blockId = blockSection.get(idx);
                    final BlockType blockType = blockTypeMap.getAsset(blockId);
                    if (blockType != null) {
                        final BlockBoundingBoxes hitbox = hitboxMap.getAsset(blockType.getHitboxTypeIndex());
                        if (hitbox != null) {
                            if (!(!hitbox.protrudesUnitBox())) {
                                final int filler = blockSection.getFiller(idx);
                                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);
                                final int rotation = blockSection.getRotationIndex(idx);
                                final BlockBoundingBoxes.RotatedVariantBoxes rotatedHitbox = hitbox.get(rotation);
                                final int fillerX = FillerBlockUtil.unpackX(filler);
                                final int fillerY = FillerBlockUtil.unpackY(filler);
                                final int fillerZ = FillerBlockUtil.unpackZ(filler);
                                final Box boundingBox = rotatedHitbox.getBoundingBox();
                                int minX = (int)boundingBox.min.x;
                                int minY = (int)boundingBox.min.y;
                                int minZ = (int)boundingBox.min.z;
                                if (minX - boundingBox.min.x > 0.0) {
                                    --minX;
                                }
                                if (minY - boundingBox.min.y > 0.0) {
                                    --minY;
                                }
                                if (minZ - boundingBox.min.z > 0.0) {
                                    --minZ;
                                }
                                int maxX = (int)boundingBox.max.x;
                                int maxY = (int)boundingBox.max.y;
                                int maxZ = (int)boundingBox.max.z;
                                if (boundingBox.max.x - maxX > 0.0) {
                                    ++maxX;
                                }
                                if (boundingBox.max.y - maxY > 0.0) {
                                    ++maxY;
                                }
                                if (boundingBox.max.z - maxZ > 0.0) {
                                    ++maxZ;
                                }
                                final Vector3f colour = new Vector3f();
                                colour.x = (fillerX - minX) / (float)(maxX - minX);
                                colour.y = (fillerY - minY) / (float)(maxY - minY);
                                colour.z = (fillerZ - minZ) / (float)(maxZ - minZ);
                                DebugUtils.addCube(chunkStore.getExternalData().getWorld(), pos, colour, 1.05, 30.0f);
                            }
                        }
                    }
                }
                playerRef.sendMessage(BlockInspectFillerCommand.MESSAGE_COMMANDS_BLOCK_INSPECT_FILLER_DONE);
            }
        }, (Executor)world));
    }
    
    static {
        MESSAGE_COMMANDS_BLOCK_INSPECT_FILLER_DONE = Message.translation("server.commands.block.inspectfiller.done");
        MESSAGE_COMMANDS_BLOCK_INSPECT_FILLER_NO_BLOCKS = Message.translation("server.commands.block.inspectfiller.noblocks");
    }
}
