package net.minecraft.client.particle;

import net.minecraft.client.renderer.MultiBufferSource;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.util.Mth;
import net.minecraft.client.Camera;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.phys.Vec3;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.entity.EntityRenderDispatcher;
import net.minecraft.world.entity.Entity;
import net.minecraft.client.renderer.RenderBuffers;

public class ItemPickupParticle extends Particle {
    private final RenderBuffers renderBuffers;
    private final Entity itemEntity;
    private final Entity target;
    private int life;
    private final EntityRenderDispatcher entityRenderDispatcher;
    
    public ItemPickupParticle(final EntityRenderDispatcher egm, final RenderBuffers ecf, final ClientLevel dym, final Entity aom4, final Entity aom5) {
        this(egm, ecf, dym, aom4, aom5, aom4.getDeltaMovement());
    }
    
    private ItemPickupParticle(final EntityRenderDispatcher egm, final RenderBuffers ecf, final ClientLevel dym, final Entity aom4, final Entity aom5, final Vec3 dem) {
        super(dym, aom4.getX(), aom4.getY(), aom4.getZ(), dem.x, dem.y, dem.z);
        this.renderBuffers = ecf;
        this.itemEntity = this.getSafeCopy(aom4);
        this.target = aom5;
        this.entityRenderDispatcher = egm;
    }
    
    private Entity getSafeCopy(final Entity aom) {
        if (!(aom instanceof ItemEntity)) {
            return aom;
        }
        return ((ItemEntity)aom).copy();
    }
    
    @Override
    public ParticleRenderType getRenderType() {
        return ParticleRenderType.CUSTOM;
    }
    
    @Override
    public void render(final VertexConsumer dhp, final Camera dlj, final float float3) {
        float float4 = (this.life + float3) / 3.0f;
        float4 *= float4;
        final double double6 = Mth.lerp(float3, this.target.xOld, this.target.getX());
        final double double7 = Mth.lerp(float3, this.target.yOld, this.target.getY()) + 0.5;
        final double double8 = Mth.lerp(float3, this.target.zOld, this.target.getZ());
        final double double9 = Mth.lerp(float4, this.itemEntity.getX(), double6);
        final double double10 = Mth.lerp(float4, this.itemEntity.getY(), double7);
        final double double11 = Mth.lerp(float4, this.itemEntity.getZ(), double8);
        final MultiBufferSource.BufferSource a18 = this.renderBuffers.bufferSource();
        final Vec3 dem19 = dlj.getPosition();
        this.entityRenderDispatcher.<Entity>render(this.itemEntity, double9 - dem19.x(), double10 - dem19.y(), double11 - dem19.z(), this.itemEntity.yRot, float3, new PoseStack(), a18, this.entityRenderDispatcher.<Entity>getPackedLightCoords(this.itemEntity, float3));
        a18.endBatch();
    }
    
    @Override
    public void tick() {
        ++this.life;
        if (this.life == 3) {
            this.remove();
        }
    }
}
