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

export default {
	name: "dread_nog",
	cookTime: cookTimeTotalMini,
	requiredTemp: "1",
	isValid: (inv, tempSlot) => {
		let eyeSlot = -1, turtleEggSlot = -1, ominousSlot = -1;
		let filled = 0;
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item) {
				filled++;
				if (item.typeId === "minecraft:fermented_spider_eye" && eyeSlot === -1) eyeSlot = slot;
				else if (item.typeId === "minecraft:turtle_egg" && turtleEggSlot === -1) turtleEggSlot = slot;
				else if (item.typeId === "minecraft:ominous_bottle" && ominousSlot === -1) ominousSlot = slot;
			}
		}
		const eggGrog = inv.getItem(liquidSlot);
		const tankard = inv.getItem(slotGen);
		const result = inv.getItem(resultSlot);
		return eyeSlot !== -1 && turtleEggSlot !== -1 && ominousSlot !== -1 &&
			   filled === 3 &&
			   eggGrog && eggGrog.typeId === "brewinandchewin:egg_grog" && eggGrog.amount >= 1 &&
			   tankard && tankard.typeId === "brewinandchewin:tankard" && tankard.amount >= 1 &&
			   (!result || result.typeId === "brewinandchewin:dread_nog") &&
			   tempSlot && tempSlot.nameTag === "1";
	},
	onComplete: (inv, keg, overworld) => {
		let eyeSlot = -1, turtleEggSlot = -1, ominousSlot = -1;
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item && item.typeId === "minecraft:fermented_spider_eye") eyeSlot = slot;
			if (item && item.typeId === "minecraft:turtle_egg") turtleEggSlot = slot;
			if (item && item.typeId === "minecraft:ominous_bottle") ominousSlot = slot;
		}
		if (eyeSlot !== -1) consumeItem(inv, eyeSlot, 1);
		if (turtleEggSlot !== -1) consumeItem(inv, turtleEggSlot, 1);
		if (ominousSlot !== -1) consumeItem(inv, ominousSlot, 1);
		
		const eggGrog = inv.getItem(liquidSlot);
		if (eggGrog) consumeItem(inv, liquidSlot, 1);
		
		const tankard = inv.getItem(slotGen);
		if (tankard) consumeItem(inv, slotGen, 1);
		
		addResult(inv, "brewinandchewin:dread_nog", 1);
		returnTaraToBuffer(inv, "brewinandchewin:tankard", 1);
	}
};
