package net.minecraft.client.renderer;

import java.util.function.Function;
import java.util.Comparator;
import java.util.Arrays;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.level.block.entity.BannerPattern;
import com.mojang.datafixers.util.Pair;
import java.util.List;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.resources.model.Material;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderDispatcher;
import net.minecraft.world.level.block.ShulkerBoxBlock;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.BedBlock;
import net.minecraft.world.level.block.AbstractBannerBlock;
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.client.renderer.blockentity.BannerRenderer;
import net.minecraft.world.item.ShieldItem;
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.client.resources.model.ModelBakery;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.BlockItem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.block.model.ItemTransforms;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.TrappedChestBlockEntity;
import net.minecraft.client.model.TridentModel;
import net.minecraft.client.model.ShieldModel;
import net.minecraft.world.level.block.entity.ConduitBlockEntity;
import net.minecraft.world.level.block.entity.BedBlockEntity;
import net.minecraft.world.level.block.entity.BannerBlockEntity;
import net.minecraft.world.level.block.entity.EnderChestBlockEntity;
import net.minecraft.world.level.block.entity.ChestBlockEntity;
import net.minecraft.world.level.block.entity.ShulkerBoxBlockEntity;

public class BlockEntityWithoutLevelRenderer {
    private static final ShulkerBoxBlockEntity[] SHULKER_BOXES;
    private static final ShulkerBoxBlockEntity DEFAULT_SHULKER_BOX;
    public static final BlockEntityWithoutLevelRenderer instance;
    private final ChestBlockEntity chest;
    private final ChestBlockEntity trappedChest;
    private final EnderChestBlockEntity enderChest;
    private final BannerBlockEntity banner;
    private final BedBlockEntity bed;
    private final ConduitBlockEntity conduit;
    private final ShieldModel shieldModel;
    private final TridentModel tridentModel;
    
    public BlockEntityWithoutLevelRenderer() {
        this.chest = new ChestBlockEntity();
        this.trappedChest = new TrappedChestBlockEntity();
        this.enderChest = new EnderChestBlockEntity();
        this.banner = new BannerBlockEntity();
        this.bed = new BedBlockEntity();
        this.conduit = new ConduitBlockEntity();
        this.shieldModel = new ShieldModel();
        this.tridentModel = new TridentModel();
    }
    
    public void renderByItem(final ItemStack bki, final ItemTransforms.TransformType b, final PoseStack dhl, final MultiBufferSource ebz, final int integer5, final int integer6) {
        final Item bke8 = bki.getItem();
        if (!(bke8 instanceof BlockItem)) {
            if (bke8 == Items.SHIELD) {
                final boolean boolean9 = bki.getTagElement("BlockEntityTag") != null;
                dhl.pushPose();
                dhl.scale(1.0f, -1.0f, -1.0f);
                final Material enk10 = boolean9 ? ModelBakery.SHIELD_BASE : ModelBakery.NO_PATTERN_SHIELD;
                final VertexConsumer dhp11 = enk10.sprite().wrap(ItemRenderer.getFoilBufferDirect(ebz, this.shieldModel.renderType(enk10.atlasLocation()), true, bki.hasFoil()));
                this.shieldModel.handle().render(dhl, dhp11, integer5, integer6, 1.0f, 1.0f, 1.0f, 1.0f);
                if (boolean9) {
                    final List<Pair<BannerPattern, DyeColor>> list12 = BannerBlockEntity.createPatterns(ShieldItem.getColor(bki), BannerBlockEntity.getItemPatterns(bki));
                    BannerRenderer.renderPatterns(dhl, ebz, integer5, integer6, this.shieldModel.plate(), enk10, false, list12, bki.hasFoil());
                }
                else {
                    this.shieldModel.plate().render(dhl, dhp11, integer5, integer6, 1.0f, 1.0f, 1.0f, 1.0f);
                }
                dhl.popPose();
            }
            else if (bke8 == Items.TRIDENT) {
                dhl.pushPose();
                dhl.scale(1.0f, -1.0f, -1.0f);
                final VertexConsumer dhp12 = ItemRenderer.getFoilBufferDirect(ebz, this.tridentModel.renderType(TridentModel.TEXTURE), false, bki.hasFoil());
                this.tridentModel.renderToBuffer(dhl, dhp12, integer5, integer6, 1.0f, 1.0f, 1.0f, 1.0f);
                dhl.popPose();
            }
            return;
        }
        final Block bvr9 = ((BlockItem)bke8).getBlock();
        if (bvr9 instanceof AbstractSkullBlock) {
            GameProfile gameProfile10 = null;
            if (bki.hasTag()) {
                final CompoundTag le11 = bki.getTag();
                if (le11.contains("SkullOwner", 10)) {
                    gameProfile10 = NbtUtils.readGameProfile(le11.getCompound("SkullOwner"));
                }
                else if (le11.contains("SkullOwner", 8) && !StringUtils.isBlank((CharSequence)le11.getString("SkullOwner"))) {
                    gameProfile10 = new GameProfile((UUID)null, le11.getString("SkullOwner"));
                    gameProfile10 = SkullBlockEntity.updateGameprofile(gameProfile10);
                    le11.remove("SkullOwner");
                    le11.put("SkullOwner", NbtUtils.writeGameProfile(new CompoundTag(), gameProfile10));
                }
            }
            SkullBlockRenderer.renderSkull(null, 180.0f, ((AbstractSkullBlock)bvr9).getType(), gameProfile10, 0.0f, dhl, ebz, integer5);
            return;
        }
        BlockEntity cdl10;
        if (bvr9 instanceof AbstractBannerBlock) {
            this.banner.fromItem(bki, ((AbstractBannerBlock)bvr9).getColor());
            cdl10 = this.banner;
        }
        else if (bvr9 instanceof BedBlock) {
            this.bed.setColor(((BedBlock)bvr9).getColor());
            cdl10 = this.bed;
        }
        else if (bvr9 == Blocks.CONDUIT) {
            cdl10 = this.conduit;
        }
        else if (bvr9 == Blocks.CHEST) {
            cdl10 = this.chest;
        }
        else if (bvr9 == Blocks.ENDER_CHEST) {
            cdl10 = this.enderChest;
        }
        else if (bvr9 == Blocks.TRAPPED_CHEST) {
            cdl10 = this.trappedChest;
        }
        else {
            if (!(bvr9 instanceof ShulkerBoxBlock)) {
                return;
            }
            final DyeColor bje11 = ShulkerBoxBlock.getColorFromItem(bke8);
            if (bje11 == null) {
                cdl10 = BlockEntityWithoutLevelRenderer.DEFAULT_SHULKER_BOX;
            }
            else {
                cdl10 = BlockEntityWithoutLevelRenderer.SHULKER_BOXES[bje11.getId()];
            }
        }
        BlockEntityRenderDispatcher.instance.<BlockEntity>renderItem(cdl10, dhl, ebz, integer5, integer6);
    }
    
    static {
        SHULKER_BOXES = Arrays.<DyeColor>stream(DyeColor.values()).sorted(Comparator.comparingInt(DyeColor::getId)).map(ShulkerBoxBlockEntity::new).<ShulkerBoxBlockEntity>toArray(ShulkerBoxBlockEntity[]::new);
        DEFAULT_SHULKER_BOX = new ShulkerBoxBlockEntity((DyeColor)null);
        instance = new BlockEntityWithoutLevelRenderer();
    }
}
