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

export default {
	name: "pale_jane",
	cookTime: cookTimeTotalMini,
	requiredTemp: "3",
	isValid: (inv, tempSlot) => {
		let sugarSlot = -1, lilySlot = -1, honeySlot = -1, barkSlot = -1;
		let filled = 0;
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item) {
				filled++;
				if (item.typeId === "minecraft:sugar" && sugarSlot === -1) sugarSlot = slot;
				else if (item.typeId === "minecraft:lily_of_the_valley" && lilySlot === -1) lilySlot = slot;
				else if (item.typeId === "minecraft:honey_bottle" && honeySlot === -1) honeySlot = slot;
				else if (item.typeId === "farmersdelight:tree_bark" && barkSlot === -1) barkSlot = slot;
			}
		}
		const riceWine = inv.getItem(liquidSlot);
		const tankard = inv.getItem(slotGen);
		const result = inv.getItem(resultSlot);
		return sugarSlot !== -1 && lilySlot !== -1 && honeySlot !== -1 && barkSlot !== -1 &&
			   filled === 4 &&
			   riceWine && riceWine.typeId === "brewinandchewin:rice_wine" && riceWine.amount >= 1 &&
			   tankard && tankard.typeId === "brewinandchewin:tankard" && tankard.amount >= 1 &&
			   (!result || result.typeId === "brewinandchewin:pale_jane") &&
			   tempSlot && tempSlot.nameTag === "3";
	},
	onComplete: (inv, keg, overworld) => {
		let sugarSlot = -1, lilySlot = -1, honeySlot = -1, barkSlot = -1;
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item && item.typeId === "minecraft:sugar") sugarSlot = slot;
			if (item && item.typeId === "minecraft:lily_of_the_valley") lilySlot = slot;
			if (item && item.typeId === "minecraft:honey_bottle") honeySlot = slot;
			if (item && item.typeId === "farmersdelight:tree_bark") barkSlot = slot;
		}
		if (sugarSlot !== -1) consumeItem(inv, sugarSlot, 1);
		if (lilySlot !== -1) consumeItem(inv, lilySlot, 1);
		if (honeySlot !== -1) consumeItem(inv, honeySlot, 1);
		if (barkSlot !== -1) consumeItem(inv, barkSlot, 1);
		
		const riceWine = inv.getItem(liquidSlot);
		if (riceWine) consumeItem(inv, liquidSlot, 1);
		
		const tankard = inv.getItem(slotGen);
		if (tankard) consumeItem(inv, slotGen, 1);
		
		addResult(inv, "brewinandchewin:pale_jane", 1);
		returnTaraToBuffer(inv, "brewinandchewin:tankard", 1);
		returnTaraToBuffer(inv, "minecraft:glass_bottle", 1);
	}
};
