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

export default {
	name: "cheese",
	cookTime: cookTimeTotal,
	requiredTemp: "3",
	isValid: (inv, tempSlot) => {
		let sugarSlot = -1, mushroomSlot = -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:sugar" && sugarSlot === -1) sugarSlot = slot;
				else if (item.typeId === "minecraft:brown_mushroom" && mushroomSlot === -1) mushroomSlot = slot;
				else if (item.typeId === "minecraft:pumpkin_seeds" && seedsSlot === -1) seedsSlot = slot;
			}
		}
		const milk = inv.getItem(liquidSlot);
		const honeycomb = inv.getItem(slotGen);
		const result = inv.getItem(resultSlot);
		const milkValid = (milk && milk.typeId === "minecraft:milk_bucket" && milk.amount >= 1) ||
						 (milk && milk.typeId === "farmersdelight:milk_bottle" && milk.amount >= 4);
		return sugarSlot !== -1 && mushroomSlot !== -1 && seedsSlot !== -1 &&
			   filled === 3 &&
			   milkValid &&
			   honeycomb && honeycomb.typeId === "minecraft:honeycomb" &&
			   (!result || result.typeId === "brewinandchewin:unripe_flaxen_cheese_wheel") &&
			   tempSlot && tempSlot.nameTag === "3";
	},
	onComplete: (inv, keg, overworld) => {
		let sugarSlot = -1, mushroomSlot = -1, seedsSlot = -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:brown_mushroom") mushroomSlot = slot;
			if (item && item.typeId === "minecraft:pumpkin_seeds") seedsSlot = slot;
		}
		if (sugarSlot !== -1) consumeItem(inv, sugarSlot, 1);
		if (mushroomSlot !== -1) consumeItem(inv, mushroomSlot, 1);
		if (seedsSlot !== -1) consumeItem(inv, seedsSlot, 1);
		
		const milk = inv.getItem(liquidSlot);
		if (milk && milk.typeId === "minecraft:milk_bucket") {
			consumeItem(inv, liquidSlot, 1);
			returnTaraToBuffer(inv, "minecraft:bucket", 1);
		} else if (milk && milk.typeId === "farmersdelight:milk_bottle") {
			consumeItem(inv, liquidSlot, 4);
			returnTaraToBuffer(inv, "minecraft:glass_bottle", 4);
		}
		
		const honeycomb = inv.getItem(slotGen);
		if (honeycomb) consumeItem(inv, slotGen, 1);
		addResult(inv, "brewinandchewin:unripe_flaxen_cheese_wheel", 1);
	}
};
