// priority: 8
//参考脆骨症的代码
function CuriosTetraPlayerAttack(event) {
    let player = event.source.player;
    if (!player) return;

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

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

const CuriosTetraPlayerAttackStrategies = {
    //先驱元件
    'harbinger_component': function (event, player, effectLevel, effectname, efficiency) {
        let gear_shift = get_gear_shift(player)
        let tetra_wear_increased_damag = player.persistentData.getDouble('tetra_wear_increased_damag') ?? 0
        player.persistentData.putDouble('tetra_wear_increased_damag', tetra_wear_increased_damag + gear_shift * effectLevel / 10000)
    },
    //深渊之卵
    'abyssal_egg': function (event, player, effectLevel, effectname, efficiency) {
        //如果玩家在飞行
        if (player.abilities.flying) {
            event.amount = event.amount * 0.1
        }
    },
    //不稳定化合物
    'unstable_compound': function (event, player, effectLevel, effectname, efficiency) {
        let check_name = 'bio_lab'
        if (getPlayerCuriosEffectData(player, check_name).level > 0) {
            let time = player.persistentData.getInt(effectname) ?? 0;
            if (player.age - time < 20 * 60) return;
        }

        // 获取并检查营养值
        let count = player.persistentData.getInt(YingYangCount) ?? 0;
        if (count < 2) return;

        let entityList = getLivingWithinRadius(entity.getLevel(), entity.position(), 2)
        entityList.forEach(nearbyEntity => {
            if (nearbyEntity.isLiving() && nearbyEntity != player) {
                simpleAttackEntity(true, null, nearbyEntity, 'minecraft:explosion', event.amount * effectLevel / 100)
            }
        })

        // 执行爆炸效果
        updateYingYangCount(player, count - 2);
        let explosion = event.entity.block.createExplosion();
        explosion.strength(1);
        explosion.explode();



        player.persistentData.putInt(effectname, player.age);
    },
    //净化血清
    'cleansing_serum': 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 entity = event.entity
        //获取营养值
        let count = player.persistentData.getInt(YingYangCount)
        if (count < 2) return

        let effectsToRemove = [];
        entity.potionEffects.active.forEach(ctx => {
            effectsToRemove.push(ctx.effect);
        });

        // 如果有buff被移除，则减少营养值
        if (effectsToRemove.length > 0) {
            //减少营养值
            updateYingYangCount(player, count - 2)
            // 遍历 effectsToRemove 数组，移除效果
            effectsToRemove.forEach(effect => {
                entity.removeEffect(effect);
            })
        }
        player.persistentData.putInt(effectname, player.age)
    },
    //凋零合金块
    'witherite_block': function (event, player, effectLevel, effectname, efficiency) {
        let time = player.persistentData.getInt(effectname) ?? 0
        let CD = Math.abs(player.age - time)
        if (CD < 10) return
        player.persistentData.putInt(effectname, player.age)

        let entity = event.entity;
        if (entity.hasEffect('minecraft:wither')) {
            let effect = entity.getEffect('minecraft:wither');
            let amplifier = effect.getAmplifier();
            let duration = effect.getDuration();
            entity.potionEffects.add('minecraft:wither', duration, amplifier + 1);
        } else {
            entity.potionEffects.add('minecraft:wither', 20 * 30, 0);
        }
    },
    //风暴瓶
    'sandstorm_in_a_bottle': function (event, player, effectLevel, effectname, efficiency) {
        let time = player.persistentData.getInt(effectname) ?? 0
        let CD = Math.abs(player.age - time)
        if (CD < 10) return
        player.persistentData.putInt(effectname, player.age)

        let type = event.getSource().getType().toString();
        if (type == 'mob') {
            event.entity.invulnerableTime = 0
        } else {
            player.getServer().scheduleInTicks(10, () => {
                let level = player.level
                let viewVector = player.getViewVector(1.0);
                let normalizedVector = normalizeVector(viewVector);
                let spawn_entity = level.createEntity("cataclysm:sandstorm_projectile");
                spawn_entity.setPosition(player.x, player.y + 1, player.z);
                spawn_entity.setMotion(normalizedVector.x, normalizedVector.y, normalizedVector.z);
                spawn_entity.setOwner(player);
                spawn_entity.mergeNbt({ damage: 6 });
                spawn_entity.spawn();
            })
        }
    },
    'default': function (event, player, effectLevel, effectname, efficiency) {
        return
    }
}