// priority: 8
function all_effect_attack(event) {
    let player = event.source.player;
    if (!player) return;

    let effects = get_all_effect(player)
    effects.forEach(effect => {
        let strategy = all_effect_attack_strategies[effect.name] || all_effect_attack_strategies.default;
        strategy(event, player, effect.level, effect.name, effect.efficiency);
    });
}
const all_effect_attack_strategies = {
    //潮湿
    'damp': function (event, player, effectLevel, effectname, efficiency) {
        let entity = event.entity
        entity.potionEffects.add('cataclysm:wetness', 20 * 10, effectLevel - 1)
    },
    //雷暴
    'storm': function (event, player, effectLevel, effectname, efficiency) {
        let entity = event.entity
        simpleAttackEntity(true, null, entity, 'lightning_bolt', effectLevel)
        let spawn_entity = entity.level.createEntity("minecraft:lightning_bolt");
        spawn_entity.setPosition(entity.x, entity.y, entity.z);
        spawn_entity.setVisualOnly(true)
        spawn_entity.spawn();
    },
    //狱火
    'hellfire': function (event, player, effectLevel, effectname, efficiency) {
        simpleAttackEntity(true, null, event.entity, 'kubejs_fire_damage', effectLevel)
    },
    //浮空
    'all_floating': function (event, player, effectLevel, effectname, efficiency) {
        let entity = event.entity
        if (!entity.onGround()) {
            simple_all_increased_damage(player, effectLevel / 100)
        }
    },
    // 遗忆
    'forgotten_ingot': function (event, player, effectLevel, effectname, efficiency) {
        let entity = event.entity
        if (entity.type.split(':')[0] == 'undergarden') {
            let tetra_wear_increased_damag = player.persistentData.getDouble('tetra_wear_increased_damag') ?? 0
            player.persistentData.putDouble('tetra_wear_increased_damag', tetra_wear_increased_damag + effectLevel / 1000)
        }

    },
    // 对甲专精
    'regalium_crystal': function (event, player, effectLevel, effectname, efficiency) {
        let entity = event.entity
        let armor = entity.getAttributeTotalValue('minecraft:generic.armor');

        let tetra_wear_increased_damag = player.persistentData.getDouble('tetra_wear_increased_damag') ?? 0
        player.persistentData.putDouble('tetra_wear_increased_damag', tetra_wear_increased_damag + armor * effectLevel / 1000)

    },
    // 铸币
    'snout': function (event, player, effectLevel, effectname, efficiency) {
        let type = event.getSource().getType().toString();
        let item = player.getMainHandItem()
        if (item != 'minecraft:golden_sword' && item != 'minecraft:golden_axe') return
        if (type == 'player') {
            event.entity.invulnerableTime = event.entity.invulnerableTime * (1 - effectLevel / 100)
        }
    },
    
    'arrow_amplification': function (event, player, effectLevel, effectname, efficiency) {
        let type = event.getSource().getType().toString();
        if (type == 'arrow') {
            let tetra_wear_increased_damag = player.persistentData.getDouble('tetra_wear_increased_damag') ?? 0
            player.persistentData.putDouble('tetra_wear_increased_damag', tetra_wear_increased_damag + effectLevel / 100)
        }
    },
    // 驱役恼鬼
    'vex_armor_trim_smithing_template': function (event, player, effectLevel, effectname, efficiency) {
        let time = player.persistentData.getInt(effectname) ?? 0
        let CD = Math.abs(player.age - time)
        if (CD < 20 * 30) return
        player.persistentData.putInt(effectname, player.age)

        for (let i = 1; i <= effectLevel; i++) {
            let spawn_entity = player.level.createEntity("minecraft:vex")
            spawn_entity.setPosition(player.x, player.y, player.z);
            //设置主人防止攻击玩家
            spawn_entity.persistentData.putString('owner', player.getUuid())
            //给予驱役buff
            spawn_entity.potionEffects.add('kubejs:enslavement', 20 * 60 * 10, 0)
            spawn_entity.setLastHurtByMob(event.entity)
            spawn_entity.spawn();
            if (player.getMainHandItem().item instanceof $ModularItem) {
                let item = player.getMainHandItem()
                let new_item = Item.of(item.item.id)
                new_item.getOrCreateTag().merge(item.nbt)
                new_item.nbt.remove('id')
                updateItemEffects(new_item)
                spawn_entity.setMainHandItem(new_item)
            }
        }
    },
    // 雷霆
    'thunder': function (event, player, effectLevel, effectname, efficiency) {
        let type = event.getSource().getType().toString();
        if (type == 'lightningBolt') {
            let tetra_wear_increased_damag = player.persistentData.getDouble('tetra_wear_increased_damag') ?? 0
            player.persistentData.putDouble('tetra_wear_increased_damag', tetra_wear_increased_damag + effectLevel / 100)
        }
    },
    //脉冲风行者水晶
    'pulse_windrunner_crystal': function (event, player, effectLevel, effectname, efficiency) {
        //如果玩家在飞行
        if (player.abilities.flying) {
            event.amount = event.amount * 0.1
        }
    },
    //风行者水晶
    'windrunner_crystal': function (event, player, effectLevel, effectname, efficiency) {
        //如果玩家在飞行
        if (player.abilities.flying) {
            event.amount = event.amount * 0.1
        }
    },
    //噬咬
    'devouring_bites': function (event, player, effectLevel, effectname, efficiency) {
        let curYingYangCount = player.persistentData.getInt(YingYangCount);
        updateYingYangCount(player, effectLevel + curYingYangCount)
    },
    //伤害增加
    'damage_increased': function (event, player, effectLevel, effectname, efficiency) {
        event.amount = event.amount * (1 + effectLevel / 100)
    },
    //飞行
    'fei_xing': function (event, player, effectLevel, effectname, efficiency) {
        //如果玩家在飞行
        if (player.abilities.flying) {
            event.amount = event.amount * 0.1
        }
    },
    //火焰增幅
    'fire_increase': function (event, player, effectLevel, effectname, efficiency) {
        let type = event.getSource().getType().toString();
        if (type == 'kubejs_fire_damage') {
            event.amount = event.amount * (1 + effectLevel / 100)
        }
    },
    'default': function (event, player, effectLevel, effectname, efficiency) {
        return;
    }
};