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 dsq) {
        super(dsq);
    }
    
    @Override
    public void render(final SkullBlockEntity bxi, final float float2, final PoseStack cyl, final MultiBufferSource dqt, final int integer5, final int integer6) {
        final float float3 = bxi.getMouthAnimation(float2);
        final BlockState byj9 = bxi.getBlockState();
        final boolean boolean10 = byj9.getBlock() instanceof WallSkullBlock;
        final Direction fp11 = boolean10 ? byj9.<Direction>getValue((Property<Direction>)WallSkullBlock.FACING) : null;
        final float float4 = 22.5f * (boolean10 ? ((2 + fp11.get2DDataValue()) * 4) : byj9.<Integer>getValue((Property<Integer>)SkullBlock.ROTATION));
        renderSkull(fp11, float4, ((AbstractSkullBlock)byj9.getBlock()).getType(), bxi.getOwnerProfile(), float3, cyl, dqt, integer5);
    }
    
    public static void renderSkull(@Nullable final Direction fp, final float float2, final SkullBlock.Type a, @Nullable final GameProfile gameProfile, final float float5, final PoseStack cyl, final MultiBufferSource dqt, final int integer) {
        final SkullModel dmp9 = SkullBlockRenderer.MODEL_BY_TYPE.get(a);
        cyl.pushPose();
        if (fp == null) {
            cyl.translate(0.5, 0.0, 0.5);
        }
        else {
            switch (fp) {
                case NORTH: {
                    cyl.translate(0.5, 0.25, 0.7400000095367432);
                    break;
                }
                case SOUTH: {
                    cyl.translate(0.5, 0.25, 0.25999999046325684);
                    break;
                }
                case WEST: {
                    cyl.translate(0.7400000095367432, 0.25, 0.5);
                    break;
                }
                default: {
                    cyl.translate(0.25999999046325684, 0.25, 0.5);
                    break;
                }
            }
        }
        cyl.scale(-1.0f, -1.0f, 1.0f);
        final VertexConsumer cyo10 = dqt.getBuffer(getRenderType(a, gameProfile));
        dmp9.setupAnim(float5, float2, 0.0f);
        dmp9.renderToBuffer(cyl, cyo10, integer, OverlayTexture.NO_OVERLAY, 1.0f, 1.0f, 1.0f, 1.0f);
        cyl.popPose();
    }
    
    private static RenderType getRenderType(final SkullBlock.Type a, @Nullable final GameProfile gameProfile) {
        final ResourceLocation sm3 = SkullBlockRenderer.SKIN_BY_TYPE.get(a);
        if (a != SkullBlock.Types.PLAYER || gameProfile == null) {
            return RenderType.entityCutoutNoCull(sm3);
        }
        final Minecraft dbn4 = Minecraft.getInstance();
        final Map<MinecraftProfileTexture.Type, MinecraftProfileTexture> map5 = dbn4.getSkinManager().getInsecureSkinInformation(gameProfile);
        if (map5.containsKey(MinecraftProfileTexture.Type.SKIN)) {
            return RenderType.entityTranslucent(dbn4.getSkinManager().registerTexture(map5.get(MinecraftProfileTexture.Type.SKIN), MinecraftProfileTexture.Type.SKIN));
        }
        return RenderType.entityCutoutNoCull(DefaultPlayerSkin.getDefaultSkin(Player.createPlayerUUID(gameProfile)));
    }
    
    static {
        final SkullModel dmp2;
        final HumanoidHeadModel dmp3;
        final DragonHeadModel dng4;
        MODEL_BY_TYPE = Util.<Map<SkullBlock.Type, SkullModel>>make(Maps.newHashMap(), hashMap -> {
            dmp2 = new SkullModel(0, 0, 64, 32);
            dmp3 = new HumanoidHeadModel();
            dng4 = new DragonHeadModel(0.0f);
            hashMap.put(SkullBlock.Types.SKELETON, (DragonHeadModel)dmp2);
            hashMap.put(SkullBlock.Types.WITHER_SKELETON, (DragonHeadModel)dmp2);
            hashMap.put(SkullBlock.Types.PLAYER, (DragonHeadModel)dmp3);
            hashMap.put(SkullBlock.Types.ZOMBIE, (DragonHeadModel)dmp3);
            hashMap.put(SkullBlock.Types.CREEPER, (DragonHeadModel)dmp2);
            hashMap.put(SkullBlock.Types.DRAGON, dng4);
            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());
        });
    }
}
