/*
 *  Copyright 2025-2026 R3944Realms
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package top.r3944realms.eroticdungeongame.client.gui.components;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.recipebook.RecipeButton;
import net.minecraft.util.Mth;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.jetbrains.annotations.NotNull;

import java.util.List;

@OnlyIn(Dist.CLIENT)
public class DungeonRecipeButton extends RecipeButton {
    public void renderWidget(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
        if (!Screen.hasControlDown()) {
            this.time += partialTick;
        }

        int i = 29;
        if (!this.collection.hasCraftable()) {
            i += 25;
        }

        int j = 206;
        if (this.collection.getRecipes(this.book.isFiltering(this.menu)).size() > 1) {
            j += 25;
        }

        boolean flag = this.animationTime > 0.0F;
        if (flag) {
            float f = 1.0F + 0.1F * (float)Math.sin((double)(this.animationTime / 15.0F * 3.1415927F));
            guiGraphics.pose().pushPose();
            guiGraphics.pose().translate((float)(this.getX() + 8), (float)(this.getY() + 12), 0.0F);
            guiGraphics.pose().scale(f, f, 1.0F);
            guiGraphics.pose().translate((float)(-(this.getX() + 8)), (float)(-(this.getY() + 12)), 0.0F);
            this.animationTime -= partialTick;
        }

        guiGraphics.blit(DungeonRecipeBookComponent.RECIPE_BOOK_LOCATION, this.getX(), this.getY(), i, j, this.width, this.height);
        List<Recipe<?>> list = this.getOrderedRecipes();
        this.currentIndex = Mth.floor(this.time / 30.0F) % list.size();
        ItemStack itemstack = list.get(this.currentIndex).getResultItem(this.collection.registryAccess());
        int k = 4;
        if (this.collection.hasSingleResultItem() && this.getOrderedRecipes().size() > 1) {
            guiGraphics.renderItem(itemstack, this.getX() + k + 1, this.getY() + k + 1, 0, 10);
            --k;
        }

        guiGraphics.renderFakeItem(itemstack, this.getX() + k, this.getY() + k);
        if (flag) {
            guiGraphics.pose().popPose();
        }

    }
}
