/*
 *  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 com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.recipebook.OverlayRecipeComponent;
import net.minecraft.client.gui.screens.recipebook.RecipeCollection;
import net.minecraft.util.Mth;
import net.minecraft.world.inventory.AbstractFurnaceMenu;
import net.minecraft.world.inventory.RecipeBookMenu;
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.Collections;
import java.util.List;

@OnlyIn(Dist.CLIENT)
public class DungeonOverlayRecipeComponent extends OverlayRecipeComponent {
    @Override
    public void init(Minecraft minecraft, @NotNull RecipeCollection collection, int x, int y, int i3, int i2, float v) {
        this.minecraft = minecraft;
        this.collection = collection;
        assert minecraft.player != null;
        if (minecraft.player.containerMenu instanceof AbstractFurnaceMenu) {
            this.isFurnaceMenu = true;
        }

        boolean flag = minecraft.player.getRecipeBook().isFiltering((RecipeBookMenu<?>)minecraft.player.containerMenu);
        List<Recipe<?>> list = collection.getDisplayRecipes(true);
        List<Recipe<?>> list1 = flag ? Collections.emptyList() : collection.getDisplayRecipes(false);
        int i = list.size();
        int j = i + list1.size();
        int k = j <= 16 ? 4 : 5;
        int l = (int)Math.ceil((float)j / (float)k);
        this.x = x;
        this.y = y;
        float f = (float)(this.x + Math.min(j, k) * 25);
        float f1 = (float)(i3 + 50);
        if (f > f1) {
            this.x = (int)((float)this.x - v * (float)((int)((f - f1) / v)));
        }

        float f2 = (float)(this.y + l * 25);
        float f3 = (float)(i2 + 50);
        if (f2 > f3) {
            this.y = (int)((float)this.y - v * (float)Mth.ceil((f2 - f3) / v));
        }

        float f4 = (float)this.y;
        float f5 = (float)(i2 - 100);
        if (f4 < f5) {
            this.y = (int)((float)this.y - v * (float)Mth.ceil((f4 - f5) / v));
        }

        this.setVisible(true);
        this.recipeButtons.clear();

        for(int i1 = 0; i1 < j; ++i1) {
            boolean flag1 = i1 < i;
            Recipe<?> recipe = flag1 ? list.get(i1) : list1.get(i1 - i);
            int j1 = this.x + 4 + 25 * (i1 % k);
            int k1 = this.y + 5 + 25 * (i1 / k);
            if (this.isFurnaceMenu) {
                this.recipeButtons.add(new DungeonOverlaySmeltingOverlayRecipeButton(j1, k1, recipe, flag1));
            } else {
                this.recipeButtons.add(new DungeonOverlayRecipeButton(j1, k1, recipe, flag1));
            }
        }

        this.lastRecipeClicked = null;
    }

    @Override
    public void render(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
        if (this.isVisible()) {
            this.time += partialTick;
            RenderSystem.enableBlend();
            guiGraphics.pose().pushPose();
            guiGraphics.pose().translate(0.0F, 0.0F, 1000.0F);
            int i = this.recipeButtons.size() <= 16 ? 4 : 5;
            int j = Math.min(this.recipeButtons.size(), i);
            int k = Mth.ceil((float)this.recipeButtons.size() / (float)i);
            guiGraphics.blitNineSliced(DungeonRecipeBookComponent.RECIPE_BOOK_LOCATION, this.x, this.y, j * 25 + 8, k * 25 + 8, 4, 32, 32, 82, 208);
            RenderSystem.disableBlend();
            for(OverlayRecipeButton button : this.recipeButtons) {
                button.render(guiGraphics, mouseX, mouseY, partialTick);
            }
            guiGraphics.pose().popPose();
        }

    }
    @OnlyIn(Dist.CLIENT)
    public class DungeonOverlaySmeltingOverlayRecipeButton extends DungeonOverlayRecipeButton {
        public DungeonOverlaySmeltingOverlayRecipeButton(int x, int y, Recipe<?> recipe, boolean isCraftable) {
            super(x, y, recipe, isCraftable);
        }

        protected void calculateIngredientsPositions(Recipe<?> recipe) {
            ItemStack[] aitemstack = recipe.getIngredients().get(0).getItems();
            this.ingredientPos.add(new OverlayRecipeButton.Pos(10, 10, aitemstack));
        }
    }
    @OnlyIn(Dist.CLIENT)
    public class DungeonOverlayRecipeButton extends OverlayRecipeButton {
        public DungeonOverlayRecipeButton(int x, int y, Recipe<?> recipe, boolean isCraftable) {
            super(x, y, recipe, isCraftable);
        }
        public void renderWidget(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
            int i = 152;
            if (!this.isCraftable) {
                i += 26;
            }

            int j = DungeonOverlayRecipeComponent.this.isFurnaceMenu ? 130 : 78;
            if (this.isHoveredOrFocused()) {
                j += 26;
            }

            guiGraphics.blit(DungeonRecipeBookComponent.RECIPE_BOOK_LOCATION, this.getX(), this.getY(), i, j, this.width, this.height);
            guiGraphics.pose().pushPose();
            guiGraphics.pose().translate(this.getX() + 2, this.getY() + 2, 150.0D);

            for(OverlayRecipeComponent.OverlayRecipeButton.Pos overlayrecipecomponent$overlayrecipebutton$pos : this.ingredientPos) {
                guiGraphics.pose().pushPose();
                guiGraphics.pose().translate((double)overlayrecipecomponent$overlayrecipebutton$pos.x, (double)overlayrecipecomponent$overlayrecipebutton$pos.y, 0.0D);
                guiGraphics.pose().scale(0.375F, 0.375F, 1.0F);
                guiGraphics.pose().translate(-8.0D, -8.0D, 0.0D);
                if (overlayrecipecomponent$overlayrecipebutton$pos.ingredients.length > 0) {
                    guiGraphics.renderItem(overlayrecipecomponent$overlayrecipebutton$pos.ingredients[Mth.floor(DungeonOverlayRecipeComponent.this.time / 30.0F) % overlayrecipecomponent$overlayrecipebutton$pos.ingredients.length], 0, 0);
                }

                guiGraphics.pose().popPose();
            }

            guiGraphics.pose().popPose();
        }
    }
}
