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