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

import net.halbear.aotg.AgeoftheGods;
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 net.minecraft.world.level.material.Fluids;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public record ForgedAlloyRecipe(Item OutcomeItem, Item RequiredMetal1, Item RequiredMetal2, float AlloyRatio) {
    public static final Map<Item,ForgedAlloyRecipe> RecipeMap = new HashMap<>();
    public static final Map<Item, List<Item>> ReverseLookupRecipeMap = new HashMap<>();
    static {
        RecipeMap.put(ModItems.BRONZE_INGOT.asItem(), new ForgedAlloyRecipe(ModItems.BRONZE_INGOT.asItem(), Items.COPPER_INGOT, ModItems.TIN_INGOT.asItem(),9f));
        RecipeMap.put(ModItems.ELECTRUM_INGOT.asItem(), new ForgedAlloyRecipe(ModItems.ELECTRUM_INGOT.asItem(), Items.GOLD_INGOT, ModItems.SILVER_INGOT.asItem(), 3f));
        RecipeMap.put(ModItems.ORICHALCUM_INGOT.asItem(), new ForgedAlloyRecipe(ModItems.ORICHALCUM_INGOT.asItem(), Items.COPPER_INGOT, ModItems.ZINC_INGOT.asItem(),4f));
        RecipeMap.put(ModItems.HEPATIZON_INGOT.asItem(), new ForgedAlloyRecipe(ModItems.HEPATIZON_INGOT.asItem(), Items.COPPER_INGOT, Items.GOLD_INGOT,9f));
        CompileReverseLookupMap();
    }

    public static void CompileReverseLookupMap(){
        ReverseLookupRecipeMap.clear();
        RecipeMap.forEach((name, recipe)->{
            Item item1 = recipe.RequiredMetal1;
            Item item2 = recipe.RequiredMetal2;
            if(!ReverseLookupRecipeMap.containsKey(item1)){
                ReverseLookupRecipeMap.put(item1, new ArrayList<>());
            }
            if(!ReverseLookupRecipeMap.containsKey(item2)){
                ReverseLookupRecipeMap.put(item2, new ArrayList<>());
            }
            ReverseLookupRecipeMap.get(item1).add(name);
            ReverseLookupRecipeMap.get(item2).add(name);
        });
    }

    public static List<ForgedAlloyRecipe> GetRecipesContainingMetals(Item Metal1, Item Metal2){
        List<Item> Metal1Recipes = ReverseLookupRecipeMap.get(Metal1);
        List<Item> Metal2Recipes = ReverseLookupRecipeMap.get(Metal2);
        List<ForgedAlloyRecipe> overlapping = new ArrayList<>();
        if(Metal1Recipes != null && Metal2Recipes != null) {
            for (Item metal1Recipe : Metal1Recipes) {
                if (metal1Recipe == null) continue;
                if (Metal2Recipes.contains(metal1Recipe)) overlapping.add(RecipeMap.get(metal1Recipe));
            }
        }
        return overlapping;
    }

    public static List<ForgedAlloyRecipe> GetValidRecipesFromContainer(ItemStack Pot){
        List<ForgedAlloyRecipe> availableRecipes = new ArrayList<>();
        List<ForgedAlloyRecipe> ValidRecipes = new ArrayList<>();
        if(Pot.getItem().equals(Items.DECORATED_POT)){
            ItemContainerContents potContents = Pot.get(DataComponents.CONTAINER);
            if (potContents != null) {
                boolean hasMarker = potContents.getStackInSlot(0).getItem().equals(ModItems.FLUID_INDICATOR_ITEM.asItem());
                if (hasMarker) {
                    ItemStack AlloyMetal1 = potContents.getStackInSlot(1);
                    ItemStack AlloyMetal2 = potContents.getStackInSlot(2);
                    availableRecipes = GetRecipesContainingMetals(AlloyMetal1.getItem(),AlloyMetal2.getItem());
                    availableRecipes.forEach(forgedAlloyRecipe -> {
                        if(Math.floor((float) AlloyMetal1.getCount() /(float)AlloyMetal2.getCount()) == Math.floor(forgedAlloyRecipe.AlloyRatio)) {
                            ValidRecipes.add(forgedAlloyRecipe);
                        }
                    });
                }
            }
        }
        return ValidRecipes;
    }

    public static List<ForgedAlloyRecipe> GetAvailableRecipesFromContainer(ItemStack Pot){
        List<ForgedAlloyRecipe> availableRecipes = new ArrayList<>();
        if(Pot.getItem().equals(Items.DECORATED_POT)){
            ItemContainerContents potContents = Pot.get(DataComponents.CONTAINER);
            if (potContents != null) {
                boolean hasMarker = potContents.getStackInSlot(0).getItem().equals(ModItems.FLUID_INDICATOR_ITEM.asItem());
                if (hasMarker) {
                    ItemStack AlloyMetal1 = potContents.getStackInSlot(1);
                    ItemStack AlloyMetal2 = potContents.getStackInSlot(2);
                    return GetRecipesContainingMetals(AlloyMetal1.getItem(),AlloyMetal2.getItem());
                }
            }
        }
        return availableRecipes;
    }
}
