package com.dairymoose.modernlife.renderer.tileentity;

import com.dairymoose.modernlife.blocks.CanvasBlock;
import com.dairymoose.modernlife.core.CustomBlocks;
import com.dairymoose.modernlife.network.play.client.ServerboundPrintSizePacket;
import com.dairymoose.modernlife.tileentities.CanvasBlockEntity;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Matrix4f;
import com.mojang.math.Vector3f;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.model.data.EmptyModelData;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@OnlyIn(Dist.CLIENT)
/* loaded from: outputsrg.jar:com/dairymoose/modernlife/renderer/tileentity/CanvasBlockEntityRenderer.class */
public class CanvasBlockEntityRenderer implements BlockEntityRenderer<CanvasBlockEntity> {
    private static final Logger LOGGER = LogManager.getLogger();

    public CanvasBlockEntityRenderer(BlockEntityRendererProvider.Context ctx) {
    }

    public boolean shouldRenderOffScreen(CanvasBlockEntity p_112306_) {
        return false;
    }

    public boolean isSane(float blockWidth, float blockHeight, float xOffset, float yOffset) {
        float after = ServerboundPrintSizePacket.sanitizeWidthHeight(blockWidth);
        if (blockWidth != after) {
            return false;
        }
        float after2 = ServerboundPrintSizePacket.sanitizeWidthHeight(blockHeight);
        if (blockHeight != after2) {
            return false;
        }
        float after3 = ServerboundPrintSizePacket.sanitizeOffset(xOffset);
        if (xOffset != after3) {
            return false;
        }
        float after4 = ServerboundPrintSizePacket.sanitizeOffset(yOffset);
        if (yOffset != after4) {
            return false;
        }
        return true;
    }

