// priority: 8
NetworkEvents.dataReceived('curios_effect', event => {
    let player = event.player;
    if (!player) return;

    // 使用工具函数获取饰品效果数据
    let curiosEffects = get_all_curios_effects(player);
    if (!curiosEffects || curiosEffects.length === 0) return;

    // 遍历所有饰品效果，执行对应策略
    curiosEffects.forEach(effect => {
        let strategy = curios_tetra_keybind_Strategies[effect.name] || curios_tetra_keybind_Strategies.default;
        strategy(event, player, effect.level, effect.name, effect.efficiency);
    });

});

const curios_tetra_keybind_Strategies = {
    // 献祭
    'bloodthirsty_thorns': function (event, player, effectLevel, effectname, efficiency) {
        let maxhealth = player.maxHealth
        let health = player.health
        player.setHealth(Math.max(health - maxhealth * effectLevel / 100,1))

        player.potionEffects.add('minecraft:strength', 20 * 60, efficiency - 1)
    },
    // 过载
    'overload': function (event, player, effectLevel, effectname, efficiency) {
        let time = player.persistentData.getInt(effectname) ?? 0
        let CD = Math.abs(player.age - time)
        if (CD < 20 * 60 * 3) return
        player.persistentData.putInt(effectname, player.age)

        player.potionEffects.add('kubejs:overload', 20 * 60, 0)
    },
    // 台阶高度
    'tai_jie_gao_du': function (event, player, effectLevel, effectname, efficiency) {
        let isEnabled = player.persistentData.getInt(effectname) ?? 0;
        // 清除当前加成
        player.modifyAttribute('forge:step_height_addition', effectname, 0, 'addition');

        // 切换开关状态
        isEnabled = isEnabled === 1 ? 0 : 1;
        player.persistentData.putInt(effectname, isEnabled);

        // 根据开关状态设置加成
        if (isEnabled === 1) {
            player.modifyAttribute('forge:step_height_addition', effectname, effectLevel / 5, 'addition');
            player.tell('台阶高度已开启');
        } else {
            player.tell('台阶高度已关闭');
        }
    },
    //原初温床联动效果
    'primordial_cradle': function (event, player, effectLevel, effectname, efficiency) {
        let count = player.persistentData.getInt(YingYangCount)

        let time = player.persistentData.getInt(effectname) ?? 0
        let CD = Math.abs(player.age - time)
        if (CD < count / 10) return
        player.persistentData.putInt(effectname, player.age)

        if (arrmor_total_effect_level(player, 'living').level >= 4) {
            player.potionEffects.add('kubejs:living', 2 * count, count/100 - 1)
            player.getServer().scheduleInTicks(2 * count, () => {
                updateYingYangCount(player, 0)
            })
        }
    },
    //配种兴奋剂
    'breeding_stimulant': function (event, player, effectLevel, effectname, efficiency) {
        if (getPlayerCuriosEffectData(player, 'bio_lab').level == 0) {
            let time = player.persistentData.getInt(effectname) ?? 0;
            if (player.age - time < 20 * 60) return;
        }

        //获取营养值
        let count = player.persistentData.getInt(YingYangCount)
        if (count < 2) return
        // 获取周围实体
        let entityList = getLivingWithinRadius(player.getLevel(), player.position(), 3)
        // 遍历实体列表
        entityList.forEach(entity => {
            if (entity.isLiving()) {
                entity.potionEffects.add('biomancy:libido', 20 * 60, 0)
            }
        })
        //减少营养值
        updateYingYangCount(player, count - 2)

        //彩蛋
        let random = Math.random()
        if (random < 0.05 && count > 52) {
            //创建实体
            let spawn_entity = player.level.createEntity('biomancy:primordial_hungry_flesh_blob')
            spawn_entity.setPosition(player.x, player.y, player.z)
            //生成实体
            spawn_entity.spawn()
            player.tell('伟大的母亲注视了你，你诞生了一个子嗣')
            updateYingYangCount(player, count - 50)
        }

        player.persistentData.putInt(effectname, player.age)
    },
    // 滕炎锭
    'ignitium_ingot': function (event, player, effectLevel, effectname, efficiency) {
        // return
        let level = player.level;

        let time = player.persistentData.getInt(effectname) ?? 0
        let CD = Math.abs(player.age - time)
        if (CD < 20 * 60) return
        player.persistentData.putInt(effectname, player.age)

        let viewVector = player.getViewVector(1.0);
        let normalizedVector = normalizeVector(viewVector);

        for (let i = 1; i <= 5; i++) {
            let entity_1 = level.createEntity("cataclysm:flame_strike");
            entity_1.setPosition(
                player.x + 3 * i * normalizedVector.x,
                player.y,
                player.z + 3 * i * normalizedVector.z
            );
            entity_1.setOwner(player);
            entity_1.mergeNbt({ Duration: 1 });
            entity_1.spawn();
        }
    },
    //狂暴血清
    'frenzy_serum': function (event, player, effectLevel, effectname, efficiency) {
        let count = player.persistentData.getInt(YingYangCount)
        if (getPlayerCuriosEffectData(player, 'bio_lab').level == 0) {
            let time = player.persistentData.getInt(effectname) ?? 0
            if (player.age - time < 20 * 60) return;
        } 
        if (player.hasEffect('biomancy:withdrawal') || player.hasEffect('biomancy:frenzy') || count < 50) return
        let random = Math.random()
        if (random < 0.3) { player.potionEffects.add('biomancy:frenzy', 20 * 60, 0) }
        if (random < 0.6 && random > 0.3) { player.potionEffects.add('biomancy:frenzy', 20 * 60, 1) }
        if (random > 0.6) { player.potionEffects.add('biomancy:frenzy', 20 * 30, 2) }
        //减少营养值
        updateYingYangCount(player, count - 50)
    },
    'default': function (event, player, effectLevel, effectname, efficiency) {
        return
    }
}