/*
 *  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;

import mezz.jei.api.IModPlugin;
import mezz.jei.api.JeiPlugin;
import mezz.jei.api.registration.*;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.crafting.RecipeManager;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;
import top.r3944realms.eroticdungeongame.EroticDungeon;
import top.r3944realms.eroticdungeongame.client.gui.screens.DungeonCraftingScreen;
import top.r3944realms.eroticdungeongame.content.menu.DungeonCraftingMenu;
import top.r3944realms.eroticdungeongame.content.recipe.DungeonCustomRecipe;
import top.r3944realms.eroticdungeongame.content.recipe.DungeonRecipe;
import top.r3944realms.eroticdungeongame.core.compat.jei.categorty.DungeonCraftingJeiCategory;
import top.r3944realms.eroticdungeongame.core.register.EDGItems;
import top.r3944realms.eroticdungeongame.core.register.EDGMenuTypes;
import top.r3944realms.eroticdungeongame.core.register.EDGRecipeTypes;
import top.r3944realms.lib39.util.ILevelHelper;

import java.util.List;

@JeiPlugin
@SuppressWarnings("unused")
public class DungeonJeiPlugin implements IModPlugin {
    @Override
    public @NotNull ResourceLocation getPluginUid() {
        return EroticDungeon.rl("jei_plugin");
    }

    @Override
    public void registerCategories(@NotNull IRecipeCategoryRegistration registration) {
        registration.addRecipeCategories(new DungeonCraftingJeiCategory(registration.getJeiHelpers().getGuiHelper()));
    }
    @Override
    public void registerRecipes(@NotNull IRecipeRegistration registration) {
        Level level;
        if (ILevelHelper.getServerLevel() != null) {
            level = ILevelHelper.getServerLevel();
        } else if (ILevelHelper.getClientLevel() != null) {
            level = ILevelHelper.getClientLevel();
        } else throw new IllegalStateException("DungeonJeiPlugin doesn't have a level");
        RecipeManager recipeManager = level.getRecipeManager();
        List<DungeonRecipe> recipes = recipeManager.getAllRecipesFor(EDGRecipeTypes.DUNGEON_CRAFTING.get()).stream().filter(i -> !(i instanceof DungeonCustomRecipe)).toList();
        registration.addRecipes(DungeonCraftingJeiCategory.DUNGEON_RECIPE_TYPE, recipes);
    }

    @Override
    public void registerGuiHandlers(@NotNull IGuiHandlerRegistration registration) {
        // 注册GUI交互器
        registration.addRecipeClickArea(DungeonCraftingScreen.class, 88, 32, 28, 23,
                DungeonCraftingJeiCategory.DUNGEON_RECIPE_TYPE);
    }

    @Override
    public void registerRecipeCatalysts(@NotNull IRecipeCatalystRegistration registration) {
        // 注册配方催化剂
        registration.addRecipeCatalyst(EDGItems.DUNGEON_CRAFTING_TABLE.get().getDefaultInstance(), DungeonCraftingJeiCategory.DUNGEON_RECIPE_TYPE);
    }

    @Override
    public void registerRecipeTransferHandlers(IRecipeTransferRegistration registration) {
        registration.addRecipeTransferHandler(DungeonCraftingMenu.class, EDGMenuTypes.DUNGEON_CRAFTING_MENU.get(), DungeonCraftingJeiCategory.DUNGEON_RECIPE_TYPE, 1, 9, 10, 36);
    }
}
