package net.minecraft.client.renderer.blockentity;

import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import java.util.Iterator;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.core.Vec3i;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.core.BlockPos;
import net.minecraft.client.renderer.LevelRenderer;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.world.level.block.state.properties.StructureMode;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.world.level.block.entity.StructureBlockEntity;

public class StructureBlockRenderer extends BlockEntityRenderer<StructureBlockEntity> {
    public StructureBlockRenderer(final BlockEntityRenderDispatcher dsq) {
        super(dsq);
    }
    
    @Override
    public void render(final StructureBlockEntity bxl, final float float2, final PoseStack cyl, final MultiBufferSource dqt, final int integer5, final int integer6) {
        if (!Minecraft.getInstance().player.canUseGameMasterBlocks() && !Minecraft.getInstance().player.isSpectator()) {
            return;
        }
        final BlockPos fk8 = bxl.getStructurePos();
        final BlockPos fk9 = bxl.getStructureSize();
        if (fk9.getX() < 1 || fk9.getY() < 1 || fk9.getZ() < 1) {
            return;
        }
        if (bxl.getMode() != StructureMode.SAVE && bxl.getMode() != StructureMode.LOAD) {
            return;
        }
        final double double10 = fk8.getX();
        final double double11 = fk8.getZ();
        final double double12 = fk8.getY();
        final double double13 = double12 + fk9.getY();
        double double14 = 0.0;
        double double15 = 0.0;
        switch (bxl.getMirror()) {
            case LEFT_RIGHT: {
                double14 = fk9.getX();
                double15 = -fk9.getZ();
                break;
            }
            case FRONT_BACK: {
                double14 = -fk9.getX();
                double15 = fk9.getZ();
                break;
            }
            default: {
                double14 = fk9.getX();
                double15 = fk9.getZ();
                break;
            }
        }
        double double16 = 0.0;
        double double17 = 0.0;
        double double18 = 0.0;
        double double19 = 0.0;
        switch (bxl.getRotation()) {
            case CLOCKWISE_90: {
                double16 = ((double15 < 0.0) ? double10 : (double10 + 1.0));
                double17 = ((double14 < 0.0) ? (double11 + 1.0) : double11);
                double18 = double16 - double15;
                double19 = double17 + double14;
                break;
            }
            case CLOCKWISE_180: {
                double16 = ((double14 < 0.0) ? double10 : (double10 + 1.0));
                double17 = ((double15 < 0.0) ? double11 : (double11 + 1.0));
                double18 = double16 - double14;
                double19 = double17 - double15;
                break;
            }
            case COUNTERCLOCKWISE_90: {
                double16 = ((double15 < 0.0) ? (double10 + 1.0) : double10);
                double17 = ((double14 < 0.0) ? double11 : (double11 + 1.0));
                double18 = double16 + double15;
                double19 = double17 - double14;
                break;
            }
            default: {
                double16 = ((double14 < 0.0) ? (double10 + 1.0) : double10);
                double17 = ((double15 < 0.0) ? (double11 + 1.0) : double11);
                double18 = double16 + double14;
                double19 = double17 + double15;
                break;
            }
        }
        final float float3 = 1.0f;
        final float float4 = 0.9f;
        final float float5 = 0.5f;
        final VertexConsumer cyo33 = dqt.getBuffer(RenderType.lines());
        if (bxl.getMode() == StructureMode.SAVE || bxl.getShowBoundingBox()) {
            LevelRenderer.renderLineBox(cyl, cyo33, double16, double12, double17, double18, double13, double19, 0.9f, 0.9f, 0.9f, 1.0f, 0.5f, 0.5f, 0.5f);
        }
        if (bxl.getMode() == StructureMode.SAVE && bxl.getShowAir()) {
            this.renderInvisibleBlocks(bxl, cyo33, fk8, true, cyl);
            this.renderInvisibleBlocks(bxl, cyo33, fk8, false, cyl);
        }
    }
    
    private void renderInvisibleBlocks(final StructureBlockEntity bxl, final VertexConsumer cyo, final BlockPos fk, final boolean boolean4, final PoseStack cyl) {
        final BlockGetter bjg7 = bxl.getLevel();
        final BlockPos fk2 = bxl.getBlockPos();
        final BlockPos fk3 = fk2.offset(fk);
        for (final BlockPos fk4 : BlockPos.betweenClosed(fk3, fk3.offset(bxl.getStructureSize()).offset(-1, -1, -1))) {
            final BlockState byj12 = bjg7.getBlockState(fk4);
            final boolean boolean5 = byj12.isAir();
            final boolean boolean6 = byj12.getBlock() == Blocks.STRUCTURE_VOID;
            if (boolean5 || boolean6) {
                final float float15 = boolean5 ? 0.05f : 0.0f;
                final double double16 = fk4.getX() - fk2.getX() + 0.45f - float15;
                final double double17 = fk4.getY() - fk2.getY() + 0.45f - float15;
                final double double18 = fk4.getZ() - fk2.getZ() + 0.45f - float15;
                final double double19 = fk4.getX() - fk2.getX() + 0.55f + float15;
                final double double20 = fk4.getY() - fk2.getY() + 0.55f + float15;
                final double double21 = fk4.getZ() - fk2.getZ() + 0.55f + float15;
                if (boolean4) {
                    LevelRenderer.renderLineBox(cyl, cyo, double16, double17, double18, double19, double20, double21, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f);
                }
                else if (boolean5) {
                    LevelRenderer.renderLineBox(cyl, cyo, double16, double17, double18, double19, double20, double21, 0.5f, 0.5f, 1.0f, 1.0f, 0.5f, 0.5f, 1.0f);
                }
                else {
                    LevelRenderer.renderLineBox(cyl, cyo, double16, double17, double18, double19, double20, double21, 1.0f, 0.25f, 0.25f, 1.0f, 1.0f, 0.25f, 0.25f);
                }
            }
        }
    }
    
    @Override
    public boolean shouldRenderOffScreen(final StructureBlockEntity bxl) {
        return true;
    }
}
