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 edw) {
        super(edw);
    }
    
    @Override
    public void render(final StructureBlockEntity cel, final float float2, final PoseStack dhl, final MultiBufferSource ebz, final int integer5, final int integer6) {
        if (!Minecraft.getInstance().player.canUseGameMasterBlocks() && !Minecraft.getInstance().player.isSpectator()) {
            return;
        }
        final BlockPos fu8 = cel.getStructurePos();
        final BlockPos fu9 = cel.getStructureSize();
        if (fu9.getX() < 1 || fu9.getY() < 1 || fu9.getZ() < 1) {
            return;
        }
        if (cel.getMode() != StructureMode.SAVE && cel.getMode() != StructureMode.LOAD) {
            return;
        }
        final double double10 = fu8.getX();
        final double double11 = fu8.getZ();
        final double double12 = fu8.getY();
        final double double13 = double12 + fu9.getY();
        double double14 = 0.0;
        double double15 = 0.0;
        switch (cel.getMirror()) {
            case LEFT_RIGHT: {
                double14 = fu9.getX();
                double15 = -fu9.getZ();
                break;
            }
            case FRONT_BACK: {
                double14 = -fu9.getX();
                double15 = fu9.getZ();
                break;
            }
            default: {
                double14 = fu9.getX();
                double15 = fu9.getZ();
                break;
            }
        }
        double double16 = 0.0;
        double double17 = 0.0;
        double double18 = 0.0;
        double double19 = 0.0;
        switch (cel.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 dhp33 = ebz.getBuffer(RenderType.lines());
        if (cel.getMode() == StructureMode.SAVE || cel.getShowBoundingBox()) {
            LevelRenderer.renderLineBox(dhl, dhp33, double16, double12, double17, double18, double13, double19, 0.9f, 0.9f, 0.9f, 1.0f, 0.5f, 0.5f, 0.5f);
        }
        if (cel.getMode() == StructureMode.SAVE && cel.getShowAir()) {
            this.renderInvisibleBlocks(cel, dhp33, fu8, true, dhl);
            this.renderInvisibleBlocks(cel, dhp33, fu8, false, dhl);
        }
    }
    
    private void renderInvisibleBlocks(final StructureBlockEntity cel, final VertexConsumer dhp, final BlockPos fu, final boolean boolean4, final PoseStack dhl) {
        final BlockGetter bpg7 = cel.getLevel();
        final BlockPos fu2 = cel.getBlockPos();
        final BlockPos fu3 = fu2.offset(fu);
        for (final BlockPos fu4 : BlockPos.betweenClosed(fu3, fu3.offset(cel.getStructureSize()).offset(-1, -1, -1))) {
            final BlockState cfj12 = bpg7.getBlockState(fu4);
            final boolean boolean5 = cfj12.isAir();
            final boolean boolean6 = cfj12.is(Blocks.STRUCTURE_VOID);
            if (boolean5 || boolean6) {
                final float float15 = boolean5 ? 0.05f : 0.0f;
                final double double16 = fu4.getX() - fu2.getX() + 0.45f - float15;
                final double double17 = fu4.getY() - fu2.getY() + 0.45f - float15;
                final double double18 = fu4.getZ() - fu2.getZ() + 0.45f - float15;
                final double double19 = fu4.getX() - fu2.getX() + 0.55f + float15;
                final double double20 = fu4.getY() - fu2.getY() + 0.55f + float15;
                final double double21 = fu4.getZ() - fu2.getZ() + 0.55f + float15;
                if (boolean4) {
                    LevelRenderer.renderLineBox(dhl, dhp, 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(dhl, dhp, double16, double17, double18, double19, double20, double21, 0.5f, 0.5f, 1.0f, 1.0f, 0.5f, 0.5f, 1.0f);
                }
                else {
                    LevelRenderer.renderLineBox(dhl, dhp, 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 cel) {
        return true;
    }
}
