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

import net.minecraft.client.gui.components.Button;
import net.minecraft.world.inventory.ClickType;
import net.minecraft.world.inventory.Slot;
import net.minecraft.client.renderer.entity.EntityRenderDispatcher;
import com.mojang.math.Quaternion;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.Minecraft;
import com.mojang.math.Vector3f;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.world.entity.LivingEntity;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gui.components.ImageButton;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.world.inventory.RecipeBookMenu;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.entity.player.Player;
import net.minecraft.client.gui.screens.recipebook.RecipeBookComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.client.gui.screens.recipebook.RecipeUpdateListener;
import net.minecraft.world.inventory.InventoryMenu;

public class InventoryScreen extends EffectRenderingInventoryScreen<InventoryMenu> implements RecipeUpdateListener {
    private static final ResourceLocation RECIPE_BUTTON_LOCATION;
    private float xMouse;
    private float yMouse;
    private final RecipeBookComponent recipeBookComponent;
    private boolean recipeBookComponentInitialized;
    private boolean widthTooNarrow;
    private boolean buttonClicked;
    
    public InventoryScreen(final Player ayj) {
        super(ayj.inventoryMenu, ayj.inventory, new TranslatableComponent("container.crafting", new Object[0]));
        this.recipeBookComponent = new RecipeBookComponent();
        this.passEvents = true;
    }
    
    @Override
    public void tick() {
        if (this.minecraft.gameMode.hasInfiniteItems()) {
            this.minecraft.setScreen(new CreativeModeInventoryScreen(this.minecraft.player));
            return;
        }
        this.recipeBookComponent.tick();
    }
    
    @Override
    protected void init() {
        if (this.minecraft.gameMode.hasInfiniteItems()) {
            this.minecraft.setScreen(new CreativeModeInventoryScreen(this.minecraft.player));
            return;
        }
        super.init();
        this.widthTooNarrow = (this.width < 379);
        this.recipeBookComponent.init(this.width, this.height, this.minecraft, this.widthTooNarrow, this.menu);
        this.recipeBookComponentInitialized = true;
        this.leftPos = this.recipeBookComponent.updateScreenPosition(this.widthTooNarrow, this.width, this.imageWidth);
        this.children.add(this.recipeBookComponent);
        this.setInitialFocus(this.recipeBookComponent);
        this.<ImageButton>addButton(new ImageButton(this.leftPos + 104, this.height / 2 - 22, 20, 18, 0, 0, 19, InventoryScreen.RECIPE_BUTTON_LOCATION, dcv -> {
            this.recipeBookComponent.initVisuals(this.widthTooNarrow);
            this.recipeBookComponent.toggleVisibility();
            this.leftPos = this.recipeBookComponent.updateScreenPosition(this.widthTooNarrow, this.width, this.imageWidth);
            dcv.setPosition(this.leftPos + 104, this.height / 2 - 22);
            this.buttonClicked = true;
        }));
    }
    
    @Override
    protected void renderLabels(final int integer1, final int integer2) {
        this.font.draw(this.title.getColoredString(), 97.0f, 8.0f, 4210752);
    }
    
    @Override
    public void render(final int integer1, final int integer2, final float float3) {
        this.renderBackground();
        this.doRenderEffects = !this.recipeBookComponent.isVisible();
        if (this.recipeBookComponent.isVisible() && this.widthTooNarrow) {
            this.renderBg(float3, integer1, integer2);
            this.recipeBookComponent.render(integer1, integer2, float3);
        }
        else {
            this.recipeBookComponent.render(integer1, integer2, float3);
            super.render(integer1, integer2, float3);
            this.recipeBookComponent.renderGhostRecipe(this.leftPos, this.topPos, false, float3);
        }
        this.renderTooltip(integer1, integer2);
        this.recipeBookComponent.renderTooltip(this.leftPos, this.topPos, integer1, integer2);
        this.xMouse = (float)integer1;
        this.yMouse = (float)integer2;
        this.magicalSpecialHackyFocus(this.recipeBookComponent);
    }
    
    @Override
    protected void renderBg(final float float1, final int integer2, final int integer3) {
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        this.minecraft.getTextureManager().bind(InventoryScreen.INVENTORY_LOCATION);
        final int integer4 = this.leftPos;
        final int integer5 = this.topPos;
        this.blit(integer4, integer5, 0, 0, this.imageWidth, this.imageHeight);
        renderEntityInInventory(integer4 + 51, integer5 + 75, 30, integer4 + 51 - this.xMouse, integer5 + 75 - 50 - this.yMouse, this.minecraft.player);
    }
    
