package net.minecraft.client.renderer.entity;

import net.minecraft.world.entity.Entity;
import net.minecraft.client.Minecraft;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec3;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.world.level.block.RenderShape;
import com.mojang.math.Vector3f;
import net.minecraft.util.Mth;
import net.minecraft.client.renderer.MultiBufferSource;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.model.MinecartModel;
import net.minecraft.client.model.EntityModel;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.vehicle.AbstractMinecart;

public class MinecartRenderer<T extends AbstractMinecart> extends EntityRenderer<T> {
    private static final ResourceLocation MINECART_LOCATION;
    protected final EntityModel<T> model;
    
    public MinecartRenderer(final EntityRenderDispatcher egm) {
        super(egm);
        this.model = new MinecartModel<T>();
        this.shadowRadius = 0.7f;
    }
    
    @Override
    public void render(final T bfr, float float2, final float float3, final PoseStack dhl, final MultiBufferSource ebz, final int integer) {
        super.render(bfr, float2, float3, dhl, ebz, integer);
        dhl.pushPose();
        long long8 = bfr.getId() * 493286711L;
        long8 = long8 * long8 * 4392167121L + long8 * 98761L;
        final float float4 = (((long8 >> 16 & 0x7L) + 0.5f) / 8.0f - 0.5f) * 0.004f;
        final float float5 = (((long8 >> 20 & 0x7L) + 0.5f) / 8.0f - 0.5f) * 0.004f;
        final float float6 = (((long8 >> 24 & 0x7L) + 0.5f) / 8.0f - 0.5f) * 0.004f;
        dhl.translate(float4, float5, float6);
        final double double13 = Mth.lerp(float3, bfr.xOld, bfr.getX());
        final double double14 = Mth.lerp(float3, bfr.yOld, bfr.getY());
        final double double15 = Mth.lerp(float3, bfr.zOld, bfr.getZ());
        final double double16 = 0.30000001192092896;
        final Vec3 dem21 = bfr.getPos(double13, double14, double15);
        float float7 = Mth.lerp(float3, bfr.xRotO, bfr.xRot);
        if (dem21 != null) {
            Vec3 dem22 = bfr.getPosOffs(double13, double14, double15, 0.30000001192092896);
            Vec3 dem23 = bfr.getPosOffs(double13, double14, double15, -0.30000001192092896);
            if (dem22 == null) {
                dem22 = dem21;
            }
            if (dem23 == null) {
                dem23 = dem21;
            }
            dhl.translate(dem21.x - double13, (dem22.y + dem23.y) / 2.0 - double14, dem21.z - double15);
            Vec3 dem24 = dem23.add(-dem22.x, -dem22.y, -dem22.z);
            if (dem24.length() != 0.0) {
                dem24 = dem24.normalize();
                float2 = (float)(Math.atan2(dem24.z, dem24.x) * 180.0 / 3.141592653589793);
                float7 = (float)(Math.atan(dem24.y) * 73.0);
            }
        }
        dhl.translate(0.0, 0.375, 0.0);
        dhl.mulPose(Vector3f.YP.rotationDegrees(180.0f - float2));
        dhl.mulPose(Vector3f.ZP.rotationDegrees(-float7));
        final float float8 = bfr.getHurtTime() - float3;
        float float9 = bfr.getDamage() - float3;
        if (float9 < 0.0f) {
            float9 = 0.0f;
        }
        if (float8 > 0.0f) {
            dhl.mulPose(Vector3f.XP.rotationDegrees(Mth.sin(float8) * float8 * float9 / 10.0f * bfr.getHurtDir()));
        }
        final int integer2 = bfr.getDisplayOffset();
        final BlockState cfj26 = bfr.getDisplayBlockState();
        if (cfj26.getRenderShape() != RenderShape.INVISIBLE) {
            dhl.pushPose();
            final float float10 = 0.75f;
            dhl.scale(0.75f, 0.75f, 0.75f);
            dhl.translate(-0.5, (integer2 - 8) / 16.0f, 0.5);
            dhl.mulPose(Vector3f.YP.rotationDegrees(90.0f));
            this.renderMinecartContents(bfr, float3, cfj26, dhl, ebz, integer);
            dhl.popPose();
        }
        dhl.scale(-1.0f, -1.0f, 1.0f);
        this.model.setupAnim(bfr, 0.0f, 0.0f, -0.1f, 0.0f, 0.0f);
        final VertexConsumer dhp27 = ebz.getBuffer(this.model.renderType(this.getTextureLocation(bfr)));
        this.model.renderToBuffer(dhl, dhp27, integer, OverlayTexture.NO_OVERLAY, 1.0f, 1.0f, 1.0f, 1.0f);
        dhl.popPose();
    }
    
    @Override
    public ResourceLocation getTextureLocation(final T bfr) {
        return MinecartRenderer.MINECART_LOCATION;
    }
    
    protected void renderMinecartContents(final T bfr, final float float2, final BlockState cfj, final PoseStack dhl, final MultiBufferSource ebz, final int integer) {
        Minecraft.getInstance().getBlockRenderer().renderSingleBlock(cfj, dhl, ebz, integer, OverlayTexture.NO_OVERLAY);
    }
    
    static {
        MINECART_LOCATION = new ResourceLocation("textures/entity/minecart.png");
    }
}
