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

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