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

export default {
	name: "scarlet_cheese",
	cookTime: cookTimeTotal,
	requiredTemp: "4",
	isValid: (inv, tempSlot) => {
		let sugarSlot = -1, fungusSlot = -1, wartSlot = -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:crimson_fungus" && fungusSlot === -1) fungusSlot = slot;
				else if (item.typeId === "minecraft:nether_wart" && wartSlot === -1) wartSlot = 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 && fungusSlot !== -1 && wartSlot !== -1 &&
			   filled === 3 &&
			   milkValid &&
			   honeycomb && honeycomb.typeId === "minecraft:honeycomb" &&
			   (!result || result.typeId === "brewinandchewin:unripe_scarlet_cheese_wheel") &&
			   tempSlot && tempSlot.nameTag === "4";
	},
	onComplete: (inv, keg, overworld) => {
		let sugarSlot = -1, fungusSlot = -1, wartSlot = -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:crimson_fungus") fungusSlot = slot;
			if (item && item.typeId === "minecraft:nether_wart") wartSlot = slot;
		}
		if (sugarSlot !== -1) consumeItem(inv, sugarSlot, 1);
		if (fungusSlot !== -1) consumeItem(inv, fungusSlot, 1);
		if (wartSlot !== -1) consumeItem(inv, wartSlot, 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_scarlet_cheese_wheel", 1);
	}
};
