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 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.client.resources.language.I18n;
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 String getFilterButtonTooltip() {
        return I18n.get(this.filterButton.isStateTriggered() ? this.getRecipeFilterName() : "gui.recipebook.toggleRecipes.all");
    }
    
    protected abstract String getRecipeFilterName();
    
    @Override
    public void slotClicked(@Nullable final Slot bca) {
        super.slotClicked(bca);
        if (bca != null && bca.index < this.menu.getSize()) {
            this.fuelSlot = null;
        }
    }
    
    @Override
    public void setupGhostRecipe(final Recipe<?> bgw, final List<Slot> list) {
        final ItemStack ben4 = bgw.getResultItem();
        this.ghostRecipe.setRecipe(bgw);
        this.ghostRecipe.addIngredient(Ingredient.of(ben4), list.get(2).x, list.get(2).y);
        final NonNullList<Ingredient> fy5 = bgw.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 = fy5.iterator();
        for (int integer7 = 0; integer7 < 2; ++integer7) {
            if (!iterator6.hasNext()) {
                return;
            }
            final Ingredient bgt8 = iterator6.next();
            if (!bgt8.isEmpty()) {
                final Slot bca9 = list.get(integer7);
                this.ghostRecipe.addIngredient(bgt8, bca9.x, bca9.y);
            }
        }
    }
    
    protected abstract Set<Item> getFuelItems();
    
    @Override
    public void renderGhostRecipe(final int integer1, final int integer2, final boolean boolean3, final float float4) {
        super.renderGhostRecipe(integer1, integer2, boolean3, float4);
        if (this.fuelSlot == null) {
            return;
        }
        if (!Screen.hasControlDown()) {
            this.time += float4;
        }
        final int integer3 = this.fuelSlot.x + integer1;
        final int integer4 = this.fuelSlot.y + integer2;
        GuiComponent.fill(integer3, integer4, integer3 + 16, integer4 + 16, 822018048);
        this.minecraft.getItemRenderer().renderAndDecorateItem(this.minecraft.player, this.getFuel().getDefaultInstance(), integer3, integer4);
        RenderSystem.depthFunc(516);
        GuiComponent.fill(integer3, integer4, integer3 + 16, integer4 + 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;
    }
}
