import { ItemStack, EntityHurtAfterEvent } from "@minecraft/server";
import { getScoreUtility, operationScoreUtility } from "./scoreboard";
import { damageItemDurability, applyVarious, isPlayer } from "./function";

/**
 * @param {EntityHurtAfterEvent} ev 
 */
export function entityHurt(ev){
	const damagingEntity = ev.damageSource.damagingEntity;
	const hurtEntity = ev.hurtEntity;
	if(isPlayer(hurtEntity)){
		const hp = hurtEntity.getComponent("minecraft:health");
		if(hp.currentValue <= 0) return;
		const hatsu = hurtEntity.getProperty("hxc:hatsu");
		const damageAmount = ev.damage;
		if(hatsu == 5){
			const pain_score = getScoreUtility(hurtEntity,"pain_score");
			hurtEntity.runCommand("scoreboard players add @s[scores={pain_score=..100}] pain_score 2");
			hurtEntity.runCommand("scoreboard players set @s[scores={pain_score=100..}] pain_score 100");
		}
		else{
			hurtEntity.runCommand("scoreboard players set @s pain_score 0");
		}
		const equipComp = hurtEntity.getComponent("equippable");
		const offhandItemStack = equipComp.getEquipment("Offhand");
		if(offhandItemStack==undefined) return;
	}
	else if(hurtEntity.typeId == "hxc:feitan"){
		hurtEntity.runCommand("scoreboard objectives add pain_score dummy");
		hurtEntity.runCommand("scoreboard players add @s[scores={pain_score=..100}] pain_score 2");
		hurtEntity.runCommand("scoreboard players set @s[scores={pain_score=100..}] pain_score 100");
	}
};