/*
 *  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.emi.category;

import dev.emi.emi.api.recipe.EmiRecipe;
import dev.emi.emi.api.recipe.EmiRecipeCategory;
import dev.emi.emi.api.render.EmiTexture;
import dev.emi.emi.api.stack.EmiIngredient;
import dev.emi.emi.api.stack.EmiStack;
import dev.emi.emi.api.widget.WidgetHolder;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import top.r3944realms.eroticdungeongame.EroticDungeon;
import top.r3944realms.eroticdungeongame.core.compat.emi.DungeonEmiPlugin;
import top.r3944realms.eroticdungeongame.core.compat.emi.widget.DungeonCraftingNoSlotWidget;

import java.util.List;

@SuppressWarnings("ClassCanBeRecord")
public class DungeonCraftingEmiRecipe implements EmiRecipe {
    private static final ResourceLocation CRAFTING_TABLE_LOCATION = EroticDungeon.rl("textures/gui/container/dungeon_crafting_table.png");
    private static final EmiTexture DUNGEON_CRAFTING = new EmiTexture(CRAFTING_TABLE_LOCATION, 29, 16, 116, 54);
    private final ResourceLocation id;
    private final List<EmiStack> outputs;
    private final List<EmiIngredient> inputs;

    public DungeonCraftingEmiRecipe(ResourceLocation id, List<EmiIngredient> inputs, List<EmiStack> outputs) {
        this.id = id;
        this.inputs = inputs;
        this.outputs = outputs;
    }

    @Override
    public EmiRecipeCategory getCategory() {
        return DungeonEmiPlugin.DUNGEON_CRAFTING;
    }

    @Override
    public @Nullable ResourceLocation getId() {
        return id;
    }

    @Override
    public List<EmiIngredient> getInputs() {
        return inputs;
    }

    @Override
    public List<EmiStack> getOutputs() {
        return outputs;
    }

    @Override
    public boolean supportsRecipeTree() {
        return true;
    }

    @Override
    public int getDisplayWidth() {
        return 116;
    }

    @Override
    public int getDisplayHeight() {
        return 54;
    }

    @Override
    public void addWidgets(WidgetHolder widgets) {
        widgets.addTexture(DUNGEON_CRAFTING, 0, 0);
        int borderSlotSize = 18;
        for (int row = 0; row <= 2; ++row) {
            for (int column = 0; column <= 2; ++column) {
                int inputIndex = row * 3 + column;
                widgets.add(new DungeonCraftingNoSlotWidget(getInput(inputs, inputIndex), column * borderSlotSize, row * borderSlotSize));
            }
        }
        DungeonCraftingNoSlotWidget widget = new DungeonCraftingNoSlotWidget(outputs.get(0), 90, 14);
        widget.large(true);
        widget.recipeContext(this);
        widgets.add(widget);

    }

    private EmiIngredient getInput(@NotNull List<EmiIngredient> inputs, int index) {
        if (index < inputs.size()) {
            return inputs.get(index);
        }
        return EmiStack.EMPTY;
    }
}
