package net.minecraft.client.renderer.blockentity;

import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.model.geom.ModelPart;
import net.minecraft.client.model.Model;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.client.renderer.Sheets;
import net.minecraft.world.level.block.state.properties.WoodType;
import net.minecraft.world.level.block.SignBlock;
import net.minecraft.world.level.block.Block;
import java.util.List;
import net.minecraft.client.gui.Font;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.resources.model.Material;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.network.chat.Component;
import net.minecraft.client.gui.components.ComponentRenderUtils;
import com.mojang.blaze3d.platform.NativeImage;
import net.minecraft.world.level.block.WallSignBlock;
import net.minecraft.core.Direction;
import com.mojang.math.Vector3f;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.StandingSignBlock;
import net.minecraft.client.renderer.MultiBufferSource;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.world.level.block.entity.SignBlockEntity;

public class SignRenderer extends BlockEntityRenderer<SignBlockEntity> {
    private final SignModel signModel;
    
    public SignRenderer(final BlockEntityRenderDispatcher dsq) {
        super(dsq);
        this.signModel = new SignModel();
    }
    
    @Override
    public void render(final SignBlockEntity bxh, final float float2, final PoseStack cyl, final MultiBufferSource dqt, final int integer5, final int integer6) {
        final BlockState byj8 = bxh.getBlockState();
        cyl.pushPose();
        final float float3 = 0.6666667f;
        if (byj8.getBlock() instanceof StandingSignBlock) {
            cyl.translate(0.5, 0.5, 0.5);
            final float float4 = -(byj8.<Integer>getValue((Property<Integer>)StandingSignBlock.ROTATION) * 360 / 16.0f);
            cyl.mulPose(Vector3f.YP.rotationDegrees(float4));
            this.signModel.stick.visible = true;
        }
        else {
            cyl.translate(0.5, 0.5, 0.5);
            final float float4 = -byj8.<Direction>getValue((Property<Direction>)WallSignBlock.FACING).toYRot();
            cyl.mulPose(Vector3f.YP.rotationDegrees(float4));
            cyl.translate(0.0, -0.3125, -0.4375);
            this.signModel.stick.visible = false;
        }
        cyl.pushPose();
        cyl.scale(0.6666667f, -0.6666667f, -0.6666667f);
        final Material eby10 = getMaterial(byj8.getBlock());
        final VertexConsumer cyo11 = eby10.buffer(dqt, this.signModel::renderType);
        this.signModel.sign.render(cyl, cyo11, integer5, integer6);
        this.signModel.stick.render(cyl, cyo11, integer5, integer6);
        cyl.popPose();
        final Font dch12 = this.renderer.getFont();
        final float float5 = 0.010416667f;
        cyl.translate(0.0, 0.3333333432674408, 0.046666666865348816);
        cyl.scale(0.010416667f, -0.010416667f, 0.010416667f);
        final int integer7 = bxh.getColor().getTextColor();
        final double double15 = 0.4;
        final int integer8 = (int)(NativeImage.getR(integer7) * 0.4);
        final int integer9 = (int)(NativeImage.getG(integer7) * 0.4);
        final int integer10 = (int)(NativeImage.getB(integer7) * 0.4);
        final int integer11 = NativeImage.combine(0, integer10, integer9, integer8);
        for (int integer12 = 0; integer12 < 4; ++integer12) {
            final List<Component> list3;
            final String string22 = bxh.getRenderMessage(integer12, lf -> {
                list3 = ComponentRenderUtils.wrapComponents(lf, 90, dch12, false, true);
                return list3.isEmpty() ? "" : list3.get(0).getColoredString();
            });
            if (string22 != null) {
                final float float6 = (float)(-dch12.width(string22) / 2);
                dch12.drawInBatch(string22, float6, (float)(integer12 * 10 - bxh.messages.length * 5), integer11, false, cyl.last().pose(), dqt, false, 0, integer5);
            }
        }
        cyl.popPose();
    }
    
    public static Material getMaterial(final Block bph) {
        WoodType bzs2;
        if (bph instanceof SignBlock) {
            bzs2 = ((SignBlock)bph).type();
        }
        else {
            bzs2 = WoodType.OAK;
        }
        return Sheets.signTexture(bzs2);
    }
    
    public static final class SignModel extends Model {
        public final ModelPart sign;
        public final ModelPart stick;
        
        public SignModel() {
            super(RenderType::entityCutoutNoCull);
            (this.sign = new ModelPart(64, 32, 0, 0)).addBox(-12.0f, -14.0f, -1.0f, 24.0f, 12.0f, 2.0f, 0.0f);
            (this.stick = new ModelPart(64, 32, 0, 14)).addBox(-1.0f, -2.0f, -1.0f, 2.0f, 14.0f, 2.0f, 0.0f);
        }
        
        @Override
        public void renderToBuffer(final PoseStack cyl, final VertexConsumer cyo, final int integer3, final int integer4, final float float5, final float float6, final float float7, final float float8) {
            this.sign.render(cyl, cyo, integer3, integer4, float5, float6, float7, float8);
            this.stick.render(cyl, cyo, integer3, integer4, float5, float6, float7, float8);
        }
    }
}
