import { resultSlot, liquidSlot, slotGen } from "./helpers.js";
import { cookTimeTotalMini } from "./helpers.js";
import { consumeItem, addResult } from "./helpers.js";

export default {
	name: "steel_toe_stout",
	cookTime: cookTimeTotalMini,
	requiredTemp: "1",
	isValid: (inv, tempSlot) => {
		let ironSlot = -1, wartSlot = -1, wheatSlot = -1, fungusSlot = -1;
		let filled = 0;
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item) {
				filled++;
				if (item.typeId === "minecraft:iron_ingot" && ironSlot === -1) ironSlot = slot;
				else if (item.typeId === "minecraft:nether_wart" && wartSlot === -1) wartSlot = slot;
				else if (item.typeId === "minecraft:wheat" && wheatSlot === -1) wheatSlot = slot;
				else if (item.typeId === "minecraft:crimson_fungus" && fungusSlot === -1) fungusSlot = slot;
			}
		}
		const strongroot = inv.getItem(liquidSlot);
		const tankard = inv.getItem(slotGen);
		const result = inv.getItem(resultSlot);
		return ironSlot !== -1 && wartSlot !== -1 && wheatSlot !== -1 && fungusSlot !== -1 &&
			   filled === 4 &&
			   strongroot && strongroot.typeId === "brewinandchewin:strongroot_ale" && strongroot.amount >= 1 &&
			   tankard && tankard.typeId === "brewinandchewin:tankard" && tankard.amount >= 1 &&
			   (!result || result.typeId === "brewinandchewin:steel_toe_stout") &&
			   tempSlot && tempSlot.nameTag === "1";
	},
	onComplete: (inv, keg, overworld) => {
		let ironSlot = -1, wartSlot = -1, wheatSlot = -1, fungusSlot = -1;
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item && item.typeId === "minecraft:iron_ingot") ironSlot = slot;
			if (item && item.typeId === "minecraft:nether_wart") wartSlot = slot;
			if (item && item.typeId === "minecraft:wheat") wheatSlot = slot;
			if (item && item.typeId === "minecraft:crimson_fungus") fungusSlot = slot;
		}
		if (ironSlot !== -1) consumeItem(inv, ironSlot, 1);
		if (wartSlot !== -1) consumeItem(inv, wartSlot, 1);
		if (wheatSlot !== -1) consumeItem(inv, wheatSlot, 1);
		if (fungusSlot !== -1) consumeItem(inv, fungusSlot, 1);
		
		const strongroot = inv.getItem(liquidSlot);
		if (strongroot) consumeItem(inv, liquidSlot, 1);
		
		const tankard = inv.getItem(slotGen);
		if (tankard) consumeItem(inv, slotGen, 1);
		
		addResult(inv, "brewinandchewin:steel_toe_stout", 1);
		returnTaraToBuffer(inv, "brewinandchewin:tankard", 1);
	}
};
