// priority: 8
function tetra_wear_event_attack(event) {
    let player = event.source.actual
    if (!player || !player.isPlayer()) return
    // 获取物品的所有效果
    let effects = get_armor_effects(player)
    effects.forEach(effect => {
        let effectname = effect.name
        if (!effectname) return
        // 查找对应的策略并执行
        let strategy = armor_damage_strategies[effectname] || armor_damage_strategies.default;
        strategy(event, player, effect.level, effectname, effect.efficiency);
    })
}

const armor_damage_strategies = {
    // 灼燃损伤
    'upgrade_templates/ignitium_ingot': function (event, player, effectLevel, effectname, efficiency) {
        let fire_damage_amount = player.getAttributeTotalValue('kubejs:fire_damage')
        let burn_upgrade_templates = event.entity.persistentData.getInt('burn_upgrade_templates')

        let new_burn_upgrade_templates = burn_upgrade_templates + fire_damage_amount * effectLevel/100
        if (new_burn_upgrade_templates > 1000) {
            simpleAttackEntity(true, null, event.entity, 'attributeslib:fire_damage', 800)
            event.entity.persistentData.putInt('burn_upgrade_templates',new_burn_upgrade_templates - 1000)
        }else {
            event.entity.persistentData.putInt('burn_upgrade_templates', new_burn_upgrade_templates)
        }
    },
    // 雷霆
    '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)
        }
    },
    // 飓风
    'hurricane': function (event, player, effectLevel, effectname, efficiency) {
        let entity = event.entity
        if (!entity.onGround()) {
            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)
        }
    },
    // 狂战士之铠
    'berserker': function (event, player, effectLevel, effectname, efficiency) {
        let health = player.health
        let maxHealth = player.getMaxHealth()
        let loss_health = (maxHealth - health) / maxHealth

        let tetra_wear_increased_damag = player.persistentData.getDouble('tetra_wear_increased_damag') ?? 0
        player.persistentData.putDouble('tetra_wear_increased_damag', tetra_wear_increased_damag + loss_health * effectLevel / 1000)
    },
    // 锻造模板——咒魂
    'upgrade_templates/cursium_ingot': function (event, player, effectLevel, effectname, efficiency) {
        let type = event.getSource().getType().toString()
        if (type == 'arrow') return
        if (effectLevel >= 4) {
            let tetra_wear_increased_damag = player.persistentData.getDouble('tetra_wear_increased_damag') ?? 0
            player.persistentData.putDouble('tetra_wear_increased_damag', tetra_wear_increased_damag + 0.2)
        }
    },
    // 遗忘者
    'forgotten': function (event, player, effectLevel, effectname, efficiency) {
        let time = player.persistentData.getInt(effectname) ?? 0
        let CD = Math.abs(player.age - time)
        if (CD < 20 * 60 * 2) return
        player.persistentData.putInt(effectname, player.age)


        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)

    },
    'default': function (event, player, effectLevel, effectname, efficiency) {
        return;
    }
};