package net.minecraft.client.renderer.entity.layers;

import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.client.renderer.block.model.ItemTransforms;
import net.minecraft.client.Minecraft;
import com.mojang.math.Vector3f;
import net.minecraft.world.item.ArmorItem;
import net.minecraft.core.Direction;
import net.minecraft.client.renderer.blockentity.SkullBlockRenderer;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.block.entity.SkullBlockEntity;
import java.util.UUID;
import com.mojang.authlib.GameProfile;
import org.apache.commons.lang3.StringUtils;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.world.level.block.AbstractSkullBlock;
import net.minecraft.world.item.BlockItem;
import net.minecraft.client.model.HeadedModel;
import net.minecraft.world.entity.monster.ZombieVillager;
import net.minecraft.world.entity.npc.Villager;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.client.renderer.MultiBufferSource;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.entity.RenderLayerParent;
import net.minecraft.world.entity.LivingEntity;

public class CustomHeadLayer<T extends LivingEntity, M extends EntityModel> extends RenderLayer<T, M> {
    private final float scaleX;
    private final float scaleY;
    private final float scaleZ;
    
    public CustomHeadLayer(final RenderLayerParent<T, M> eid) {
        this(eid, 1.0f, 1.0f, 1.0f);
    }
    
    public CustomHeadLayer(final RenderLayerParent<T, M> eid, final float float2, final float float3, final float float4) {
        super(eid);
        this.scaleX = float2;
        this.scaleY = float3;
        this.scaleZ = float4;
    }
    
    @Override
    public void render(final PoseStack dhl, final MultiBufferSource ebz, final int integer, final T aoy, final float float5, final float float6, final float float7, final float float8, final float float9, final float float10) {
        final ItemStack bki12 = aoy.getItemBySlot(EquipmentSlot.HEAD);
        if (bki12.isEmpty()) {
            return;
        }
        final Item bke13 = bki12.getItem();
        dhl.pushPose();
        dhl.scale(this.scaleX, this.scaleY, this.scaleZ);
        final boolean boolean14 = aoy instanceof Villager || aoy instanceof ZombieVillager;
        if (aoy.isBaby() && !(aoy instanceof Villager)) {
            final float float11 = 2.0f;
            final float float12 = 1.4f;
            dhl.translate(0.0, 0.03125, 0.0);
            dhl.scale(0.7f, 0.7f, 0.7f);
            dhl.translate(0.0, 1.0, 0.0);
        }
        this.getParentModel().getHead().translateAndRotate(dhl);
        if (bke13 instanceof BlockItem && ((BlockItem)bke13).getBlock() instanceof AbstractSkullBlock) {
            final float float11 = 1.1875f;
            dhl.scale(1.1875f, -1.1875f, -1.1875f);
            if (boolean14) {
                dhl.translate(0.0, 0.0625, 0.0);
            }
            GameProfile gameProfile16 = null;
            if (bki12.hasTag()) {
                final CompoundTag le17 = bki12.getTag();
                if (le17.contains("SkullOwner", 10)) {
                    gameProfile16 = NbtUtils.readGameProfile(le17.getCompound("SkullOwner"));
                }
                else if (le17.contains("SkullOwner", 8)) {
                    final String string18 = le17.getString("SkullOwner");
                    if (!StringUtils.isBlank((CharSequence)string18)) {
                        gameProfile16 = SkullBlockEntity.updateGameprofile(new GameProfile((UUID)null, string18));
                        le17.put("SkullOwner", NbtUtils.writeGameProfile(new CompoundTag(), gameProfile16));
                    }
                }
            }
            dhl.translate(-0.5, 0.0, -0.5);
            SkullBlockRenderer.renderSkull(null, 180.0f, ((AbstractSkullBlock)((BlockItem)bke13).getBlock()).getType(), gameProfile16, float5, dhl, ebz, integer);
        }
        else if (!(bke13 instanceof ArmorItem) || ((ArmorItem)bke13).getSlot() != EquipmentSlot.HEAD) {
            final float float11 = 0.625f;
            dhl.translate(0.0, -0.25, 0.0);
            dhl.mulPose(Vector3f.YP.rotationDegrees(180.0f));
            dhl.scale(0.625f, -0.625f, -0.625f);
            if (boolean14) {
                dhl.translate(0.0, 0.1875, 0.0);
            }
            Minecraft.getInstance().getItemInHandRenderer().renderItem(aoy, bki12, ItemTransforms.TransformType.HEAD, false, dhl, ebz, integer);
        }
        dhl.popPose();
    }
}
