package com.husj.recipe;

import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.registry.Registries;

import java.util.HashMap;
import java.util.Map;

public class DisassemblerRecipes {
    private static final Map<Item, ItemStack> RECIPES = new HashMap<>();

    static {
        // 钻石工具
        registerRecipe(Items.DIAMOND_SWORD, Items.DIAMOND, 2);
        registerRecipe(Items.DIAMOND_PICKAXE, Items.DIAMOND, 3);
        registerRecipe(Items.DIAMOND_AXE, Items.DIAMOND, 3);
        registerRecipe(Items.DIAMOND_SHOVEL, Items.DIAMOND, 1);
        registerRecipe(Items.DIAMOND_HOE, Items.DIAMOND, 2);

        // 铁工具
        registerRecipe(Items.IRON_SWORD, Items.IRON_INGOT, 2);
        registerRecipe(Items.IRON_PICKAXE, Items.IRON_INGOT, 3);
        registerRecipe(Items.IRON_AXE, Items.IRON_INGOT, 3);
        registerRecipe(Items.IRON_SHOVEL, Items.IRON_INGOT, 1);
        registerRecipe(Items.IRON_HOE, Items.IRON_INGOT, 2);

        // 金工具
        registerRecipe(Items.GOLDEN_SWORD, Items.GOLD_INGOT, 2);
        registerRecipe(Items.GOLDEN_PICKAXE, Items.GOLD_INGOT, 3);
        registerRecipe(Items.GOLDEN_AXE, Items.GOLD_INGOT, 3);
        registerRecipe(Items.GOLDEN_SHOVEL, Items.GOLD_INGOT, 1);
        registerRecipe(Items.GOLDEN_HOE, Items.GOLD_INGOT, 2);

        // 石工具
        registerRecipe(Items.STONE_SWORD, Items.COBBLESTONE, 2);
        registerRecipe(Items.STONE_PICKAXE, Items.COBBLESTONE, 3);
        registerRecipe(Items.STONE_AXE, Items.COBBLESTONE, 3);
        registerRecipe(Items.STONE_SHOVEL, Items.COBBLESTONE, 1);
        registerRecipe(Items.STONE_HOE, Items.COBBLESTONE, 2);

        // 铜工具
        // 铜盔甲
        registerRecipe(Items.COPPER_HELMET, Items.COPPER_INGOT, 5);
        registerRecipe(Items.COPPER_CHESTPLATE, Items.COPPER_INGOT, 8);
        registerRecipe(Items.COPPER_LEGGINGS, Items.COPPER_INGOT, 7);
        registerRecipe(Items.COPPER_BOOTS, Items.COPPER_INGOT, 4);
        // 铜工具
        registerRecipe(Items.COPPER_SWORD, Items.COPPER_INGOT, 2);
        registerRecipe(Items.COPPER_PICKAXE, Items.COPPER_INGOT, 3);
        registerRecipe(Items.COPPER_AXE, Items.COPPER_INGOT, 3);
        registerRecipe(Items.COPPER_SHOVEL, Items.COPPER_INGOT, 1);
        registerRecipe(Items.COPPER_HOE, Items.COPPER_INGOT, 2);

        // 钻石盔甲
        registerRecipe(Items.DIAMOND_HELMET, Items.DIAMOND, 5);
        registerRecipe(Items.DIAMOND_CHESTPLATE, Items.DIAMOND, 8);
        registerRecipe(Items.DIAMOND_LEGGINGS, Items.DIAMOND, 7);
        registerRecipe(Items.DIAMOND_BOOTS, Items.DIAMOND, 4);

        // 铁盔甲
        registerRecipe(Items.IRON_HELMET, Items.IRON_INGOT, 5);
        registerRecipe(Items.IRON_CHESTPLATE, Items.IRON_INGOT, 8);
        registerRecipe(Items.IRON_LEGGINGS, Items.IRON_INGOT, 7);
        registerRecipe(Items.IRON_BOOTS, Items.IRON_INGOT, 4);

        // 金盔甲
        registerRecipe(Items.GOLDEN_HELMET, Items.GOLD_INGOT, 5);
        registerRecipe(Items.GOLDEN_CHESTPLATE, Items.GOLD_INGOT, 8);
        registerRecipe(Items.GOLDEN_LEGGINGS, Items.GOLD_INGOT, 7);
        registerRecipe(Items.GOLDEN_BOOTS, Items.GOLD_INGOT, 4);

        // 下界合金工具（简化版，只返还下界合金）
        registerRecipe(Items.NETHERITE_SWORD, Items.NETHERITE_INGOT, 1);
        registerRecipe(Items.NETHERITE_PICKAXE, Items.NETHERITE_INGOT, 1);
        registerRecipe(Items.NETHERITE_AXE, Items.NETHERITE_INGOT, 1);
        registerRecipe(Items.NETHERITE_SHOVEL, Items.NETHERITE_INGOT, 1);
        registerRecipe(Items.NETHERITE_HOE, Items.NETHERITE_INGOT, 1);

        // 下界合金盔甲（简化版，只返还下界合金）
        registerRecipe(Items.NETHERITE_HELMET, Items.NETHERITE_INGOT, 1);
        registerRecipe(Items.NETHERITE_CHESTPLATE, Items.NETHERITE_INGOT, 1);
        registerRecipe(Items.NETHERITE_LEGGINGS, Items.NETHERITE_INGOT, 1);
        registerRecipe(Items.NETHERITE_BOOTS, Items.NETHERITE_INGOT, 1);
    }

    private static void registerRecipe(Item tool, Item material, int count) {
        RECIPES.put(tool, new ItemStack(material, count));
    }

    public static boolean hasRecipe(ItemStack stack) {
        return RECIPES.containsKey(stack.getItem());
    }

    public static ItemStack getRecipeResult(ItemStack stack) {
        ItemStack result = RECIPES.get(stack.getItem());
        if (result != null) {
            return result.copy();
        }
        return ItemStack.EMPTY;
    }

    // 检查是否为矿物工具（排除木制工具）
    public static boolean isMineralTool(ItemStack stack) {
        Item item = stack.getItem();

        // 排除木制工具
        if (item == Items.WOODEN_SWORD || item == Items.WOODEN_PICKAXE ||
                item == Items.WOODEN_AXE || item == Items.WOODEN_SHOVEL ||
                item == Items.WOODEN_HOE) {
            return false;
        }

        // 检查是否为已注册的工具
        return hasRecipe(stack);
    }
}