import { system, world } from '@minecraft/server';

world.afterEvents.entityHitEntity.subscribe(event => {
    const attacker = event.damagingEntity;
    const hitEntity = event.hitEntity;

    if (attacker.typeId !== 'nb:zodd_apostle') {
        return;
    }

    const randomNumber = Math.random();

    if (randomNumber < 0.15) {
        especiall(attacker, hitEntity);

    } else if (randomNumber < 0.35) {
        attacker.runCommandAsync("playanimation @s animation.zoddapostle.horn");
        hitEntity.runCommandAsync("particle nb:varios_cortes ~ ~1.2 ~");

        const knockbackStrengthH = 4;
        const knockbackStrengthV = 0.2;
        const knockbackZoddH = 8;
        const knockbackZoddV = 0;        
        const viewDirection = attacker.getViewDirection();

        hitEntity.applyKnockback(
            viewDirection.x,
            viewDirection.z,
            knockbackStrengthH,
            knockbackStrengthV
        );
        
        attacker.applyKnockback(
            viewDirection.x,
            viewDirection.z,
            knockbackZoddH,
            knockbackZoddV
        );
    } else {
        attacker.runCommandAsync("playanimation @s animation.zoddapostle.attack");
    }
});

/**
 * 
 * @param {import('@minecraft/server').Entity} attacker A entidade que ataca (Zodd).
 * @param {import('@minecraft/server').Entity} target A entidade que é atingida.
 */
function especiall(attacker, target) {
    attacker.runCommandAsync("playsound zoddnever @a[r=20]");
    attacker.addTag("zoddap_esp");
    attacker.runCommandAsync("effect @s resistance 255 255 true");  
    attacker.runCommandAsync("effect @s slowness 255 255 true");        
    attacker.runCommandAsync("playanimation @s animation.zoddapostle.stomp");
    system.runTimeout(() => {
        attacker.runCommandAsync("particle nb:zodd_explosion ~ ~0.8 ~");
    }, 20);    
    system.runTimeout(() => {
        attacker.removeTag("zoddap_esp");
        attacker.runCommandAsync("effect @s clear");

        const applyAreaDamage = (damageAmount, tickDelay) => {
        system.runTimeout(() => {
            if (!attacker.isValid()) return;
            attacker.runCommandAsync(`damage @e[tag=!zoddap_esp,r=5] ${damageAmount} entity_attack entity @s`);
        }, tickDelay);
    };

    applyAreaDamage(18, 1);

        const knockbackStrengthHesp = 0.2;
        const knockbackStrengthVesp = 0.7; 
        const viewDirection = attacker.getViewDirection();

            const affectedEntities = attacker.dimension.getEntities({
            location: attacker.location,
            maxDistance: 8,
            excludeTags: ["zoddap_esp"]
        });

        for (const entity of affectedEntities) {
            entity.applyKnockback(
                viewDirection.x,
                viewDirection.z,
                knockbackStrengthHesp,
                knockbackStrengthVesp
            );
        }

    }, 30);
}

system.runInterval(() => {
    const zoddapostleEntities = world.getDimension("overworld").getEntities({ type: 'nb:zodd_apostle' });

    for (const zoddapostle of zoddapostleEntities) {
        
        if (zoddapostle.hasTag('fase2')) {
            zoddapostle.runCommandAsync("tp @s @s");
        }            
        
        const velocity = zoddapostle.getVelocity();
        const horizontalVelocity = Math.sqrt(velocity.x ** 2 + velocity.z ** 2);

        if (horizontalVelocity > 0.08) {
            zoddapostle.runCommandAsync("playanimation @s[tag=!zoddap_esp,tag=!fase2] animation.zoddapostle.walk");
        } else {
            zoddapostle.runCommandAsync("playanimation @s[tag=!zoddap_esp,tag=!fase2] animation.zoddapostle.idle");
        }
    }
}, 10);