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

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.client.gui.GuiComponent;
import net.minecraft.client.gui.screens.Screen;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.ItemStack;
import java.util.List;
import net.minecraft.world.item.crafting.Recipe;
import javax.annotation.Nullable;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.world.inventory.Slot;
import java.util.Set;
import net.minecraft.world.item.Item;
import java.util.Iterator;

public abstract class AbstractFurnaceRecipeBookComponent extends RecipeBookComponent {
    private Iterator<Item> iterator;
    private Set<Item> fuels;
    private Slot fuelSlot;
    private Item fuel;
    private float time;
    
    @Override
    protected boolean updateFiltering() {
        final boolean boolean2 = !this.getFilteringCraftable();
        this.setFilteringCraftable(boolean2);
        return boolean2;
    }
    
    protected abstract boolean getFilteringCraftable();
    
    protected abstract void setFilteringCraftable(final boolean boolean1);
    
    @Override
    public boolean isVisible() {
        return this.isGuiOpen();
    }
    
    protected abstract boolean isGuiOpen();
    
    @Override
    protected void setVisible(final boolean boolean1) {
        this.setGuiOpen(boolean1);
        if (!boolean1) {
            this.recipeBookPage.setInvisible();
        }
        this.sendUpdateSettings();
    }
    
    protected abstract void setGuiOpen(final boolean boolean1);
    
    @Override
    protected void initFilterButtonTextures() {
        this.filterButton.initTextureValues(152, 182, 28, 18, AbstractFurnaceRecipeBookComponent.RECIPE_BOOK_LOCATION);
    }
    
    @Override
    protected Component getFilterButtonTooltip() {
        return this.filterButton.isStateTriggered() ? this.getRecipeFilterName() : new TranslatableComponent("gui.recipebook.toggleRecipes.all");
    }
    
    protected abstract Component getRecipeFilterName();
    
    @Override
    public void slotClicked(@Nullable final Slot bhw) {
        super.slotClicked(bhw);
        if (bhw != null && bhw.index < this.menu.getSize()) {
            this.fuelSlot = null;
        }
    }
    
    @Override
    public void setupGhostRecipe(final Recipe<?> bmu, final List<Slot> list) {
        final ItemStack bki4 = bmu.getResultItem();
        this.ghostRecipe.setRecipe(bmu);
        this.ghostRecipe.addIngredient(Ingredient.of(bki4), list.get(2).x, list.get(2).y);
        final NonNullList<Ingredient> gi5 = bmu.getIngredients();
        this.fuelSlot = list.get(1);
        if (this.fuels == null) {
            this.fuels = this.getFuelItems();
        }
        this.iterator = this.fuels.iterator();
        this.fuel = null;
        final Iterator<Ingredient> iterator6 = gi5.iterator();
        for (int integer7 = 0; integer7 < 2; ++integer7) {
            if (!iterator6.hasNext()) {
                return;
            }
            final Ingredient bmr8 = iterator6.next();
            if (!bmr8.isEmpty()) {
                final Slot bhw9 = list.get(integer7);
                this.ghostRecipe.addIngredient(bmr8, bhw9.x, bhw9.y);
            }
        }
    }
    
    protected abstract Set<Item> getFuelItems();
    
    @Override
    public void renderGhostRecipe(final PoseStack dhl, final int integer2, final int integer3, final boolean boolean4, final float float5) {
        super.renderGhostRecipe(dhl, integer2, integer3, boolean4, float5);
        if (this.fuelSlot == null) {
            return;
        }
        if (!Screen.hasControlDown()) {
            this.time += float5;
        }
        final int integer4 = this.fuelSlot.x + integer2;
        final int integer5 = this.fuelSlot.y + integer3;
        GuiComponent.fill(dhl, integer4, integer5, integer4 + 16, integer5 + 16, 822018048);
        this.minecraft.getItemRenderer().renderAndDecorateItem(this.minecraft.player, this.getFuel().getDefaultInstance(), integer4, integer5);
        RenderSystem.depthFunc(516);
        GuiComponent.fill(dhl, integer4, integer5, integer4 + 16, integer5 + 16, 822083583);
        RenderSystem.depthFunc(515);
    }
    
    private Item getFuel() {
        if (this.fuel == null || this.time > 30.0f) {
            this.time = 0.0f;
            if (this.iterator == null || !this.iterator.hasNext()) {
                if (this.fuels == null) {
                    this.fuels = this.getFuelItems();
                }
                this.iterator = this.fuels.iterator();
            }
            this.fuel = this.iterator.next();
        }
        return this.fuel;
    }
}
