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.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 ben, final PoseStack cyl, final MultiBufferSource dqt, final int integer4, final int integer5) {
        final Item bei7 = ben.getItem();
        if (!(bei7 instanceof BlockItem)) {
            if (bei7 == Items.SHIELD) {
                final boolean boolean8 = ben.getTagElement("BlockEntityTag") != null;
                cyl.pushPose();
                cyl.scale(1.0f, -1.0f, -1.0f);
                final Material eby9 = boolean8 ? ModelBakery.SHIELD_BASE : ModelBakery.NO_PATTERN_SHIELD;
                final VertexConsumer cyo10 = eby9.sprite().wrap(ItemRenderer.getFoilBuffer(dqt, this.shieldModel.renderType(eby9.atlasLocation()), false, ben.hasFoil()));
                this.shieldModel.handle().render(cyl, cyo10, integer4, integer5, 1.0f, 1.0f, 1.0f, 1.0f);
                if (boolean8) {
                    final List<Pair<BannerPattern, DyeColor>> list11 = BannerBlockEntity.createPatterns(ShieldItem.getColor(ben), BannerBlockEntity.getItemPatterns(ben));
                    BannerRenderer.renderPatterns(cyl, dqt, integer4, integer5, this.shieldModel.plate(), eby9, false, list11);
                }
                else {
                    this.shieldModel.plate().render(cyl, cyo10, integer4, integer5, 1.0f, 1.0f, 1.0f, 1.0f);
                }
                cyl.popPose();
            }
            else if (bei7 == Items.TRIDENT) {
                cyl.pushPose();
                cyl.scale(1.0f, -1.0f, -1.0f);
                final VertexConsumer cyo11 = ItemRenderer.getFoilBuffer(dqt, this.tridentModel.renderType(TridentModel.TEXTURE), false, ben.hasFoil());
                this.tridentModel.renderToBuffer(cyl, cyo11, integer4, integer5, 1.0f, 1.0f, 1.0f, 1.0f);
                cyl.popPose();
            }
            return;
        }
        final Block bph8 = ((BlockItem)bei7).getBlock();
        if (bph8 instanceof AbstractSkullBlock) {
            GameProfile gameProfile9 = null;
            if (ben.hasTag()) {
                final CompoundTag jt10 = ben.getTag();
                if (jt10.contains("SkullOwner", 10)) {
                    gameProfile9 = NbtUtils.readGameProfile(jt10.getCompound("SkullOwner"));
                }
                else if (jt10.contains("SkullOwner", 8) && !StringUtils.isBlank((CharSequence)jt10.getString("SkullOwner"))) {
                    gameProfile9 = new GameProfile((UUID)null, jt10.getString("SkullOwner"));
                    gameProfile9 = SkullBlockEntity.updateGameprofile(gameProfile9);
                    jt10.remove("SkullOwner");
                    jt10.put("SkullOwner", NbtUtils.writeGameProfile(new CompoundTag(), gameProfile9));
                }
            }
            SkullBlockRenderer.renderSkull(null, 180.0f, ((AbstractSkullBlock)bph8).getType(), gameProfile9, 0.0f, cyl, dqt, integer4);
            return;
        }
        BlockEntity bwl9;
        if (bph8 instanceof AbstractBannerBlock) {
            this.banner.fromItem(ben, ((AbstractBannerBlock)bph8).getColor());
            bwl9 = this.banner;
        }
        else if (bph8 instanceof BedBlock) {
            this.bed.setColor(((BedBlock)bph8).getColor());
            bwl9 = this.bed;
        }
        else if (bph8 == Blocks.CONDUIT) {
            bwl9 = this.conduit;
        }
        else if (bph8 == Blocks.CHEST) {
            bwl9 = this.chest;
        }
        else if (bph8 == Blocks.ENDER_CHEST) {
            bwl9 = this.enderChest;
        }
        else if (bph8 == Blocks.TRAPPED_CHEST) {
            bwl9 = this.trappedChest;
        }
        else {
            if (!(bph8 instanceof ShulkerBoxBlock)) {
                return;
            }
            final DyeColor bdj10 = ShulkerBoxBlock.getColorFromItem(bei7);
            if (bdj10 == null) {
                bwl9 = BlockEntityWithoutLevelRenderer.DEFAULT_SHULKER_BOX;
            }
            else {
                bwl9 = BlockEntityWithoutLevelRenderer.SHULKER_BOXES[bdj10.getId()];
            }
        }
        BlockEntityRenderDispatcher.instance.<BlockEntity>renderItem(bwl9, cyl, dqt, integer4, integer5);
    }
    
    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();
    }
}
