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

export default {
	name: "strongroot_ale",
	cookTime: cookTimeTotalMini,
	requiredTemp: "0",
	isValid: (inv, tempSlot) => {
		let potatoSlot = -1, mushroomSlot = -1, beetrootSlot = -1, jerkySlot = -1;
		let filled = 0;
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item) {
				filled++;
				if (item.typeId === "minecraft:potato" && potatoSlot === -1) potatoSlot = slot;
				else if (item.typeId === "minecraft:brown_mushroom" && mushroomSlot === -1) mushroomSlot = slot;
				else if (item.typeId === "minecraft:beetroot" && beetrootSlot === -1) beetrootSlot = slot;
				else if (item.typeId === "brewinandchewin:jerky" && jerkySlot === -1) jerkySlot = slot;
			}
		}
		const beer = inv.getItem(liquidSlot);
		const tankard = inv.getItem(slotGen);
		const result = inv.getItem(resultSlot);
		return potatoSlot !== -1 && mushroomSlot !== -1 && beetrootSlot !== -1 && jerkySlot !== -1 &&
			   filled === 4 &&
			   beer && beer.typeId === "brewinandchewin:beer" && beer.amount >= 1 &&
			   tankard && tankard.typeId === "brewinandchewin:tankard" && tankard.amount >= 1 &&
			   (!result || result.typeId === "brewinandchewin:strongroot_ale") &&
			   tempSlot && tempSlot.nameTag === "0";
	},
	onComplete: (inv, keg, overworld) => {
		let potatoSlot = -1, mushroomSlot = -1, beetrootSlot = -1, jerkySlot = -1;
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item && item.typeId === "minecraft:potato") potatoSlot = slot;
			if (item && item.typeId === "minecraft:brown_mushroom") mushroomSlot = slot;
			if (item && item.typeId === "minecraft:beetroot") beetrootSlot = slot;
			if (item && item.typeId === "brewinandchewin:jerky") jerkySlot = slot;
		}
		if (potatoSlot !== -1) consumeItem(inv, potatoSlot, 1);
		if (mushroomSlot !== -1) consumeItem(inv, mushroomSlot, 1);
		if (beetrootSlot !== -1) consumeItem(inv, beetrootSlot, 1);
		if (jerkySlot !== -1) consumeItem(inv, jerkySlot, 1);
		
		const beer = inv.getItem(liquidSlot);
		if (beer) consumeItem(inv, liquidSlot, 1);
		
		const tankard = inv.getItem(slotGen);
		if (tankard) consumeItem(inv, slotGen, 1);
		
		addResult(inv, "brewinandchewin:strongroot_ale", 1);
		returnTaraToBuffer(inv, "brewinandchewin:tankard", 1);
	}
};
