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

export default {
	name: "bloody_mary",
	cookTime: cookTimeTotalMini,
	requiredTemp: "3",
	isValid: (inv, tempSlot) => {
		let berriesSlot = -1, tomatoSlot = -1, cabbageSlot = -1;
		let filled = 0;
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item) {
				filled++;
				if (item.typeId === "minecraft:sweet_berries" && berriesSlot === -1) berriesSlot = slot;
				else if (item.typeId === "farmersdelight:tomato" && tomatoSlot === -1) tomatoSlot = slot;
				else if ((item.typeId === "farmersdelight:cabbage" || item.typeId === "farmersdelight:cabbage_leaf") && cabbageSlot === -1) cabbageSlot = slot;
			}
		}
		const vodka = inv.getItem(liquidSlot);
		const tankard = inv.getItem(slotGen);
		const result = inv.getItem(resultSlot);
		return berriesSlot !== -1 && tomatoSlot !== -1 && cabbageSlot !== -1 &&
			   filled === 3 &&
			   vodka && vodka.typeId === "brewinandchewin:vodka" && vodka.amount >= 1 &&
			   tankard && tankard.typeId === "brewinandchewin:tankard" && tankard.amount >= 1 &&
			   (!result || result.typeId === "brewinandchewin:bloody_mary") &&
			   tempSlot && tempSlot.nameTag === "3";
	},
	onComplete: (inv, keg, overworld) => {
		let berriesSlot = -1, tomatoSlot = -1, cabbageSlot = -1;
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item && item.typeId === "minecraft:sweet_berries") berriesSlot = slot;
			if (item && item.typeId === "farmersdelight:tomato") tomatoSlot = slot;
			if (item && (item.typeId === "farmersdelight:cabbage" || item.typeId === "farmersdelight:cabbage_leaf")) cabbageSlot = slot;
		}
		if (berriesSlot !== -1) consumeItem(inv, berriesSlot, 1);
		if (tomatoSlot !== -1) consumeItem(inv, tomatoSlot, 1);
		if (cabbageSlot !== -1) consumeItem(inv, cabbageSlot, 1);
		
		const vodka = inv.getItem(liquidSlot);
		if (vodka) consumeItem(inv, liquidSlot, 1);
		
		const tankard = inv.getItem(slotGen);
		if (tankard) consumeItem(inv, slotGen, 1);
		
		addResult(inv, "brewinandchewin:bloody_mary", 1);
		returnTaraToBuffer(inv, "brewinandchewin:tankard", 1);
	}
};
