// priority: 8
PlayerEvents.tick(event => {
    let player = event.player;
    // 每秒执行一次
    if (player.age % 25 != 0 || !player) return;

    let effects = get_all_effect(player);

    effects.forEach(effect => {
        let strategy = all_effect_tick_strategies[effect.name] || all_effect_tick_strategies.default;
        strategy(event, player, effect.level, effect.name, effect.efficiency);
    });
});
const all_effect_tick_strategies = {
    //核电轻而易举啊
    'curios_nuclear_explosion': function (event, player, effectLevel, effectname, efficiency) {
        if (player.age % (25 * 48) != 0) return;
        
        function explosion_tishi(i, player) {
            player.server.scheduleInTicks(200 * i, () => {
                player.tell('§4§l距离核事故爆发！还剩下' + (6 - i) * 10 + '秒钟')
            })
        }

        if (Math.random() < effectLevel / 100) {
            let viewVector = player.getViewVector(1.0);
            let normalizedVector = normalizeVector(viewVector)

            for (let i = 0; i < 6; i++) {
                explosion_tishi(i, player)
            }
            player.level.spawnParticles(
                'hp_dzzl:nuclear_explosion_png_show',
                true,
                player.x + normalizedVector.x * 10,
                player.y + normalizedVector.y * 10,
                player.z + normalizedVector.z * 10,
                1, 1, 1, 1, 1
            )
            player.server.scheduleInTicks(200 * 6, () => {
                let spawn_entity = player.level.createEntity("alexscaves:nuclear_explosion")
                spawn_entity.setSize(0)
                spawn_entity.setPosition(player.x, player.y, player.z);
                spawn_entity.spawn();
            })
        }
    },
    //治疗灌注
    'regeneration': function (event, player, effectLevel, effectname, efficiency) {
        if (get_specific_effect(player, 'healing_additive').level > 0) {
            effectLevel += 1
        }
        player.potionEffects.add('minecraft:regeneration', 20 * 6, effectLevel - 1);
    },
    //深渊触手
    'abyssal_tentacles': function (event, player, effectLevel, effectname, efficiency) {
        let level = player.level
        let count = player.persistentData.getInt(YingYangCount)

        let tentacleCap = $ModCapabilities.getCapability(player, $ModCapabilities.TENTACLE_CAPABILITY);
        if (tentacleCap && tentacleCap.hasTentacle()) {
            // 已有活跃触手，直接返回，不召唤新的
            return;
        }
        // 获取周围实体
        let entityList = getLivingWithinRadius(player.getLevel(), player.position(), count / 50)
        for (let entity of entityList) {
            if (entity.isLiving() && !entity.isPlayer() && entity.isMonster()) {
                $TidalTentacleUtil.retractFarTentacles(level, player);

                let tentacleEntity = $ModEntities.TIDAL_TENTACLE.get().create(level);

                tentacleEntity.copyPosition(player); // 触手起点设为玩家位置
                tentacleEntity.setCreatorEntityUUID(player.getUuid()); // 绑定发射者UUID
                tentacleEntity.setFromEntityID(player.id); // 发射者的实体ID
                tentacleEntity.setToEntityID(entity.id); // 目标实体的ID
                tentacleEntity.setProgress(0.0); // 初始化伸展进度（0=未伸展）

                tentacleEntity.persistentData.putInt('null_attack_addition', player.getMaxHealth() * effectLevel / 100);

                // 将触手实体添加到世界
                level.addFreshEntity(tentacleEntity);

                // 记录最近发射的触手
                $TidalTentacleUtil.setLastTentacle(player, tentacleEntity);

                break;
            }
        }
    },
    //极寒
    'extremely_cold': function (event, player, effectLevel, effectname, efficiency) {
        // 获取周围实体
        let entityList = getLivingWithinRadius(player.getLevel(), player.position(), 6)
        // 遍历实体列表
        entityList.forEach(entity => {
            if (entity.isLiving() && entity != player) {
                entity.setHealth(entity.health * (1 - effectLevel / 100))
                entity.modifyAttribute('attributeslib:healing_received', 'extremely_cold', -efficiency / 100, 'addition')
            }
        })
    },
    //力量灌注
    'strength_infused': function (event, player, effectLevel, effectname, efficiency) {
        player.potionEffects.add('minecraft:strength', 20 * 6, effectLevel - 1)
    },
    //水下呼吸
    'underwater_breathing': function (event, player, effectLevel, effectname, efficiency) {
        player.potionEffects.add('minecraft:water_breathing', 20 * 6, 0)
    },
    //应力
    'occupation_stress': function (event, player, effectLevel, effectname, efficiency) {
        if (player.hasEffect('kubejs:overload')) return

        if (get_occupation_stress(player) >= get_specific_effect(player, 'stress').level) {
            player.tell('应力过载')
            player.potionEffects.add('minecraft:slowness', 40, 5);
            player.potionEffects.add('minecraft:mining_fatigue', 40, 5);
        }
    },
    //鞘翅飞行
    'elytra': function (event, player, effectLevel, effectname, efficiency) {
        player.potionEffects.add('kubejs:zhou_yi', 20 * 6, 0)
    },
    //飞行
    'fei_xing': function (event, player, effectLevel, effectname, efficiency) {
        player.potionEffects.add('kubejs:fei_xing', 20 * 6, 0)
    },
    'default': function (event, player, effectLevel, effectname, efficiency) {
        return;
    }
};