// priority: 9
//每秒玩家刻事件
//参考脆骨症夜鸢大佬的代码
//不清楚优化会不会更好，但是写起来方便一点
PlayerEvents.tick(event => {
    let player = event.player;
    // 每秒执行一次
    if (player.age % 20 != 0) return;
    let item = player.getMainHandItem();
    if (!item.item instanceof $ModularItem) return;

    let effects = getAllEffects(item);
    if (!effects) return;
    effects.forEach(effect => {
        let strategy = tetraPlayerTickStrategies[effect.name] || tetraPlayerTickStrategies.default;
        strategy(event, player, effect.level, item, effect.name, effect.efficiency);
    });
});
const tetraPlayerTickStrategies = {
    // 幽匿亲和
    'echo_1': function (event, player, effectLevel, item, effectname, efficiency) {
        let time = player.persistentData.getInt(effectname) ?? 0
        let CD = Math.abs(player.age - time)
        let duration = 20 * 60 * 0.5 // 两分钟检测一次

        if (CD > duration) {
            if (getPlayerBiomeKey(player) == "minecraft:deep_dark") {
                player.persistentData.putInt(effectname + "_biome", 1) // 压入群系
                player.tell(1)
            } else {
                player.persistentData.putInt(effectname + "_biome", 0)
                startBlockScan(
                    player,
                    ['minecraft:sculk_vein', 'minecraft:obsidian'],
                    10,
                    (count) => {
                        player.persistentData.putInt(effectname + "_number", count) // 压入方块数量
                        // player.tell(`找到 ${count} 个目标方块！`);
                    },
                    50
                );
                player.tell(2)
            }
            player.persistentData.putInt(effectname, player.age) // 压入cd
        }
    },
    // 畸变
    'aberration': function (event, player, effectLevel, item, effectname, efficiency) {
        let currentYingYangCount = player.persistentData.getInt(YingYangCount) ?? 0
        let radiation_efficiency = simple_get_radiation(player).efficiency
        updateYingYangCount(player, currentYingYangCount + radiation_efficiency * (1 + efficiency / 100))
    },
    // 狂暴法术
    'fury_spell': function (event, player, effectLevel, item, effectname, efficiency) {
        if (item.nbt.getString('fury_spell_entity') && player.level.getEntity(item.nbt.getString('fury_spell_entity'))) {

            let entity = player.level.getEntity(item.nbt.getString('fury_spell_entity'))
            for (let i = 1; i <= 20; i++) {
                entity.getServer().scheduleInTicks(i, () => {
                    entity.invulnerableTime = 20
                    entity.teleportTo(player.level.getDimension(), player.x, player.y, player.z, entity.yaw, entity.pitch)

                    entity.level.getEntities(entity, entity.getBoundingBox().inflate(0.1)).forEach(player_1 => {
                        if (player_1.isPlayer()) {
                            player_1.potionEffects.add('pasterdream:fury_spell_buff', 40, 0)
                        }
                    })
                })
            }

        } else {
            let spawn_entity = player.level.createEntity("pasterdream:fury_spell_entity");
            spawn_entity.setPosition(player.x, player.y, player.z);
            spawn_entity.spawn();
            item.nbt.putString('fury_spell_entity', spawn_entity.getUuid())

            for (let i = 1; i <= 20; i++) {
                spawn_entity.getServer().scheduleInTicks(i, () => {
                    spawn_entity.invulnerableTime = 20
                    spawn_entity.teleportTo(player.level.getDimension(), player.x, player.y, player.z, spawn_entity.yaw, spawn_entity.pitch)

                    spawn_entity.level.getEntities(spawn_entity, spawn_entity.getBoundingBox().inflate(0.1)).forEach(player_1 => {
                        if (player_1.isPlayer()) {
                            player_1.potionEffects.add('pasterdream:fury_spell_buff', 40, 0)
                        }
                    })
                })
            }
        }
    },
    // 鹦鹉螺壳
    'nautilus_shell': function (event, player, effectLevel, item, effectname, efficiency) {
        player.potionEffects.add('minecraft:water_breathing', 20 * 2, 2)
        player.potionEffects.add('minecraft:dolphins_grace', 20 * 2, 2)
    },
    // 贯虹
    'guan_hong': function (event, player, effectLevel, item, effectname, efficiency) {
        // 检查主手是否持有Tetra弓
        if (item.getItem() instanceof $ModularBowItem) {
            // 检查玩家是否正在拉弓
            if (player.isUsingItem() && player.getUseItem() === item) {
                // 获取蓄力时长
                let bow = item.getItem();
                let useDuration = bow.getUseDuration(item);
                let remainingTicks = player.getUseItemRemainingTicks();
                let time = (useDuration - remainingTicks) / 20
                //获取最大拉弓耗时
                let drawDuration = item.getItem().getDrawDuration(item) / 20

                player.persistentData.putInt('guan_hong', Math.min(time, drawDuration))
            }
        }
    },
    // 下界合金块
    'netherite_block': function (event, player, effectLevel, item, effectname, efficiency) {
        player.potionEffects.add('minecraft:fire_resistance', 20 * 1, 0)
    },
    //红石
    'red_stone': function (event, player, effectLevel, item, effectname, efficiency) {
        player.potionEffects.add('minecraft:haste', 20 * 10, 0)
    },
    //融化
    'ice_break': function (event, player, effectLevel, item, effectname, efficiency) {
        // 获取当前耐久度
        let currentDurability = item.getDamageValue();
        // 获取最大耐久度
        let maxDurability = item.getMaxDamage();

        // 如果还有耐久可以损失
        if (currentDurability < maxDurability) {
            // 增加1点损耗
            item.setDamageValue(currentDurability + 1);
        }
    },
    //光合作用
    'leaves_photosynthesis': function (event, player, effectLevel, item, effectname, efficiency) {
        if (item.damageVale) {
            let level = getPlayerLightLevel(player)
            item.damageVale = item.damageVale - level
        }
    },
    //金锭
    'gold_ingot': function (event, player, effectLevel, item, effectname, efficiency) {
        player.potionEffects.add('minecraft:haste', 20 * 10, 1)
    },
    //利维坦肉
    'leviathan_and_abyssal_egg': function (event, player, effectLevel, item, effectname, efficiency) {
        player.potionEffects.add('minecraft:regeneration', 20 * 10, 1)
    },
    //时之沙
    'time_hourglass': function (event, player, effectLevel, item, effectname, efficiency) {
        let random1 = Math.random()
        let random2 = Math.round(Math.random() * 2 * 100) / 100
        let damage = item.nbt.kubejs_damage || 0
        let shang_xian = item.nbt.shang_xian || 0
        if (random1 < 0.9 || shang_xian > 10) return
        item.nbt.putInt('kubejs_damage', damage + random2)
        item.nbt.putInt('shang_xian', shang_xian + 1)
        player.tell('主手武器攻击力已增加' + random2)
    },
    //脉冲风行者水晶
    'pulse_windrunner_crystal': function (event, player, effectLevel, item, effectname, efficiency) {
        player.potionEffects.add('kubejs:fei_xing', 20 * 6, 2)
    },
    //风行者水晶
    'windrunner_crystal': function (event, player, effectLevel, item, effectname, efficiency) {
        let dimension = player.level.getDimension()
        let meltdreamenergy = player.getAttribute('pasterdream:meltdreamenergy').getValue()
        if (dimension == 'pasterdream:wind_journey_world') {
            player.potionEffects.add('kubejs:fei_xing', 20 * 6, 0)
        } else if (meltdreamenergy >= 1) {
            player.potionEffects.add('kubejs:fei_xing', 20 * 6, 0)
            player.setAttributeBaseValue('pasterdream:meltdreamenergy', player.getAttribute('pasterdream:meltdreamenergy').getValue() - 1)
        }
    },
    //恶魂之泪
    'ghast_tear': function (event, player, effectLevel, item, effectname, efficiency) {
        player.potionEffects.add('minecraft:regeneration', 20 * 60, 1)
    },
    //机械手柄
    'ji_xie_shou_bing': function (event, player, effectLevel, item, effectname, efficiency) {
        player.potionEffects.add('minecraft:haste', 20 * 10, effectLevel * (1 + get_gear_shift(player) / 100) - 1)
    },
    //原初核心
    'primordial_core': function (event, player, effectLevel, item, effectname, efficiency) {
        player.potionEffects.add('biomancy:despoil', 20 * 60, 1)
    },
    //巨食人鱼眼效果
    'sea_terror_eye': function (event, player, effectLevel, item, effectname, efficiency) {
        player.potionEffects.add('minecraft:night_vision', 20 * 60, 0)
        player.potionEffects.add('minecraft:water_breathing', 20 * 60, 0)
        player.potionEffects.add('minecraft:dolphins_grace', 20 * 60, 0)
    },
    'default': function (event, player, effectLevel, item, effectname, efficiency) {
        return
    }
}