import { world, system, BlockPermutation, ItemStack, Player } from "@minecraft/server";
import * as defaultWorldArrays from "../arrays/default_world_arrays.js"
import * as blockArrays from "../arrays/block_arrays.js"
import * as dragonArrays from "../arrays/dragon_arrays.js"
import * as itemArrays from "../arrays/item_arrays.js"
import * as dragonUtilities from "../utilities/dragon_utilities.js"
import * as itemUtilities from "../utilities/item_utilities.js"

system.runInterval(() => {
	for (const dim of defaultWorldArrays.addonDimensions) {
		const entities = world.getDimension(dim).getEntities({type:"megaverse:alchemic_book"});
		if (!entities.isValid) return;
		for (const alchemicBook of entities) {
			const blockBelow = alchemicBook.dimension.getBlock({x:alchemicBook.location.x,y:alchemicBook.location.y-0.5,z:alchemicBook.location.z});
			const isAboveAltar = alchemicBook.getProperty("megaverse:is_above_altar");
			if (blockBelow.typeId=="megaverse:alchemy_altar") {
				const canBeUsed = blockBelow.permutation.getState("megaverse:can_be_used");
				if (canBeUsed&&!isAboveAltar) alchemicBook.setProperty("megaverse:is_above_altar",true);
				else if (!canBeUsed&&isAboveAltar) alchemicBook.setProperty("megaverse:is_above_altar",false);
			}
			else if (blockBelow.typeId!="megaverse:alchemy_altar"&&isAboveAltar) alchemicBook.setProperty("megaverse:is_above_altar",false);
		}
	}
},0);

world.afterEvents.dataDrivenEntityTrigger.subscribe(({entity,eventId}) => {
	if (!entity.isValid) return;
	if (entity.typeId=="megaverse:silkworm"&&eventId=="minecraft:entity_transformed") {
		const blockLocate = entity.dimension.getBlock(entity.location);
		const blockBelow = entity.dimension.getBlock({x:entity.location.x,y:entity.location.y-1,z:entity.location.z});
		if ((blockLocate.isAir||(blockLocate.typeId=="minecraft:flowing_water"||blockLocate.typeId=="minecraft:water"))&&!blockBelow.isAir&&!blockArrays.silkwormCocoonDenyBlocks.includes(blockBelow.typeId)) {
			blockLocate.dimension.spawnParticle("megaverse:frost",blockLocate.center());
			blockLocate.setPermutation(BlockPermutation.resolve("megaverse:silkworm_cocoon",{"minecraft:block_face":"up"}));
			entity.triggerEvent("minecraft:despawned");
		}
	}
});
world.afterEvents.dataDrivenEntityTrigger.subscribe(({entity,eventId}) => {
	if (!entity.isValid) return;
	if (entity.typeId!="megaverse:alchemic_book") return;
	const alchemyBook = entity.getProperty("megaverse:alchemic_book");
	const bookMap = {
		arcane: "megaverse:arcane_alchemic_book",
		aurora: "megaverse:aurora_alchemic_book",
		dark: "megaverse:dark_alchemic_book",
		electric: "megaverse:electric_alchemic_book",
		essentia: "megaverse:essentia_alchemic_book",
		fire: "megaverse:fire_alchemic_book",
		geo: "megaverse:geo_alchemic_book",
		ice: "megaverse:ice_alchemic_book",
		light: "megaverse:light_alchemic_book",
		necromancy: "megaverse:necromancy_alchemic_book",
		restoration: "megaverse:restoration_alchemic_book",
		soul: "megaverse:soul_alchemic_book",
		stellar: "megaverse:stellar_alchemic_book",
		water: "megaverse:water_alchemic_book"
	};
	let dropItem = bookMap[alchemyBook]??"megaverse:alchemic_book";
	if (eventId=="minecraft:on_take") {
		entity.dimension.spawnItem(new ItemStack(dropItem, 1),entity.location);
		entity.triggerEvent("minecraft:on_remove");
	}
	if (eventId=="minecraft:on_drop") {
		entity.dimension.spawnItem(new ItemStack("megaverse:alchemic_book", 1),entity.location);
		entity.triggerEvent("minecraft:on_remove");
	}
	if (eventId=="minecraft:on_remove") {
		entity.remove();
	}
});
world.afterEvents.dataDrivenEntityTrigger.subscribe(({entity,eventId}) => {
	const projectiles = itemArrays.projectileTypesList.includes(entity.typeId);
	if (!projectiles) return;
	const rule = itemArrays.projectileTypes[entity.typeId];
	if (!rule) return;
	if (eventId==rule.dropItemEvent) {
		if (rule.dropItem) entity.dimension.spawnItem(new ItemStack(rule.dropItem, 1),entity.location);
		else entity.dimension.spawnItem(new ItemStack(entity.typeId, 1),entity.location);
		entity.triggerEvent(rule.removeEntityEvent);
	}
	if (eventId==rule.removeEntityEvent) {
		entity.remove();
	}
});
/*world.afterEvents.entityHurt.subscribe(({damage,damageSource,hurtEntity}) => {
	if (hurtEntity.typeId!="megaverse:target_dummy") return;
	const damager = damageSource.damagingEntity;
	if (damager instanceof Player) {
		damager.onScreenDisplay.setActionBar({rawtext:[{translate:`${Math.round(damage)}`}]});
	}//For future use when dummies are added.
});*/
world.afterEvents.dataDrivenEntityTrigger.subscribe(({entity,eventId}) => {
	if (!entity.isValid) return;
	if (entity.typeId!="megaverse:falling_ash_block") return;
	const centerBlock = entity.dimension.getBlock(entity.location);
	if (eventId=="minecraft:falling_block_on_ground") {
		if (centerBlock.isAir) {
			entity.setProperty("megaverse:falling_block_on_ground",true);
			centerBlock.setType("megaverse:ash_block");
		}
		if (centerBlock.isLiquid||centerBlock.isWaterlogged) {
			entity.dimension.playSound("break.sand",entity.location);
			entity.setProperty("megaverse:falling_block_on_ground",true);
		}
		else {
			entity.dimension.playSound("break.sand",entity.location);
			entity.setProperty("megaverse:falling_block_on_ground",true);
		}
	}
});