    public static void renderEntityInInventory(final int integer1, final int integer2, final int integer3, final float float4, final float float5, final LivingEntity akz) {
        final float float6 = (float)Math.atan(float4 / 40.0f);
        final float float7 = (float)Math.atan(float5 / 40.0f);
        RenderSystem.pushMatrix();
        RenderSystem.translatef((float)integer1, (float)integer2, 1050.0f);
        RenderSystem.scalef(1.0f, 1.0f, -1.0f);
        final PoseStack cyl9 = new PoseStack();
        cyl9.translate(0.0, 0.0, 1000.0);
        cyl9.scale((float)integer3, (float)integer3, (float)integer3);
        final Quaternion c10 = Vector3f.ZP.rotationDegrees(180.0f);
        final Quaternion c11 = Vector3f.XP.rotationDegrees(float7 * 20.0f);
        c10.mul(c11);
        cyl9.mulPose(c10);
        final float float8 = akz.yBodyRot;
        final float float9 = akz.yRot;
        final float float10 = akz.xRot;
        final float float11 = akz.yHeadRotO;
        final float float12 = akz.yHeadRot;
        akz.yBodyRot = 180.0f + float6 * 20.0f;
        akz.yRot = 180.0f + float6 * 40.0f;
        akz.xRot = -float7 * 20.0f;
        akz.yHeadRot = akz.yRot;
        akz.yHeadRotO = akz.yRot;
        final EntityRenderDispatcher dvf17 = Minecraft.getInstance().getEntityRenderDispatcher();
        c11.conj();
        dvf17.overrideCameraOrientation(c11);
        dvf17.setRenderShadow(false);
        final MultiBufferSource.BufferSource a18 = Minecraft.getInstance().renderBuffers().bufferSource();
        dvf17.<LivingEntity>render(akz, 0.0, 0.0, 0.0, 0.0f, 1.0f, cyl9, a18, 15728880);
        a18.endBatch();
        dvf17.setRenderShadow(true);
        akz.yBodyRot = float8;
        akz.yRot = float9;
        akz.xRot = float10;
        akz.yHeadRotO = float11;
        akz.yHeadRot = float12;
        RenderSystem.popMatrix();
    }
    
    @Override
    protected boolean isHovering(final int integer1, final int integer2, final int integer3, final int integer4, final double double5, final double double6) {
        return (!this.widthTooNarrow || !this.recipeBookComponent.isVisible()) && super.isHovering(integer1, integer2, integer3, integer4, double5, double6);
    }
    
    @Override
    public boolean mouseClicked(final double double1, final double double2, final int integer) {
        return this.recipeBookComponent.mouseClicked(double1, double2, integer) || ((!this.widthTooNarrow || !this.recipeBookComponent.isVisible()) && super.mouseClicked(double1, double2, integer));
    }
    
    @Override
    public boolean mouseReleased(final double double1, final double double2, final int integer) {
        if (this.buttonClicked) {
            this.buttonClicked = false;
            return true;
        }
        return super.mouseReleased(double1, double2, integer);
    }
    
    @Override
    protected boolean hasClickedOutside(final double double1, final double double2, final int integer3, final int integer4, final int integer5) {
        final boolean boolean9 = double1 < integer3 || double2 < integer4 || double1 >= integer3 + this.imageWidth || double2 >= integer4 + this.imageHeight;
        return this.recipeBookComponent.hasClickedOutside(double1, double2, this.leftPos, this.topPos, this.imageWidth, this.imageHeight, integer5) && boolean9;
    }
    
    @Override
    protected void slotClicked(final Slot bca, final int integer2, final int integer3, final ClickType bav) {
        super.slotClicked(bca, integer2, integer3, bav);
        this.recipeBookComponent.slotClicked(bca);
    }
    
    @Override
    public void recipesUpdated() {
        this.recipeBookComponent.recipesUpdated();
    }
    
    @Override
    public void removed() {
        if (this.recipeBookComponentInitialized) {
            this.recipeBookComponent.removed();
        }
        super.removed();
    }
    
    @Override
    public RecipeBookComponent getRecipeBookComponent() {
        return this.recipeBookComponent;
    }
    
    static {
        RECIPE_BUTTON_LOCATION = new ResourceLocation("textures/gui/recipe_button.png");
    }
}
