import { world, system, EntityDamageCause, EntityInventoryComponent, EquipmentSlot, GameMode, ItemStack, Player } from "@minecraft/server";
import * as itemArrays from "../arrays/item_arrays.js"

export function cookedMeatConsume(itemStack,source,params) {
	const rule = params;
	if (rule.chance_effect) {
		const percentage = rule.chance_effect.numerator/rule.chance_effect.denominator;
		if (Math.random()>percentage) return;
	}
	if (rule.add_effect) source.addEffect(rule.add_effect.type,rule.add_effect.duration*20);
};
export function itemBottleConsume(itemStack,source) {
	if (source.getGameMode()==GameMode.Creative) return;
	const rule = itemStack.getComponent("megaverse:consume_item_bottle").customComponentParameters.params;
	if (!rule) return;
	if (rule.apply_damage) source.applyDamage(rule.apply_damage.damage,{cause:EntityDamageCause[rule.apply_damage.cause]});
	if (rule.add_effect) source.addEffect(rule.add_effect.type,rule.add_effect.duration*20);
	if (rule.particle) source.dimension.spawnParticle(rule.particle,{x:source.location.x,y:source.location.y+1,z:source.location.z});
	if (rule.sound) source.playSound(rule.sound);
	const health = source.getComponent("health");
	let interval;
	function looper() {
		if (health.currentValue==0||health.currentValue==health.effectiveMax||source.getGameMode()==GameMode.Creative) return system.clearRun(interval);
		system.runTimeout(() => {
			if (health.currentValue!=0&&health.currentValue!=health.effectiveMax&&source.getGameMode()!=GameMode.Creative) {
				if (rule.add_after_effect) source.addEffect(rule.add_after_effect.type,rule.add_after_effect.duration*20);
			}
			return system.clearRun(interval);
		},30);
		if (rule.apply_damage) source.applyDamage(rule.apply_damage.damage,{cause:EntityDamageCause[rule.apply_damage.cause]});
		if (rule.particle) source.dimension.spawnParticle(rule.particle,{x:source.location.x,y:source.location.y+1,z:source.location.z});
		if (rule.sound) source.playSound(rule.sound);
	};
	interval = system.runInterval(looper,10);
};
export function itemDecrementStack(itemStack,player) {
	if (player.getGameMode()==GameMode.Creative) return;
	const mainhand = player.getComponent("minecraft:equippable").getEquipmentSlot(EquipmentSlot.Mainhand);
	if (itemStack.amount>1) mainhand.amount--;
	else mainhand.setItem(undefined);
};
export function itemToolDamage(itemStack,player) {
	const mainhand = player.getComponent("minecraft:equippable").getEquipmentSlot(EquipmentSlot.Mainhand);
	const durability = itemStack.getComponent("minecraft:durability");
	const enchantable = itemStack.getComponent("minecraft:enchantable");
	if (!durability||!enchantable) return;
	if (player.getGameMode()==GameMode.Creative) return;
	const unbreaking = enchantable?.getEnchantment("unbreaking")?.level;
	const damageChance = 1/(unbreaking+1);
	if (Math.random()>damageChance) return;
	if (durability.damage>=durability.maxDurability) {
		mainhand.setItem(undefined);
		player.playSound("random.break");
	}
	else {
		durability.damage++;
		mainhand.setItem(itemStack);
	}
};
export function itemWeaponAbilityHitEntity(attackingEntity,hadEffect,hitEntity,params) {
	const rule = params;
	if (rule.unblocks_shield) {
		if (hitEntity.typeId!="minecraft:player") return;
		const equippable = hitEntity.getComponent("minecraft:equippable");
		const mainhand = equippable.getEquipment("Mainhand");
		const offhand = equippable.getEquipment("Offhand");
		if (!hitEntity.isSneaking||hitEntity.getItemCooldown("shield")||(!mainhand||mainhand.typeId!="minecraft:shield")&&(!offhand||offhand.typeId!="minecraft:shield")) {
			return;
		}
		hitEntity.startItemCooldown(rule.unblocks_shield.cooldown,rule.unblocks_shield.duration*20);
		system.run(() => {
			hitEntity.dimension.playSound("random.break",hitEntity.location);
		});
	}
};
export function alchemyWandUse(itemStack,source) {
	const rule = itemStack.getComponent("megaverse:use_alchemy_wand").customComponentParameters.params;
	if (!rule||!rule.sounds||!rule.magic_spells) return;
	source.playSound(rule.sounds.casting,{location:source.location});
	itemStack.setLore(['terramagia.projectile_type.title','terramagia.projectile_type.sphere','terramagia.magic_type.title','terramagia.magic_type.none']);
	let projectileEntity = source.dimension.spawnEntity(rule.magic_spells.spell_type,{
		x: source.location.x+(source.getViewDirection().x==0),
		y: source.location.y+1.4+(source.getViewDirection().y==0),
		z: source.location.z+(source.getViewDirection().z==0)
	},{spawnEvent:rule.magic_spells.spell_duration});
	projectileEntity.triggerEvent(rule.magic_spells.cast_type);
	let projectileType = projectileEntity.getComponent("minecraft:projectile");
	projectileType.owner = source;
	projectileType.shoot({
		x: source.getViewDirection().x*rule.magic_spells.spell_speed,
		y: source.getViewDirection().y*rule.magic_spells.spell_speed,
		z: source.getViewDirection().z*rule.magic_spells.spell_speed
	});
	return projectileEntity;
};
export function rawMeatConsume(itemStack,source,params) {
	const rule = params;
	if (rule.chance_effect) {
		const percentage = rule.chance_effect.numerator/rule.chance_effect.denominator;
		if (Math.random()>percentage) return;
	}
	if (rule.add_effect) source.addEffect(rule.add_effect.type,rule.add_effect.duration*20);
};
export function windInstrumentUse(itemStack,source) {
	const rule = itemStack.getComponent("megaverse:use_wind_instrument").customComponentParameters.params;
	if (!rule) return;
	if (rule.cooldown) source.startItemCooldown(rule.cooldown.type,rule.cooldown.duration*20);
	if (rule.sounds) {
		system.run(() => {
			source.dimension.playSound(rule.sounds.default,source.location);
		});
	}
	if (!rule.shoot_projectile) return;
	let projectile = source.dimension.spawnEntity(rule.shoot_projectile.type, {
		x: source.location.x+(source.getViewDirection().x==0),
		y: source.location.y+1.4+(source.getViewDirection().y==0),
		z: source.location.z+(source.getViewDirection().z==0)
	});
	let callComponent = projectile.getComponent("minecraft:projectile");
	callComponent.owner = source;
	callComponent.shoot({
		x: source.getViewDirection().x*rule.shoot_projectile.speed,
		y: source.getViewDirection().y*rule.shoot_projectile.speed,
		z: source.getViewDirection().z*rule.shoot_projectile.speed
	});
	return projectile;
};