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

import net.minecraft.world.item.ItemStack;
import net.minecraft.client.renderer.entity.ItemRenderer;
import com.mojang.blaze3d.systems.RenderSystem;
import java.util.Iterator;
import java.util.List;
import net.minecraft.client.ClientRecipeBook;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.inventory.RecipeBookMenu;
import net.minecraft.client.Minecraft;
import net.minecraft.client.RecipeBookCategories;
import net.minecraft.client.gui.components.StateSwitchingButton;

public class RecipeBookTabButton extends StateSwitchingButton {
    private final RecipeBookCategories category;
    private float animationTime;
    
    public RecipeBookTabButton(final RecipeBookCategories dbu) {
        super(0, 0, 35, 27, false);
        this.category = dbu;
        this.initTextureValues(153, 2, 35, 0, RecipeBookComponent.RECIPE_BOOK_LOCATION);
    }
    
    public void startAnimation(final Minecraft dbn) {
        final ClientRecipeBook dbc3 = dbn.player.getRecipeBook();
        final List<RecipeCollection> list4 = dbc3.getCollection(this.category);
        if (!(dbn.player.containerMenu instanceof RecipeBookMenu)) {
            return;
        }
        for (final RecipeCollection dit6 : list4) {
            for (final Recipe<?> bgw8 : dit6.getRecipes(dbc3.isFilteringCraftable(dbn.player.containerMenu))) {
                if (dbc3.willHighlight(bgw8)) {
                    this.animationTime = 15.0f;
                }
            }
        }
    }
    
    @Override
    public void renderButton(final int integer1, final int integer2, final float float3) {
        if (this.animationTime > 0.0f) {
            final float float4 = 1.0f + 0.1f * (float)Math.sin(this.animationTime / 15.0f * 3.1415927f);
            RenderSystem.pushMatrix();
            RenderSystem.translatef((float)(this.x + 8), (float)(this.y + 12), 0.0f);
            RenderSystem.scalef(1.0f, float4, 1.0f);
            RenderSystem.translatef((float)(-(this.x + 8)), (float)(-(this.y + 12)), 0.0f);
        }
        final Minecraft dbn5 = Minecraft.getInstance();
        dbn5.getTextureManager().bind(this.resourceLocation);
        RenderSystem.disableDepthTest();
        int integer3 = this.xTexStart;
        int integer4 = this.yTexStart;
        if (this.isStateTriggered) {
            integer3 += this.xDiffTex;
        }
        if (this.isHovered()) {
            integer4 += this.yDiffTex;
        }
        int integer5 = this.x;
        if (this.isStateTriggered) {
            integer5 -= 2;
        }
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        this.blit(integer5, this.y, integer3, integer4, this.width, this.height);
        RenderSystem.enableDepthTest();
        this.renderIcon(dbn5.getItemRenderer());
        if (this.animationTime > 0.0f) {
            RenderSystem.popMatrix();
            this.animationTime -= float3;
        }
    }
    
    private void renderIcon(final ItemRenderer dvz) {
        final List<ItemStack> list3 = this.category.getIconItems();
        final int integer4 = this.isStateTriggered ? -2 : 0;
        if (list3.size() == 1) {
            dvz.renderAndDecorateItem(list3.get(0), this.x + 9 + integer4, this.y + 5);
        }
        else if (list3.size() == 2) {
            dvz.renderAndDecorateItem(list3.get(0), this.x + 3 + integer4, this.y + 5);
            dvz.renderAndDecorateItem(list3.get(1), this.x + 14 + integer4, this.y + 5);
        }
    }
    
    public RecipeBookCategories getCategory() {
        return this.category;
    }
    
    public boolean updateVisibility(final ClientRecipeBook dbc) {
        final List<RecipeCollection> list3 = dbc.getCollection(this.category);
        this.visible = false;
        if (list3 != null) {
            for (final RecipeCollection dit5 : list3) {
                if (dit5.hasKnownRecipes() && dit5.hasFitting()) {
                    this.visible = true;
                    break;
                }
            }
        }
        return this.visible;
    }
}
