import { ItemStack, world } from '@minecraft/server';
import { COOKING_RECIPES } from './Recipes';

export class McfMicrowave {
	static getMicrowaveData(entity) {
		return {
			progress: entity.getDynamicProperty("mr:microwave_progress") ?? 0,
			total: entity.getDynamicProperty("mr:microwave_total") ?? 0,
			cookingItem: entity.getDynamicProperty("mr:microwave_item") ?? ""
		};
	}
	static setMicrowaveData(entity, data) {
		entity.setDynamicProperty("mr:microwave_progress", data.progress);
		entity.setDynamicProperty("mr:microwave_total", data.total);
		entity.setDynamicProperty("mr:microwave_item", data.cookingItem ?? "");
	}
	static calculateCookingTime(item) {
		return 100 + (50 * (item.amount - 1));
	}
	static getItemKey(item) {
		return `${item.typeId}*/*${item.amount}`;
	}
	static checkItemChanged (entity, inventory, microwaveData, currentItem) {
		const id = microwaveData.cookingItem.split("*/*")[0];
		if (id === currentItem?.typeId) {
			microwaveData.total = this.calculateCookingTime(currentItem);
			microwaveData.cookingItem = this.getItemKey(currentItem);
			this.setMicrowaveData(entity, microwaveData);
		} else return this.stopCooking(entity, inventory, microwaveData);
	}
	static itemChanged(currentItem, storedItemKey) {
		if (!currentItem) return true;
		return this.getItemKey(currentItem) !== storedItemKey;
	}
	static updateProgressDisplay(entity, inventory, microwaveData) {
		const progressVisual = Math.max(1, Math.ceil((microwaveData.progress / microwaveData.total) * 64));
		inventory.setItem(1, new ItemStack("mr:timer", progressVisual));
		this.setMicrowaveData(entity, microwaveData);
	}
	static stopCooking(entity, inventory, microwaveData) {
		microwaveData.progress = 0;
		microwaveData.total = 0;
		microwaveData.cookingItem = undefined;
		this.setMicrowaveData(entity, microwaveData);
		inventory.setItem(1, new ItemStack("mr:timer", 1));
		entity.triggerEvent("mr:stop");
	}
	static startCooking(entity, microwaveData, item) {
		microwaveData.total = this.calculateCookingTime(item);
		microwaveData.progress = 0;
		microwaveData.cookingItem = this.getItemKey(item);
		this.setMicrowaveData(entity, microwaveData);
		entity.triggerEvent("mr:start");
	}
	static completeCooking(entity, inventory, microwaveData, item) {
		const cookedItem = new ItemStack(COOKING_RECIPES[item.typeId], item.amount);
		inventory.setItem(0, cookedItem);
		this.stopCooking(entity, inventory, microwaveData);
	}
	static handleCheckEvent(entity, inventory, microwaveData) {
		const item = inventory.getItem(0);
		if (COOKING_RECIPES[item?.typeId]) return this.startCooking(entity, microwaveData, item);
	}
	static handleUpdateTimerEvent(entity, inventory, microwaveData) {
		const item = inventory.getItem(0);
		if (this.itemChanged(item, microwaveData.cookingItem)) this.checkItemChanged(entity, inventory, microwaveData, item);
		microwaveData.progress++;
		this.updateProgressDisplay(entity, inventory, microwaveData);
		if (microwaveData.progress >= microwaveData.total) return this.completeCooking(entity, inventory, microwaveData, item);
	}
	static registerEvent() {
		world.afterEvents.dataDrivenEntityTrigger.subscribe(({ entity, eventId }) => {
			if (!entity?.isValid) return;
			const inventory = entity.getComponent("inventory").container;
			const microwaveData = this.getMicrowaveData(entity);
			if (eventId === "mr:check") this.handleCheckEvent(entity, inventory, microwaveData);
			if (eventId === "mr:update_timer") this.handleUpdateTimerEvent(entity, inventory, microwaveData);
		}, {
			entityTypes: ["mr:microwave"],
			eventTypes: ["mr:check", "mr:update_timer"]
		})
	}
}