/*
 *  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.core.compat.jei.categorty;

import mezz.jei.api.constants.VanillaTypes;
import mezz.jei.api.gui.builder.IRecipeLayoutBuilder;
import mezz.jei.api.gui.drawable.IDrawable;
import mezz.jei.api.helpers.IGuiHelper;
import mezz.jei.api.recipe.IFocusGroup;
import mezz.jei.api.recipe.RecipeIngredientRole;
import mezz.jei.api.recipe.RecipeType;
import mezz.jei.api.recipe.category.IRecipeCategory;
import net.minecraft.core.NonNullList;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import top.r3944realms.eroticdungeongame.EroticDungeon;
import top.r3944realms.eroticdungeongame.content.recipe.DungeonRecipe;
import top.r3944realms.eroticdungeongame.content.recipe.DungeonShapedRecipe;
import top.r3944realms.eroticdungeongame.content.recipe.DungeonShapelessRecipe;
import top.r3944realms.eroticdungeongame.core.register.EDGItems;

import java.util.Arrays;

public class DungeonCraftingJeiCategory implements IRecipeCategory<DungeonRecipe> {
    public static final ResourceLocation UID = EroticDungeon.rl("dungeon_crafting_table");
    private static final MutableComponent TITLE = Component.translatable("container.eroticdungeongame.crafting_table");
    private static final ResourceLocation CRAFTING_TABLE_LOCATION = EroticDungeon.rl("textures/gui/container/dungeon_crafting_table.png");

    private final IDrawable bgDraw;
    private final IDrawable icon;
    public static final RecipeType<DungeonRecipe> DUNGEON_RECIPE_TYPE =
            new RecipeType<>(UID, DungeonRecipe.class);

    public DungeonCraftingJeiCategory(IGuiHelper helper) {
        this.icon = helper.createDrawableIngredient(VanillaTypes.ITEM_STACK, new ItemStack(EDGItems.DUNGEON_CRAFTING_TABLE.get()));
        this.bgDraw = helper.createDrawable(CRAFTING_TABLE_LOCATION,29, 16, 116, 54);
    }
    @Override
    public @NotNull RecipeType<DungeonRecipe> getRecipeType() {
        return DUNGEON_RECIPE_TYPE;
    }

    @Override
    public @NotNull Component getTitle() {
        return TITLE;
    }

    @SuppressWarnings("removal")
    @Override
    public @Nullable IDrawable getBackground() {
        return bgDraw;
    }

    @Override
    public @Nullable IDrawable getIcon() {
        return icon;
    }

    @Override
    public void setRecipe(@NotNull IRecipeLayoutBuilder builder, @NotNull DungeonRecipe recipe, @NotNull IFocusGroup focuses) {
        NonNullList<Ingredient> ingredients = recipe.getIngredients();
        ItemStack resultStack = recipe.getOutput();
        if(recipe instanceof DungeonShapedRecipe || recipe instanceof DungeonShapelessRecipe){
            int borderSlotSize = 18;
            for (int row = 0; row <= 2; ++row) {
                for (int column = 0; column <= 2; ++column) {
                    int inputIndex = row * 3 + column;
                    if (inputIndex < ingredients.size()) {
                        builder.addSlot(RecipeIngredientRole.INPUT, (column * borderSlotSize) + 1, (row * borderSlotSize) + 1)
                                .addItemStacks(Arrays.asList(ingredients.get(inputIndex).getItems()));
                    }
                }
            }
            builder.addSlot(RecipeIngredientRole.OUTPUT, 94, 20)
                    .addItemStack(resultStack);
            if(recipe instanceof DungeonShapelessRecipe) builder.setShapeless();
        }
    }
}
