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.FormattedText;
import net.minecraft.network.chat.Style;
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 edw) {
        super(edw);
        this.signModel = new SignModel();
    }
    
    @Override
    public void render(final SignBlockEntity ceh, final float float2, final PoseStack dhl, final MultiBufferSource ebz, final int integer5, final int integer6) {
        final BlockState cfj8 = ceh.getBlockState();
        dhl.pushPose();
        final float float3 = 0.6666667f;
        if (cfj8.getBlock() instanceof StandingSignBlock) {
            dhl.translate(0.5, 0.5, 0.5);
            final float float4 = -(cfj8.<Integer>getValue((Property<Integer>)StandingSignBlock.ROTATION) * 360 / 16.0f);
            dhl.mulPose(Vector3f.YP.rotationDegrees(float4));
            this.signModel.stick.visible = true;
        }
        else {
            dhl.translate(0.5, 0.5, 0.5);
            final float float4 = -cfj8.<Direction>getValue((Property<Direction>)WallSignBlock.FACING).toYRot();
            dhl.mulPose(Vector3f.YP.rotationDegrees(float4));
            dhl.translate(0.0, -0.3125, -0.4375);
            this.signModel.stick.visible = false;
        }
        dhl.pushPose();
        dhl.scale(0.6666667f, -0.6666667f, -0.6666667f);
        final Material enk10 = getMaterial(cfj8.getBlock());
        final VertexConsumer dhp11 = enk10.buffer(ebz, this.signModel::renderType);
        this.signModel.sign.render(dhl, dhp11, integer5, integer6);
        this.signModel.stick.render(dhl, dhp11, integer5, integer6);
        dhl.popPose();
        final Font dmt12 = this.renderer.getFont();
        final float float5 = 0.010416667f;
        dhl.translate(0.0, 0.3333333432674408, 0.046666666865348816);
        dhl.scale(0.010416667f, -0.010416667f, 0.010416667f);
        final int integer7 = ceh.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);
        final int integer12 = 20;
        for (int integer13 = 0; integer13 < 4; ++integer13) {
            final List<FormattedText> list3;
            final FormattedText mu2 = ceh.getRenderMessage(integer13, mu -> {
                list3 = dmt12.getSplitter().splitLines(mu, 90, Style.EMPTY);
                return list3.isEmpty() ? FormattedText.EMPTY : list3.get(0);
            });
            if (mu2 != null) {
                final float float6 = (float)(-dmt12.width(mu2) / 2);
                dmt12.drawInBatch(mu2, float6, (float)(integer13 * 10 - 20), integer11, false, dhl.last().pose(), ebz, false, 0, integer5);
            }
        }
        dhl.popPose();
    }
    
    public static Material getMaterial(final Block bvr) {
        WoodType cgs2;
        if (bvr instanceof SignBlock) {
            cgs2 = ((SignBlock)bvr).type();
        }
        else {
            cgs2 = WoodType.OAK;
        }
        return Sheets.signTexture(cgs2);
    }
    
    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 dhl, final VertexConsumer dhp, final int integer3, final int integer4, final float float5, final float float6, final float float7, final float float8) {
            this.sign.render(dhl, dhp, integer3, integer4, float5, float6, float7, float8);
            this.stick.render(dhl, dhp, integer3, integer4, float5, float6, float7, float8);
        }
    }
}
