package net.minecraft.client.gui.screens.inventory;

import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.MultiBufferSource;
import com.mojang.blaze3d.vertex.Tesselator;
import javax.annotation.Nullable;
import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.Level;
import net.minecraft.world.item.MapItem;
import net.minecraft.world.item.Items;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.CartographyTableMenu;

public class CartographyTableScreen extends AbstractContainerScreen<CartographyTableMenu> {
    private static final ResourceLocation BG_LOCATION;
    
    public CartographyTableScreen(final CartographyTableMenu bat, final Inventory ayi, final Component lf) {
        super(bat, ayi, lf);
    }
    
    @Override
    public void render(final int integer1, final int integer2, final float float3) {
        super.render(integer1, integer2, float3);
        this.renderTooltip(integer1, integer2);
    }
    
    @Override
    protected void renderLabels(final int integer1, final int integer2) {
        this.font.draw(this.title.getColoredString(), 8.0f, 4.0f, 4210752);
        this.font.draw(this.inventory.getDisplayName().getColoredString(), 8.0f, (float)(this.imageHeight - 96 + 2), 4210752);
    }
    
    @Override
    protected void renderBg(final float float1, final int integer2, final int integer3) {
        this.renderBackground();
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        this.minecraft.getTextureManager().bind(CartographyTableScreen.BG_LOCATION);
        final int integer4 = this.leftPos;
        final int integer5 = this.topPos;
        this.blit(integer4, integer5, 0, 0, this.imageWidth, this.imageHeight);
        final Item bei7 = ((CartographyTableMenu)this.menu).getSlot(1).getItem().getItem();
        final boolean boolean8 = bei7 == Items.MAP;
        final boolean boolean9 = bei7 == Items.PAPER;
        final boolean boolean10 = bei7 == Items.GLASS_PANE;
        final ItemStack ben11 = ((CartographyTableMenu)this.menu).getSlot(0).getItem();
        boolean boolean11 = false;
        MapItemSavedData crg12;
        if (ben11.getItem() == Items.FILLED_MAP) {
            crg12 = MapItem.getSavedData(ben11, this.minecraft.level);
            if (crg12 != null) {
                if (crg12.locked) {
                    boolean11 = true;
                    if (boolean9 || boolean10) {
                        this.blit(integer4 + 35, integer5 + 31, this.imageWidth + 50, 132, 28, 21);
                    }
                }
                if (boolean9 && crg12.scale >= 4) {
                    boolean11 = true;
                    this.blit(integer4 + 35, integer5 + 31, this.imageWidth + 50, 132, 28, 21);
                }
            }
        }
        else {
            crg12 = null;
        }
        this.renderResultingMap(crg12, boolean8, boolean9, boolean10, boolean11);
    }
    
    private void renderResultingMap(@Nullable final MapItemSavedData crg, final boolean boolean2, final boolean boolean3, final boolean boolean4, final boolean boolean5) {
        final int integer7 = this.leftPos;
        final int integer8 = this.topPos;
        if (boolean3 && !boolean5) {
            this.blit(integer7 + 67, integer8 + 13, this.imageWidth, 66, 66, 66);
            this.renderMap(crg, integer7 + 85, integer8 + 31, 0.226f);
        }
        else if (boolean2) {
            this.blit(integer7 + 67 + 16, integer8 + 13, this.imageWidth, 132, 50, 66);
            this.renderMap(crg, integer7 + 86, integer8 + 16, 0.34f);
            this.minecraft.getTextureManager().bind(CartographyTableScreen.BG_LOCATION);
            RenderSystem.pushMatrix();
            RenderSystem.translatef(0.0f, 0.0f, 1.0f);
            this.blit(integer7 + 67, integer8 + 13 + 16, this.imageWidth, 132, 50, 66);
            this.renderMap(crg, integer7 + 70, integer8 + 32, 0.34f);
            RenderSystem.popMatrix();
        }
        else if (boolean4) {
            this.blit(integer7 + 67, integer8 + 13, this.imageWidth, 0, 66, 66);
            this.renderMap(crg, integer7 + 71, integer8 + 17, 0.45f);
            this.minecraft.getTextureManager().bind(CartographyTableScreen.BG_LOCATION);
            RenderSystem.pushMatrix();
            RenderSystem.translatef(0.0f, 0.0f, 1.0f);
            this.blit(integer7 + 66, integer8 + 12, 0, this.imageHeight, 66, 66);
            RenderSystem.popMatrix();
        }
        else {
            this.blit(integer7 + 67, integer8 + 13, this.imageWidth, 0, 66, 66);
            this.renderMap(crg, integer7 + 71, integer8 + 17, 0.45f);
        }
    }
    
    private void renderMap(@Nullable final MapItemSavedData crg, final int integer2, final int integer3, final float float4) {
        if (crg != null) {
            RenderSystem.pushMatrix();
            RenderSystem.translatef((float)integer2, (float)integer3, 1.0f);
            RenderSystem.scalef(float4, float4, 1.0f);
            final MultiBufferSource.BufferSource a6 = MultiBufferSource.immediate(Tesselator.getInstance().getBuilder());
            this.minecraft.gameRenderer.getMapRenderer().render(new PoseStack(), a6, crg, true, 15728880);
            a6.endBatch();
            RenderSystem.popMatrix();
        }
    }
    
    static {
        BG_LOCATION = new ResourceLocation("textures/gui/container/cartography_table.png");
    }
}
