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

export default {
	name: "mead",
	cookTime: cookTimeTotal,
	requiredTemp: "0",
	isValid: (inv, tempSlot) => {
		let wheatSlot = -1, berriesSlot = -1, seedsSlot = -1;
		let filled = 0;
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item) {
				filled++;
				if (item.typeId === "minecraft:wheat" && wheatSlot === -1) wheatSlot = slot;
				else if (item.typeId === "minecraft:sweet_berries" && berriesSlot === -1) berriesSlot = slot;
				else if (item.typeId === "minecraft:wheat_seeds" && seedsSlot === -1) seedsSlot = slot;
			}
		}
		const honey = inv.getItem(liquidSlot);
		const tankard = inv.getItem(slotGen);
		const result = inv.getItem(resultSlot);
		return wheatSlot !== -1 && berriesSlot !== -1 && seedsSlot !== -1 &&
			   filled === 3 &&
			   honey && honey.typeId === "minecraft:honey_bottle" && honey.amount >= 1 &&
			   tankard && tankard.typeId === "brewinandchewin:tankard" && tankard.amount >= 1 &&
			   (!result || result.typeId === "brewinandchewin:mead") &&
			   tempSlot && tempSlot.nameTag === "0";
	},
	onComplete: (inv, keg, overworld) => {
		let wheatSlot = -1, berriesSlot = -1, seedsSlot = -1;
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item && item.typeId === "minecraft:wheat") wheatSlot = slot;
			if (item && item.typeId === "minecraft:sweet_berries") berriesSlot = slot;
			if (item && item.typeId === "minecraft:wheat_seeds") seedsSlot = slot;
		}
		if (wheatSlot !== -1) consumeItem(inv, wheatSlot, 1);
		if (berriesSlot !== -1) consumeItem(inv, berriesSlot, 1);
		if (seedsSlot !== -1) consumeItem(inv, seedsSlot, 1);
		
		const honey = inv.getItem(liquidSlot);
		if (honey) {
			consumeItem(inv, liquidSlot, 1);
			returnTaraToBuffer(inv, "minecraft:glass_bottle", 1);
		}
		const tankard = inv.getItem(slotGen);
		if (tankard) consumeItem(inv, slotGen, 1);
		addResult(inv, "brewinandchewin:mead", 1);
	}
};
