package com.zivilon.cinder_loe.recipe;

import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.world.World;
import com.zivilon.cinder_loe.CinderLoE;

public class ToxicCoreArrowsRecipe implements IRecipe {

    private final ItemStack result;

    public ToxicCoreArrowsRecipe(ItemStack result) {
        this.result = result;
    }

    @Override
    public boolean matches(InventoryCrafting inv, World worldIn) {
        ItemStack toxicCore = null;
        int arrowCount = 0;

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

            if (itemstack != null) {
                if (itemstack.getItem() == CinderLoE.toxicCore) {
                    if (toxicCore != null) {
                        return false; // Only one toxic core allowed
                    }
                    toxicCore = itemstack;
                } else if (itemstack.getItem() == Items.arrow) {
                    arrowCount++;
                } else {
                    return false; // Invalid item in grid
                }
            }
        }

        if (toxicCore != null && arrowCount > 0) {
            return toxicCore.getItemDamage() + arrowCount <= toxicCore.getMaxDamage();
        }

        return false;
    }

    @Override
    public ItemStack getCraftingResult(InventoryCrafting inv) {
        ItemStack toxicCore = null;
        int arrowCount = 0;

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

            if (itemstack != null) {
                if (itemstack.getItem() == CinderLoE.toxicCore) {
                    toxicCore = itemstack;
                } else if (itemstack.getItem() == Items.arrow) {
                    arrowCount++;
                }
            }
        }

        if (toxicCore != null && arrowCount > 0) {
            ItemStack resultStack = new ItemStack(result.getItem(), arrowCount);
            return resultStack;
        }

        return null;
    }

    @Override
    public int getRecipeSize() {
        return 9;
    }

    @Override
    public ItemStack getRecipeOutput() {
        return result;
    }
}
