import { Entity, ItemStack } from "@minecraft/server";
import { addDamageItem, setLoreItem, spawnEntityWithOwner } from "../../library/itemManager";

/**
 * 
 * @param {{attackingEntity: Entity, hadEffect: boolean, hitEntity: Entity, itemStack: ItemStack}} event 
 */
export const plasmaDrillHurtEntityEvent = ({attackingEntity, hadEffect, hitEntity, itemStack}) => {
    const state = attackingEntity.getDynamicProperty('nps_ec:drill');
    const currentDamage = itemStack.getComponent('minecraft:durability').damage;
    const maxDurability = itemStack.getComponent('minecraft:durability').maxDurability;
    const durability = maxDurability - currentDamage;
    if(state == undefined) {
        attackingEntity.setDynamicProperty('nps_ec:drill', 0);
        setLoreItem(itemStack, attackingEntity, "§r§9+18", 'Drill in single block mode');
    }

    if(state) {
        setLoreItem(itemStack, attackingEntity, "§r§9Damage area", 'Drill in multiblock mode');
        if(durability > 30) {
            addDamageItem(itemStack, attackingEntity, 12);
            attackingEntity.runCommand("playsound drill.digger @a [r=5] ~ ~ ~ 3.0");
            spawnEntityWithOwner(attackingEntity, 'nps_ec:drill_hurt_entity<nps_ec:owner>');
        } else {
            attackingEntity.runCommand("replaceitem entity @s slot.weapon.mainhand 0 nps_ec:plasma_drill_unloaded 1 1239")
            spawnEntityWithOwner(attackingEntity, 'nps_ec:drill_hurt_entity<nps_ec:owner>');
        }
    } else {
        setLoreItem(itemStack, attackingEntity, "§r§9+18", 'Drill in single block mode');
        if(durability > 10) {
            addDamageItem(itemStack, attackingEntity, 1);
            hitEntity.applyDamage(18);
            attackingEntity.runCommand("playsound drill.digger @a [r=5] ~ ~ ~ 3.0");
        } else {
            hitEntity.applyDamage(18);
            attackingEntity.runCommand("replaceitem entity @s slot.weapon.mainhand 0 nps_ec:plasma_drill_unloaded 1 1239")
        }
    }
}