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 dvf) {
        super(dvf);
        this.model = new MinecartModel<T>();
        this.shadowRadius = 0.7f;
    }
    
    @Override
    public void render(final T azx, float float2, final float float3, final PoseStack cyl, final MultiBufferSource dqt, final int integer) {
        super.render(azx, float2, float3, cyl, dqt, integer);
        cyl.pushPose();
        long long8 = azx.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;
        cyl.translate(float4, float5, float6);
        final double double13 = Mth.lerp(float3, azx.xOld, azx.getX());
        final double double14 = Mth.lerp(float3, azx.yOld, azx.getY());
        final double double15 = Mth.lerp(float3, azx.zOld, azx.getZ());
        final double double16 = 0.30000001192092896;
        final Vec3 cvl21 = azx.getPos(double13, double14, double15);
        float float7 = Mth.lerp(float3, azx.xRotO, azx.xRot);
        if (cvl21 != null) {
            Vec3 cvl22 = azx.getPosOffs(double13, double14, double15, 0.30000001192092896);
            Vec3 cvl23 = azx.getPosOffs(double13, double14, double15, -0.30000001192092896);
            if (cvl22 == null) {
                cvl22 = cvl21;
            }
            if (cvl23 == null) {
                cvl23 = cvl21;
            }
            cyl.translate(cvl21.x - double13, (cvl22.y + cvl23.y) / 2.0 - double14, cvl21.z - double15);
            Vec3 cvl24 = cvl23.add(-cvl22.x, -cvl22.y, -cvl22.z);
            if (cvl24.length() != 0.0) {
                cvl24 = cvl24.normalize();
                float2 = (float)(Math.atan2(cvl24.z, cvl24.x) * 180.0 / 3.141592653589793);
                float7 = (float)(Math.atan(cvl24.y) * 73.0);
            }
        }
        cyl.translate(0.0, 0.375, 0.0);
        cyl.mulPose(Vector3f.YP.rotationDegrees(180.0f - float2));
        cyl.mulPose(Vector3f.ZP.rotationDegrees(-float7));
        final float float8 = azx.getHurtTime() - float3;
        float float9 = azx.getDamage() - float3;
        if (float9 < 0.0f) {
            float9 = 0.0f;
        }
        if (float8 > 0.0f) {
            cyl.mulPose(Vector3f.XP.rotationDegrees(Mth.sin(float8) * float8 * float9 / 10.0f * azx.getHurtDir()));
        }
        final int integer2 = azx.getDisplayOffset();
        final BlockState byj26 = azx.getDisplayBlockState();
        if (byj26.getRenderShape() != RenderShape.INVISIBLE) {
            cyl.pushPose();
            final float float10 = 0.75f;
            cyl.scale(0.75f, 0.75f, 0.75f);
            cyl.translate(-0.5, (integer2 - 8) / 16.0f, 0.5);
            cyl.mulPose(Vector3f.YP.rotationDegrees(90.0f));
            this.renderMinecartContents(azx, float3, byj26, cyl, dqt, integer);
            cyl.popPose();
        }
        cyl.scale(-1.0f, -1.0f, 1.0f);
        this.model.setupAnim(azx, 0.0f, 0.0f, -0.1f, 0.0f, 0.0f);
        final VertexConsumer cyo27 = dqt.getBuffer(this.model.renderType(this.getTextureLocation(azx)));
        this.model.renderToBuffer(cyl, cyo27, integer, OverlayTexture.NO_OVERLAY, 1.0f, 1.0f, 1.0f, 1.0f);
        cyl.popPose();
    }
    
    @Override
    public ResourceLocation getTextureLocation(final T azx) {
        return MinecartRenderer.MINECART_LOCATION;
    }
    
    protected void renderMinecartContents(final T azx, final float float2, final BlockState byj, final PoseStack cyl, final MultiBufferSource dqt, final int integer) {
        Minecraft.getInstance().getBlockRenderer().renderSingleBlock(byj, cyl, dqt, integer, OverlayTexture.NO_OVERLAY);
    }
    
    static {
        MINECART_LOCATION = new ResourceLocation("textures/entity/minecart.png");
    }
}
