package net.minecraft.client.renderer.blockentity;

import net.minecraft.Util;
import com.google.common.collect.Maps;
import net.minecraft.client.model.dragon.DragonHeadModel;
import net.minecraft.client.model.HumanoidHeadModel;
import java.util.HashMap;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.client.resources.DefaultPlayerSkin;
import net.minecraft.world.entity.player.Player;
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.RenderType;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.renderer.texture.OverlayTexture;
import com.mojang.authlib.GameProfile;
import javax.annotation.Nullable;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.AbstractSkullBlock;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.WallSkullBlock;
import net.minecraft.client.renderer.MultiBufferSource;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.client.model.SkullModel;
import net.minecraft.world.level.block.SkullBlock;
import java.util.Map;
import net.minecraft.world.level.block.entity.SkullBlockEntity;

public class SkullBlockRenderer extends BlockEntityRenderer<SkullBlockEntity> {
    private static final Map<SkullBlock.Type, SkullModel> MODEL_BY_TYPE;
    private static final Map<SkullBlock.Type, ResourceLocation> SKIN_BY_TYPE;
    
    public SkullBlockRenderer(final BlockEntityRenderDispatcher edw) {
        super(edw);
    }
    
    @Override
    public void render(final SkullBlockEntity cei, final float float2, final PoseStack dhl, final MultiBufferSource ebz, final int integer5, final int integer6) {
        final float float3 = cei.getMouthAnimation(float2);
        final BlockState cfj9 = cei.getBlockState();
        final boolean boolean10 = cfj9.getBlock() instanceof WallSkullBlock;
        final Direction fz11 = boolean10 ? cfj9.<Direction>getValue((Property<Direction>)WallSkullBlock.FACING) : null;
        final float float4 = 22.5f * (boolean10 ? ((2 + fz11.get2DDataValue()) * 4) : cfj9.<Integer>getValue((Property<Integer>)SkullBlock.ROTATION));
        renderSkull(fz11, float4, ((AbstractSkullBlock)cfj9.getBlock()).getType(), cei.getOwnerProfile(), float3, dhl, ebz, integer5);
    }
    
    public static void renderSkull(@Nullable final Direction fz, final float float2, final SkullBlock.Type a, @Nullable final GameProfile gameProfile, final float float5, final PoseStack dhl, final MultiBufferSource ebz, final int integer) {
        final SkullModel dxm9 = SkullBlockRenderer.MODEL_BY_TYPE.get(a);
        dhl.pushPose();
        if (fz == null) {
            dhl.translate(0.5, 0.0, 0.5);
        }
        else {
            final float float6 = 0.25f;
            dhl.translate(0.5f - fz.getStepX() * 0.25f, 0.25, 0.5f - fz.getStepZ() * 0.25f);
        }
        dhl.scale(-1.0f, -1.0f, 1.0f);
        final VertexConsumer dhp10 = ebz.getBuffer(getRenderType(a, gameProfile));
        dxm9.setupAnim(float5, float2, 0.0f);
        dxm9.renderToBuffer(dhl, dhp10, integer, OverlayTexture.NO_OVERLAY, 1.0f, 1.0f, 1.0f, 1.0f);
        dhl.popPose();
    }
    
    private static RenderType getRenderType(final SkullBlock.Type a, @Nullable final GameProfile gameProfile) {
        final ResourceLocation uh3 = SkullBlockRenderer.SKIN_BY_TYPE.get(a);
        if (a != SkullBlock.Types.PLAYER || gameProfile == null) {
            return RenderType.entityCutoutNoCullZOffset(uh3);
        }
        final Minecraft dlx4 = Minecraft.getInstance();
        final Map<MinecraftProfileTexture.Type, MinecraftProfileTexture> map5 = dlx4.getSkinManager().getInsecureSkinInformation(gameProfile);
        if (map5.containsKey(MinecraftProfileTexture.Type.SKIN)) {
            return RenderType.entityTranslucent(dlx4.getSkinManager().registerTexture(map5.get(MinecraftProfileTexture.Type.SKIN), MinecraftProfileTexture.Type.SKIN));
        }
        return RenderType.entityCutoutNoCull(DefaultPlayerSkin.getDefaultSkin(Player.createPlayerUUID(gameProfile)));
    }
    
    static {
        final SkullModel dxm2;
        final HumanoidHeadModel dxm3;
        final DragonHeadModel dye4;
        MODEL_BY_TYPE = Util.<Map<SkullBlock.Type, SkullModel>>make(Maps.newHashMap(), hashMap -> {
            dxm2 = new SkullModel(0, 0, 64, 32);
            dxm3 = new HumanoidHeadModel();
            dye4 = new DragonHeadModel(0.0f);
            hashMap.put(SkullBlock.Types.SKELETON, (DragonHeadModel)dxm2);
            hashMap.put(SkullBlock.Types.WITHER_SKELETON, (DragonHeadModel)dxm2);
            hashMap.put(SkullBlock.Types.PLAYER, (DragonHeadModel)dxm3);
            hashMap.put(SkullBlock.Types.ZOMBIE, (DragonHeadModel)dxm3);
            hashMap.put(SkullBlock.Types.CREEPER, (DragonHeadModel)dxm2);
            hashMap.put(SkullBlock.Types.DRAGON, dye4);
            return;
        });
        SKIN_BY_TYPE = Util.<Map<SkullBlock.Type, ResourceLocation>>make(Maps.newHashMap(), hashMap -> {
            hashMap.put(SkullBlock.Types.SKELETON, new ResourceLocation("textures/entity/skeleton/skeleton.png"));
            hashMap.put(SkullBlock.Types.WITHER_SKELETON, new ResourceLocation("textures/entity/skeleton/wither_skeleton.png"));
            hashMap.put(SkullBlock.Types.ZOMBIE, new ResourceLocation("textures/entity/zombie/zombie.png"));
            hashMap.put(SkullBlock.Types.CREEPER, new ResourceLocation("textures/entity/creeper/creeper.png"));
            hashMap.put(SkullBlock.Types.DRAGON, new ResourceLocation("textures/entity/enderdragon/dragon.png"));
            hashMap.put(SkullBlock.Types.PLAYER, DefaultPlayerSkin.getDefaultSkin());
        });
    }
}
