package net.minecraft.client.renderer.entity;

import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.Entity;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
import net.minecraft.world.item.ItemStack;
import net.minecraft.client.resources.model.ModelManager;
import net.minecraft.client.renderer.block.BlockRenderDispatcher;
import net.minecraft.world.phys.Vec3;
import net.minecraft.core.Direction;
import net.minecraft.client.renderer.block.model.ItemTransforms;
import net.minecraft.world.item.MapItem;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.renderer.Sheets;
import net.minecraft.world.item.Items;
import com.mojang.math.Vector3f;
import net.minecraft.client.renderer.MultiBufferSource;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.world.entity.decoration.ItemFrame;

public class ItemFrameRenderer extends EntityRenderer<ItemFrame> {
    private static final ModelResourceLocation FRAME_LOCATION;
    private static final ModelResourceLocation MAP_FRAME_LOCATION;
    private final Minecraft minecraft;
    private final ItemRenderer itemRenderer;
    
    public ItemFrameRenderer(final EntityRenderDispatcher egm, final ItemRenderer ehh) {
        super(egm);
        this.minecraft = Minecraft.getInstance();
        this.itemRenderer = ehh;
    }
    
    @Override
    public void render(final ItemFrame bba, final float float2, final float float3, final PoseStack dhl, final MultiBufferSource ebz, final int integer) {
        super.render(bba, float2, float3, dhl, ebz, integer);
        dhl.pushPose();
        final Direction fz8 = bba.getDirection();
        final Vec3 dem9 = this.getRenderOffset(bba, float3);
        dhl.translate(-dem9.x(), -dem9.y(), -dem9.z());
        final double double10 = 0.46875;
        dhl.translate(fz8.getStepX() * 0.46875, fz8.getStepY() * 0.46875, fz8.getStepZ() * 0.46875);
        dhl.mulPose(Vector3f.XP.rotationDegrees(bba.xRot));
        dhl.mulPose(Vector3f.YP.rotationDegrees(180.0f - bba.yRot));
        final boolean boolean12 = bba.isInvisible();
        if (!boolean12) {
            final BlockRenderDispatcher ecq13 = this.minecraft.getBlockRenderer();
            final ModelManager enm14 = ecq13.getBlockModelShaper().getModelManager();
            final ModelResourceLocation enn15 = (bba.getItem().getItem() == Items.FILLED_MAP) ? ItemFrameRenderer.MAP_FRAME_LOCATION : ItemFrameRenderer.FRAME_LOCATION;
            dhl.pushPose();
            dhl.translate(-0.5, -0.5, -0.5);
            ecq13.getModelRenderer().renderModel(dhl.last(), ebz.getBuffer(Sheets.solidBlockSheet()), null, enm14.getModel(enn15), 1.0f, 1.0f, 1.0f, integer, OverlayTexture.NO_OVERLAY);
            dhl.popPose();
        }
        final ItemStack bki13 = bba.getItem();
        if (!bki13.isEmpty()) {
            final boolean boolean13 = bki13.getItem() == Items.FILLED_MAP;
            if (boolean12) {
                dhl.translate(0.0, 0.0, 0.5);
            }
            else {
                dhl.translate(0.0, 0.0, 0.4375);
            }
            final int integer2 = boolean13 ? (bba.getRotation() % 4 * 2) : bba.getRotation();
            dhl.mulPose(Vector3f.ZP.rotationDegrees(integer2 * 360.0f / 8.0f));
            if (boolean13) {
                dhl.mulPose(Vector3f.ZP.rotationDegrees(180.0f));
                final float float4 = 0.0078125f;
                dhl.scale(0.0078125f, 0.0078125f, 0.0078125f);
                dhl.translate(-64.0, -64.0, 0.0);
                final MapItemSavedData czv17 = MapItem.getOrCreateSavedData(bki13, bba.level);
                dhl.translate(0.0, 0.0, -1.0);
                if (czv17 != null) {
                    this.minecraft.gameRenderer.getMapRenderer().render(dhl, ebz, czv17, true, integer);
                }
            }
            else {
                dhl.scale(0.5f, 0.5f, 0.5f);
                this.itemRenderer.renderStatic(bki13, ItemTransforms.TransformType.FIXED, integer, OverlayTexture.NO_OVERLAY, dhl, ebz);
            }
        }
        dhl.popPose();
    }
    
    @Override
    public Vec3 getRenderOffset(final ItemFrame bba, final float float2) {
        return new Vec3(bba.getDirection().getStepX() * 0.3f, -0.25, bba.getDirection().getStepZ() * 0.3f);
    }
    
    @Override
    public ResourceLocation getTextureLocation(final ItemFrame bba) {
        return TextureAtlas.LOCATION_BLOCKS;
    }
    
    @Override
    protected boolean shouldShowName(final ItemFrame bba) {
        if (!Minecraft.renderNames() || bba.getItem().isEmpty() || !bba.getItem().hasCustomHoverName() || this.entityRenderDispatcher.crosshairPickEntity != bba) {
            return false;
        }
        final double double3 = this.entityRenderDispatcher.distanceToSqr(bba);
        final float float5 = bba.isDiscrete() ? 32.0f : 64.0f;
        return double3 < float5 * float5;
    }
    
    @Override
    protected void renderNameTag(final ItemFrame bba, final Component mr, final PoseStack dhl, final MultiBufferSource ebz, final int integer) {
        super.renderNameTag(bba, bba.getItem().getHoverName(), dhl, ebz, integer);
    }
    
    static {
        FRAME_LOCATION = new ModelResourceLocation("item_frame", "map=false");
        MAP_FRAME_LOCATION = new ModelResourceLocation("item_frame", "map=true");
    }
}
