package net.minecraft.client.renderer.entity;

import net.minecraft.world.entity.Entity;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.renderer.texture.OverlayTexture;
import com.mojang.math.Vector3f;
import net.minecraft.client.renderer.block.model.ItemTransforms;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.Item;
import net.minecraft.client.renderer.MultiBufferSource;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.world.item.ItemStack;
import java.util.Random;
import net.minecraft.world.entity.item.ItemEntity;

public class ItemEntityRenderer extends EntityRenderer<ItemEntity> {
    private final ItemRenderer itemRenderer;
    private final Random random;
    
    public ItemEntityRenderer(final EntityRenderDispatcher egm, final ItemRenderer ehh) {
        super(egm);
        this.random = new Random();
        this.itemRenderer = ehh;
        this.shadowRadius = 0.15f;
        this.shadowStrength = 0.75f;
    }
    
    private int getRenderAmount(final ItemStack bki) {
        int integer3 = 1;
        if (bki.getCount() > 48) {
            integer3 = 5;
        }
        else if (bki.getCount() > 32) {
            integer3 = 4;
        }
        else if (bki.getCount() > 16) {
            integer3 = 3;
        }
        else if (bki.getCount() > 1) {
            integer3 = 2;
        }
        return integer3;
    }
    
    @Override
    public void render(final ItemEntity bbg, final float float2, final float float3, final PoseStack dhl, final MultiBufferSource ebz, final int integer) {
        dhl.pushPose();
        final ItemStack bki8 = bbg.getItem();
        final int integer2 = bki8.isEmpty() ? 187 : (Item.getId(bki8.getItem()) + bki8.getDamageValue());
        this.random.setSeed(integer2);
        final BakedModel enh10 = this.itemRenderer.getModel(bki8, bbg.level, null);
        final boolean boolean11 = enh10.isGui3d();
        final int integer3 = this.getRenderAmount(bki8);
        final float float4 = 0.25f;
        final float float5 = Mth.sin((bbg.getAge() + float3) / 10.0f + bbg.bobOffs) * 0.1f + 0.1f;
        final float float6 = enh10.getTransforms().getTransform(ItemTransforms.TransformType.GROUND).scale.y();
        dhl.translate(0.0, float5 + 0.25f * float6, 0.0);
        final float float7 = bbg.getSpin(float3);
        dhl.mulPose(Vector3f.YP.rotation(float7));
        final float float8 = enh10.getTransforms().ground.scale.x();
        final float float9 = enh10.getTransforms().ground.scale.y();
        final float float10 = enh10.getTransforms().ground.scale.z();
        if (!boolean11) {
            final float float11 = -0.0f * (integer3 - 1) * 0.5f * float8;
            final float float12 = -0.0f * (integer3 - 1) * 0.5f * float9;
            final float float13 = -0.09375f * (integer3 - 1) * 0.5f * float10;
            dhl.translate(float11, float12, float13);
        }
        for (int integer4 = 0; integer4 < integer3; ++integer4) {
            dhl.pushPose();
            if (integer4 > 0) {
                if (boolean11) {
                    final float float12 = (this.random.nextFloat() * 2.0f - 1.0f) * 0.15f;
                    final float float13 = (this.random.nextFloat() * 2.0f - 1.0f) * 0.15f;
                    final float float14 = (this.random.nextFloat() * 2.0f - 1.0f) * 0.15f;
                    dhl.translate(float12, float13, float14);
                }
                else {
                    final float float12 = (this.random.nextFloat() * 2.0f - 1.0f) * 0.15f * 0.5f;
                    final float float13 = (this.random.nextFloat() * 2.0f - 1.0f) * 0.15f * 0.5f;
                    dhl.translate(float12, float13, 0.0);
                }
            }
            this.itemRenderer.render(bki8, ItemTransforms.TransformType.GROUND, false, dhl, ebz, integer, OverlayTexture.NO_OVERLAY, enh10);
            dhl.popPose();
            if (!boolean11) {
                dhl.translate(0.0f * float8, 0.0f * float9, 0.09375f * float10);
            }
        }
        dhl.popPose();
        super.render(bbg, float2, float3, dhl, ebz, integer);
    }
    
    @Override
    public ResourceLocation getTextureLocation(final ItemEntity bbg) {
        return TextureAtlas.LOCATION_BLOCKS;
    }
}