    public void render(CanvasBlockEntity tileEntity, float v, PoseStack matrixStack, MultiBufferSource iRenderTypeBuffer, int lightmap, int i1) {
        int canvasSize = ((Integer) tileEntity.getBlockState().getValue(CanvasBlock.SIZE)).intValue();
        Direction facing = tileEntity.getBlockState().getValue(CanvasBlock.FACING);
        boolean saneCustomCanvas = false;
        if (canvasSize == 0) {
            saneCustomCanvas = isSane(tileEntity.blockWidth, tileEntity.blockHeight, tileEntity.xOffset, tileEntity.yOffset);
            if (saneCustomCanvas) {
                matrixStack.pushPose();
                if (facing == Direction.EAST) {
                    matrixStack.translate(0.5d, 0.5d, 0.5d);
                    matrixStack.mulPose(Vector3f.YP.rotationDegrees(270.0f));
                    matrixStack.translate(-0.5d, -0.5d, -0.5d);
                } else if (facing == Direction.SOUTH) {
                    matrixStack.translate(0.5d, 0.5d, 0.5d);
                    matrixStack.mulPose(Vector3f.YP.rotationDegrees(180.0f));
                    matrixStack.translate(-0.5d, -0.5d, -0.5d);
                } else if (facing == Direction.WEST) {
                    matrixStack.translate(0.5d, 0.5d, 0.5d);
                    matrixStack.mulPose(Vector3f.YP.rotationDegrees(90.0f));
                    matrixStack.translate(-0.5d, -0.5d, -0.5d);
                }
                matrixStack.translate(-tileEntity.xOffset, tileEntity.yOffset, 0.0d);
                float xDiff = tileEntity.blockWidth - 1.0f;
                float yDiff = tileEntity.blockHeight - 1.0f;
                matrixStack.translate((-xDiff) / 2.0f, (-yDiff) / 2.0f, 0.0d);
                matrixStack.scale(tileEntity.blockWidth, tileEntity.blockHeight, 1.0f);
            }
            Minecraft.getInstance().getBlockRenderer().renderSingleBlock((BlockState) ((BlockState) CustomBlocks.BLOCK_FACELESS_CANVAS.get().defaultBlockState().setValue(CanvasBlock.SIZE, 1)).setValue(CanvasBlock.FACING, Direction.NORTH), matrixStack, iRenderTypeBuffer, lightmap, i1, EmptyModelData.INSTANCE);
            if (saneCustomCanvas) {
                matrixStack.popPose();
            }
        }
        switch (C01421.$SwitchMap$net$minecraft$core$Direction[facing.ordinal()]) {
            case 1:
                if (Minecraft.getInstance().player.blockPosition().getX() < tileEntity.getBlockPos().getX()) {
                    return;
                }
                break;
            case 2:
                if (Minecraft.getInstance().player.blockPosition().getX() > tileEntity.getBlockPos().getX()) {
                    return;
                }
                break;
            case 3:
                if (Minecraft.getInstance().player.blockPosition().getZ() > tileEntity.getBlockPos().getZ()) {
                    return;
                }
                break;
            case 4:
                if (Minecraft.getInstance().player.blockPosition().getZ() < tileEntity.getBlockPos().getZ()) {
                    return;
                }
                break;
        }
        tileEntity.initImage();
        matrixStack.pushPose();
        switch (C01421.$SwitchMap$net$minecraft$core$Direction[tileEntity.getBlockState().getValue(CanvasBlock.FACING).ordinal()]) {
            case 1:
                matrixStack.mulPose(Vector3f.YP.rotationDegrees(270.0f));
                break;
            case 2:
                matrixStack.mulPose(Vector3f.YP.rotationDegrees(90.0f));
                matrixStack.translate(-1.0d, 0.0d, 1.0d);
                break;
            case 3:
                matrixStack.mulPose(Vector3f.YP.rotationDegrees(0.0f));
                matrixStack.translate(0.0d, 0.0d, 1.0d);
                break;
            case 4:
                matrixStack.mulPose(Vector3f.YP.rotationDegrees(180.0f));
                matrixStack.translate(-1.0d, 0.0d, 0.0d);
                break;
        }
        matrixStack.translate(0.0d, 0.0d, -0.0949999988079071d);
        RenderType renderType = tileEntity.getDynamicRenderType();
        if (renderType != null) {
            VertexConsumer dynamicBuilder = iRenderTypeBuffer.getBuffer(tileEntity.getDynamicRenderType());
            float mod = 0.0f;
            float topMod = 0.0f;
            float modWidth = 0.0f;
            float modHeight = 0.0f;
            if (canvasSize == 0) {
                if (saneCustomCanvas) {
                    float sizeW = (tileEntity.blockWidth - 1.0f) / 2.0f;
                    float sizeH = (tileEntity.blockHeight - 1.0f) / 2.0f;
                    matrixStack.translate(-tileEntity.xOffset, tileEntity.yOffset, 0.0d);
                    matrixStack.translate(0.0d, -sizeH, 0.0d);
                    modWidth = sizeW;
                    modHeight = sizeH;
                    topMod = sizeH;
                } else {
                    canvasSize = 1;
                }
            }
            if (canvasSize == 4) {
                mod = 1.0f;
            } else if (canvasSize == 3) {
                mod = 0.5f;
            } else if (canvasSize == 2) {
                mod = 0.25f;
            } else if (canvasSize == 5) {
                matrixStack.translate(0.0d, -0.3125d, 0.0d);
                mod = 0.3125f;
                topMod = 0.3125f;
            } else if (canvasSize == 6) {
                matrixStack.translate(0.0d, -0.625d, 0.0d);
                mod = 0.625f;
                topMod = 0.625f;
            } else if (canvasSize == 7) {
                matrixStack.translate(0.0d, -1.0d, 0.0d);
                mod = 1.0f;
                topMod = 1.0f;
            }
            if (canvasSize != 0) {
                modWidth = mod;
                modHeight = mod;
            }
            Matrix4f matrix4f = matrixStack.last().pose();
            dynamicBuilder.vertex(matrix4f, 0.0f - modWidth, 1.0f + modHeight + topMod, 0.0f).color(255, 255, 255, 255).uv(0.0f, 1.0f).uv2(lightmap).endVertex();
            dynamicBuilder.vertex(matrix4f, 1.0f + modWidth, 1.0f + modHeight + topMod, 0.0f).color(255, 255, 255, 255).uv(1.0f, 1.0f).uv2(lightmap).endVertex();
            dynamicBuilder.vertex(matrix4f, 1.0f + modWidth, 0.0f, 0.0f).color(255, 255, 255, 255).uv(1.0f, 0.0f).uv2(lightmap).endVertex();
            dynamicBuilder.vertex(matrix4f, 0.0f - modWidth, 0.0f, 0.0f).color(255, 255, 255, 255).uv(0.0f, 0.0f).uv2(lightmap).endVertex();
        }
        matrixStack.popPose();
    }

    /* renamed from: com.dairymoose.modernlife.renderer.tileentity.CanvasBlockEntityRenderer$1 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/renderer/tileentity/CanvasBlockEntityRenderer$1.class */
    public static /* synthetic */ class C01421 {
        static final /* synthetic */ int[] $SwitchMap$net$minecraft$core$Direction = new int[Direction.values().length];

        static {
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.EAST.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.WEST.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.NORTH.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.SOUTH.ordinal()] = 4;
            } catch (NoSuchFieldError e4) {
            }
        }
    }
}
