package com.zivilon.cinder_loe;

import java.util.List;
import java.util.ArrayList;

import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.ShapelessRecipes;
import net.minecraft.world.World;
import net.minecraft.item.crafting.IRecipe;

public class ShapelessDurabilityRecipe implements IRecipe {
    private final ItemStack recipeOutput;
    private final List<ItemStack> recipeItems;
    private final Item toolItem;

    public ShapelessDurabilityRecipe(ItemStack result, Item toolItem, ItemStack... ingredients) {
        this.recipeOutput = result;
        this.recipeItems = new ArrayList<>(ingredients.length);
        for (ItemStack ingredient : ingredients) {
            this.recipeItems.add(ingredient);
        }
        this.toolItem = toolItem;
        System.out.println("[CinderLoE] Created ShapelessDurabilityRecipe for: " + result.getUnlocalizedName());
        System.out.println("[CinderLoE] Ingredients: " + this.recipeItems);
    }

    @Override
    public boolean matches(InventoryCrafting inv, World worldIn) {

        boolean hasTool = false;
        List<ItemStack> ingredientsCopy = new ArrayList<>(this.recipeItems);

        for (int i = 0; i < inv.getSizeInventory(); ++i) {
            ItemStack stack = inv.getStackInSlot(i);
            if (stack != null) {
                if (stack.getItem() == toolItem) {
                    hasTool = true;
                } else {
                    boolean matched = false;
                    for (ItemStack recipeStack : ingredientsCopy) {
                        if (stack.getItem() == recipeStack.getItem() && (recipeStack.getItemDamage() == 32767 || stack.getItemDamage() == recipeStack.getItemDamage())) {
                            matched = true;
                            ingredientsCopy.remove(recipeStack);
                            break;
                        }
                    }
                    if (!matched) {
                        return false;
                    }
                }
            }
        }

        boolean matches = hasTool && ingredientsCopy.isEmpty();
        return matches;
    }

    @Override
    public ItemStack getCraftingResult(InventoryCrafting inv) {
        return this.recipeOutput.copy();
    }

    @Override
    public int getRecipeSize() {
        return this.recipeItems.size();
    }

    @Override
    public ItemStack getRecipeOutput() {
        return this.recipeOutput;
    }

    public ItemStack[] getRemainingItems(InventoryCrafting inv) {
        ItemStack[] remainingItems = new ItemStack[inv.getSizeInventory()];

        for (int i = 0; i < remainingItems.length; ++i) {
            ItemStack itemstack = inv.getStackInSlot(i);

            if (itemstack != null && itemstack.getItem() == toolItem) {
                ItemStack tool = itemstack.copy();
                tool.setItemDamage(tool.getItemDamage() + 1);

                if (tool.getItemDamage() >= tool.getMaxDamage()) {
                    tool = null;
                }

                remainingItems[i] = tool;
            } else if (itemstack != null && itemstack.getItem().hasContainerItem(itemstack)) {
                remainingItems[i] = itemstack.getItem().getContainerItem(itemstack);
            } else {
                remainingItems[i] = itemstack;
            }
        }

        return remainingItems;
    }
}
