package net.minecraft.client.renderer.entity;

import com.mojang.math.Matrix4f;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
import net.minecraft.client.gui.Font;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.client.renderer.MultiBufferSource;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.AABB;
import net.minecraft.client.renderer.culling.Frustum;
import net.minecraft.world.level.LightLayer;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;

public abstract class EntityRenderer<T extends Entity> {
    protected final EntityRenderDispatcher entityRenderDispatcher;
    protected float shadowRadius;
    protected float shadowStrength;
    
    protected EntityRenderer(final EntityRenderDispatcher egm) {
        this.shadowStrength = 1.0f;
        this.entityRenderDispatcher = egm;
    }
    
    public final int getPackedLightCoords(final T aom, final float float2) {
        final BlockPos fu4 = new BlockPos(aom.getEyePosition(float2));
        return LightTexture.pack(this.getBlockLightLevel(aom, fu4), this.getSkyLightLevel(aom, fu4));
    }
    
    protected int getSkyLightLevel(final T aom, final BlockPos fu) {
        return aom.level.getBrightness(LightLayer.SKY, fu);
    }
    
    protected int getBlockLightLevel(final T aom, final BlockPos fu) {
        if (aom.isOnFire()) {
            return 15;
        }
        return aom.level.getBrightness(LightLayer.BLOCK, fu);
    }
    
    public boolean shouldRender(final T aom, final Frustum ees, final double double3, final double double4, final double double5) {
        if (!aom.shouldRender(double3, double4, double5)) {
            return false;
        }
        if (aom.noCulling) {
            return true;
        }
        AABB deg10 = aom.getBoundingBoxForCulling().inflate(0.5);
        if (deg10.hasNaN() || deg10.getSize() == 0.0) {
            deg10 = new AABB(aom.getX() - 2.0, aom.getY() - 2.0, aom.getZ() - 2.0, aom.getX() + 2.0, aom.getY() + 2.0, aom.getZ() + 2.0);
        }
        return ees.isVisible(deg10);
    }
    
    public Vec3 getRenderOffset(final T aom, final float float2) {
        return Vec3.ZERO;
    }
    
    public void render(final T aom, final float float2, final float float3, final PoseStack dhl, final MultiBufferSource ebz, final int integer) {
        if (!this.shouldShowName(aom)) {
            return;
        }
        this.renderNameTag(aom, aom.getDisplayName(), dhl, ebz, integer);
    }
    
    protected boolean shouldShowName(final T aom) {
        return aom.shouldShowName() && aom.hasCustomName();
    }
    
    public abstract ResourceLocation getTextureLocation(final T aom);
    
    public Font getFont() {
        return this.entityRenderDispatcher.getFont();
    }
    
    protected void renderNameTag(final T aom, final Component mr, final PoseStack dhl, final MultiBufferSource ebz, final int integer) {
        final double double7 = this.entityRenderDispatcher.distanceToSqr(aom);
        if (double7 > 4096.0) {
            return;
        }
        final boolean boolean9 = !aom.isDiscrete();
        final float float10 = aom.getBbHeight() + 0.5f;
        final int integer2 = "deadmau5".equals(mr.getString()) ? -10 : 0;
        dhl.pushPose();
        dhl.translate(0.0, float10, 0.0);
        dhl.mulPose(this.entityRenderDispatcher.cameraOrientation());
        dhl.scale(-0.025f, -0.025f, 0.025f);
        final Matrix4f b12 = dhl.last().pose();
        final float float11 = Minecraft.getInstance().options.getBackgroundOpacity(0.25f);
        final int integer3 = (int)(float11 * 255.0f) << 24;
        final Font dmt15 = this.getFont();
        final float float12 = (float)(-dmt15.width(mr) / 2);
        dmt15.drawInBatch(mr, float12, (float)integer2, 553648127, false, b12, ebz, boolean9, integer3, integer);
        if (boolean9) {
            dmt15.drawInBatch(mr, float12, (float)integer2, -1, false, b12, ebz, false, 0, integer);
        }
        dhl.popPose();
    }
    
    public EntityRenderDispatcher getDispatcher() {
        return this.entityRenderDispatcher;
    }
}
