package net.minecraft.client.renderer.entity;

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 dvf, final ItemRenderer dvz) {
        super(dvf);
        this.minecraft = Minecraft.getInstance();
        this.itemRenderer = dvz;
    }
    
    @Override
    public void render(final ItemFrame avr, final float float2, final float float3, final PoseStack cyl, final MultiBufferSource dqt, final int integer) {
        super.render(avr, float2, float3, cyl, dqt, integer);
        cyl.pushPose();
        final Direction fp8 = avr.getDirection();
        final Vec3 cvl9 = this.getRenderOffset(avr, float3);
        cyl.translate(-cvl9.x(), -cvl9.y(), -cvl9.z());
        final double double10 = 0.46875;
        cyl.translate(fp8.getStepX() * 0.46875, fp8.getStepY() * 0.46875, fp8.getStepZ() * 0.46875);
        cyl.mulPose(Vector3f.XP.rotationDegrees(avr.xRot));
        cyl.mulPose(Vector3f.YP.rotationDegrees(180.0f - avr.yRot));
        final BlockRenderDispatcher drk12 = this.minecraft.getBlockRenderer();
        final ModelManager eca13 = drk12.getBlockModelShaper().getModelManager();
        final ModelResourceLocation ecb14 = (avr.getItem().getItem() == Items.FILLED_MAP) ? ItemFrameRenderer.MAP_FRAME_LOCATION : ItemFrameRenderer.FRAME_LOCATION;
        cyl.pushPose();
        cyl.translate(-0.5, -0.5, -0.5);
        drk12.getModelRenderer().renderModel(cyl.last(), dqt.getBuffer(Sheets.solidBlockSheet()), null, eca13.getModel(ecb14), 1.0f, 1.0f, 1.0f, integer, OverlayTexture.NO_OVERLAY);
        cyl.popPose();
        final ItemStack ben15 = avr.getItem();
        if (!ben15.isEmpty()) {
            final boolean boolean16 = ben15.getItem() == Items.FILLED_MAP;
            cyl.translate(0.0, 0.0, 0.4375);
            final int integer2 = boolean16 ? (avr.getRotation() % 4 * 2) : avr.getRotation();
            cyl.mulPose(Vector3f.ZP.rotationDegrees(integer2 * 360.0f / 8.0f));
            if (boolean16) {
                cyl.mulPose(Vector3f.ZP.rotationDegrees(180.0f));
                final float float4 = 0.0078125f;
                cyl.scale(0.0078125f, 0.0078125f, 0.0078125f);
                cyl.translate(-64.0, -64.0, 0.0);
                final MapItemSavedData crg19 = MapItem.getOrCreateSavedData(ben15, avr.level);
                cyl.translate(0.0, 0.0, -1.0);
                if (crg19 != null) {
                    this.minecraft.gameRenderer.getMapRenderer().render(cyl, dqt, crg19, true, integer);
                }
            }
            else {
                cyl.scale(0.5f, 0.5f, 0.5f);
                this.itemRenderer.renderStatic(ben15, ItemTransforms.TransformType.FIXED, integer, OverlayTexture.NO_OVERLAY, cyl, dqt);
            }
        }
        cyl.popPose();
    }
    
    @Override
    public Vec3 getRenderOffset(final ItemFrame avr, final float float2) {
        return new Vec3(avr.getDirection().getStepX() * 0.3f, -0.25, avr.getDirection().getStepZ() * 0.3f);
    }
    
    @Override
    public ResourceLocation getTextureLocation(final ItemFrame avr) {
        return TextureAtlas.LOCATION_BLOCKS;
    }
    
    @Override
    protected boolean shouldShowName(final ItemFrame avr) {
        if (!Minecraft.renderNames() || avr.getItem().isEmpty() || !avr.getItem().hasCustomHoverName() || this.entityRenderDispatcher.crosshairPickEntity != avr) {
            return false;
        }
        final double double3 = this.entityRenderDispatcher.distanceToSqr(avr);
        final float float5 = avr.isDiscrete() ? 32.0f : 64.0f;
        return double3 < float5 * float5;
    }
    
    @Override
    protected void renderNameTag(final ItemFrame avr, final String string, final PoseStack cyl, final MultiBufferSource dqt, final int integer) {
        super.renderNameTag(avr, avr.getItem().getHoverName().getColoredString(), cyl, dqt, integer);
    }
    
    static {
        FRAME_LOCATION = new ModelResourceLocation("item_frame", "map=false");
        MAP_FRAME_LOCATION = new ModelResourceLocation("item_frame", "map=true");
    }
}
