package net.halbear.aotg.custom.blocks.Forge;

import net.halbear.aotg.registries.ModItems;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.component.ItemContainerContents;

import java.util.*;

import static net.halbear.aotg.custom.blocks.Forge.ForgedAlloyRecipe.GetRecipesContainingMetals;

public record ForgeItemRecipe(Item primaryOutcomeItem, Item[] RequiredItems, int[] RequiredItemCounts) {
    public static final Map<Item, ForgeItemRecipe> FORGE_ITEM_RECIPE_MAP = new HashMap<>();
    public static final Map<Item, List<Item>> ITEM_RECIPE_REVERSE_LOOKUP = new HashMap<>();
    static{
        FORGE_ITEM_RECIPE_MAP.put(ModItems.XIPHOS_SWORD.asItem(),new ForgeItemRecipe(ModItems.XIPHOS_SWORD.asItem(),new Item[]{ModItems.BRONZE_INGOT.asItem(), Items.STICK},new int[]{2,1}));
        CompileReverseLookupMap();
    }

    public static void CompileReverseLookupMap(){
        ITEM_RECIPE_REVERSE_LOOKUP.clear();
        FORGE_ITEM_RECIPE_MAP.forEach((name, recipe)->{
            Arrays.stream(recipe.RequiredItems).toList().forEach(item -> {
                if(!ITEM_RECIPE_REVERSE_LOOKUP.containsKey(item)){
                    ITEM_RECIPE_REVERSE_LOOKUP.put(item, new ArrayList<>());
                }
                ITEM_RECIPE_REVERSE_LOOKUP.get(item).add(name);
            });
        });
    }

    public static List<ForgeItemRecipe> GetRecipesContainingItem(Item item){
        List<Item> Metal1Recipes = ITEM_RECIPE_REVERSE_LOOKUP.get(item);
        List<ForgeItemRecipe> overlapping = new ArrayList<>();
        if(Metal1Recipes != null) {
            for (int i = 0; i < Metal1Recipes.size(); i++) {
                if(Metal1Recipes.get(i) == null) continue;
                overlapping.add(FORGE_ITEM_RECIPE_MAP.get(Metal1Recipes.get(i)));
            }
        }
        return overlapping;
    }

    public static List<ForgeItemRecipe> GetValidRecipesFromContainer(ItemStack Pot, ItemStack Secondary){
        return GetAllValidRecipesFromContainer(Pot,Secondary).ItemRecipes;
    }

    public static RecipeSet GetAllValidRecipesFromContainer(ItemStack Pot, ItemStack Secondary){
        List<ForgedAlloyRecipe> availableAlloyRecipes = ForgedAlloyRecipe.GetValidRecipesFromContainer(Pot);
        List<Item> ItemsToSearchWith = new ArrayList<>();
        ItemsToSearchWith.add(Secondary.getItem());
            availableAlloyRecipes.forEach(forgedAlloyRecipe -> {
                ItemsToSearchWith.add(forgedAlloyRecipe.OutcomeItem());
            });
        Set<ForgeItemRecipe> ValidRecipeSet = new LinkedHashSet<>();
        ItemsToSearchWith.forEach(item->{
            ValidRecipeSet.addAll(GetRecipesContainingItem(item));
        });
        return new RecipeSet(availableAlloyRecipes, new ArrayList<>(ValidRecipeSet));
    }


    public record RecipeSet(List<ForgedAlloyRecipe> AlloyRecipes, List<ForgeItemRecipe> ItemRecipes){}
}
