/*
 *  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.content.recipe;

import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;
import top.r3944realms.eroticdungeongame.content.item.DeviceKeyItem;
import top.r3944realms.eroticdungeongame.core.register.EDGItems;
import top.r3944realms.eroticdungeongame.core.register.EDGRecipeSerializers;

public class DungeonKeyCopyRecipe extends DungeonCustomRecipe {

    public DungeonKeyCopyRecipe(ResourceLocation id, DungeonCraftingBookCategory category) {
        super(id, category);
    }

    @Override
    public ItemStack getOutput() {
        return ItemStack.EMPTY;
    }

    @Override
    public boolean matches(@NotNull CraftingContainer craftingContainer, @NotNull Level level) {
        int emptyKeyCount = 0, tempCopyKeyCount = 0;
        for (int i = 0; i < craftingContainer.getWidth(); i++) {
            for (int j = 0; j < craftingContainer.getHeight(); j++) {
                int slotId = i * craftingContainer.getWidth() + j;
                ItemStack stack = craftingContainer.getItem(slotId);
                if (stack.getItem() instanceof DeviceKeyItem) {
                    if (stack.getOrCreateTag().contains(DeviceKeyItem.LOCK_CODE)) {
                        tempCopyKeyCount++;
                    } else emptyKeyCount++;
                }
                if (tempCopyKeyCount > 1 || emptyKeyCount > 1) return false;
            }
        }
        return tempCopyKeyCount == 1 && emptyKeyCount == 1;
    }

    @Override
    public @NotNull ItemStack assemble(@NotNull CraftingContainer craftingContainer, @NotNull RegistryAccess registryAccess) {
        long lockCode = -1;
        ItemStack result = EDGItems.DEVICE_KEY.get().getDefaultInstance();
        for (int i = 0; i < craftingContainer.getWidth(); i++) {
            for (int j = 0; j < craftingContainer.getHeight(); j++) {
                int slotId = i * craftingContainer.getWidth() + j;
                ItemStack stack = craftingContainer.getItem(slotId);
                if (stack.getItem() instanceof DeviceKeyItem) {
                    if (stack.getOrCreateTag().contains(DeviceKeyItem.LOCK_CODE)) {
                       lockCode = stack.getOrCreateTag().getLong(DeviceKeyItem.LOCK_CODE);
                    }
                }
            }
        }
        if (lockCode != -1) {
            result.setCount(2);
            result.getOrCreateTag().putLong(DeviceKeyItem.LOCK_CODE, lockCode);
        }
        return result;
    }

    @Override
    public boolean canCraftInDimensions(int width, int height) {
        return (width >= 2 && height > 0) || (height >= 2 && width > 0);
    }

    @Override
    public @NotNull RecipeSerializer<?> getSerializer() {
        return EDGRecipeSerializers.DUNGEON_KEY_COPY.get();
    }
}
