import {system, world} from "@minecraft/server"

system.runInterval(() => {
    for (const player of world.getPlayers()) {
        // Sneak boost
        if (player.isSneaking) {
            player.runCommand("tag @s add cubc_mo_en_sneaking");
        } else {
            player.runCommand("tag @s remove cubc_mo_en_sneaking");
        }

        // Extra hearts
        let extraHeartsScore = getScore(player, "extra_hearts");
        if (extraHeartsScore > 0) {
            player.addEffect("health_boost", 5000, {"amplifier": extraHeartsScore, "showParticles": false})
        } else {
            player.removeEffect("health_boost")
        }

        // Lifesaver cooldown
        let lifeSaverCooldown = getScore(player, "lifeSaverCD");
        let health = player.getComponent('health').currentValue;
        if (lifeSaverCooldown > 0 && health < 5) {
            player.runCommand(`title @s actionbar Life saver cooldown: §e${Math.ceil(lifeSaverCooldown / 20)}`);
        }
    }
}, 1);

export function getScore(entity, scoreboard) {
    try {
        const objective = world.scoreboard.getObjective(scoreboard);
        if (objective) {
            const score = objective.getScore(entity);
            if (score !== undefined && score > 0) {
                return score;
            }
        }
    } catch {}

    try {
        const cubedObjective = world.scoreboard.getObjective(`cubc_mo_en_${scoreboard}`);
        if (cubedObjective) {
            const cubedScore = cubedObjective.getScore(entity);
            if (cubedScore !== undefined) {
                return cubedScore;
            }
        }
    } catch {}

    return 0;
}


world.afterEvents.entityHurt.subscribe((event) => {
    let damagingEntity = event.damageSource.damagingEntity;
    if (damagingEntity === undefined)
        return;
    try {
        if (event.hurtEntity.typeId === "minecraft:player") {
            // Rage
            event.hurtEntity.runCommand(`effect @s[scores={cubc_mo_en_rage=1..3}] strength 5 1`);
            event.hurtEntity.runCommand(`effect @s[scores={cubc_mo_en_rage=4..6}] strength 6 2`);
            event.hurtEntity.runCommand(`effect @s[scores={cubc_mo_en_rage=7..9}] strength 7 2`);
            event.hurtEntity.runCommand(`effect @s[scores={cubc_mo_en_rage=10..}] strength 8 3`);

            // Life saver
            let lifeSaverScore = getScore(event.hurtEntity, "life_saver");
            let lifeSaverCooldown = getScore(event.hurtEntity, "lifeSaverCD");
            let health = event.hurtEntity.getComponent('health').currentValue;
            if (lifeSaverScore > 0 && lifeSaverCooldown <= 0 && health <= 5) {
                event.hurtEntity.runCommand(`effect @s strength 5 ${lifeSaverScore}`);
                event.hurtEntity.runCommand(`effect @s regeneration 5 ${lifeSaverScore}`);
                event.hurtEntity.runCommand(`effect @s resistance 5 ${lifeSaverScore}`);
                event.hurtEntity.runCommand(`scoreboard players set @s cubc_mo_en_lifeSaverCD 1200`);
            }

            let infernScore = getScore(event.hurtEntity, "inferno");
            if (infernScore > 0) {
                // Get score and repeat it x times depending on the score
                let repeatTimes = Math.ceil(infernScore / 4);
                damagingEntity.runCommand(`scriptevent cubc_mo_en:repeat repeat:${repeatTimes}, delay:30 "/scoreboard players set @s cubc_mo_en_set_on_fire 7`);
            }
        }
    } catch (e) {

    }

});