// priority: 1000
/**
 * 获取玩家盔甲+饰品+主手特定效果的等级和效率值
 * @param {Internal.Player} player - 玩家实体
 * @param {string} effectName - 要查询的效果名称
 */
function get_specific_effect(player, effectName) {
    let allEffects = get_all_effect(player);
    let targetEffect = allEffects.find(effect => effect.name == effectName);

    if (targetEffect) {
        return {
            level: targetEffect.level,
            efficiency: targetEffect.efficiency
        };
    }
    return {
        level: 0,
        efficiency: 0
    }
}
/**
 * 获取玩家所有盔甲的效果及其等级和效率值
 * @param {Internal.Player} player - 玩家实体
 * @returns {Array<{name: string, level: number, efficiency: number}>} 效果数组
 */
function get_armor_effects(player) {
    const effectData = [];

    // 检查盔甲栏物品
    let armorItems = [
        player.getHeadArmorItem(),
        player.getChestArmorItem(),
        player.getLegsArmorItem(),
        player.getFeetArmorItem()
    ];

    armorItems.forEach(item => {
        if (item.item instanceof $ModularItem) {
            let effects = getAllEffects(item);
            if (effects) {
                effects.forEach(effect => {
                    let effectName = effect.name;
                    let level = effect.level;
                    let efficiency = effect.efficiency;

                    // 查找是否已存在该效果
                    let existingEffect = effectData.find(e => e.name === effectName);
                    if (existingEffect) {
                        existingEffect.level += level;
                        existingEffect.efficiency += efficiency;
                    } else {
                        effectData.push({
                            name: effectName,
                            level: level,
                            efficiency: efficiency
                        });
                    }
                });
            }
        }
    });

    return effectData;
}
/**
 * 获取玩家全部effect及其等级和效率值
 * @param {Internal.Player} player - 玩家实体
 * @returns {Array<{name: string, level: number, efficiency: number}>} 效果数组
 */
function get_all_effect(player) {
    const effectData = [];
    // 检查装备栏物品
    let itemList = [
        player.getHeadArmorItem(),
        player.getChestArmorItem(),
        player.getLegsArmorItem(),
        player.getFeetArmorItem(),
        player.getMainHandItem()
    ];

    itemList.forEach(item => {
        if (item.item instanceof $ModularItem) {
            let effects = getAllEffects(item);
            if (effects) {
                effects.forEach(effect => {
                    let effectName = effect.name;
                    let level = effect.level;
                    let efficiency = effect.efficiency;

                    // 查找是否已存在该效果
                    let existingEffect = effectData.find(e => e.name === effectName);
                    if (existingEffect) {
                        existingEffect.level += level;
                        existingEffect.efficiency += efficiency;
                    } else {
                        effectData.push({
                            name: effectName,
                            level: level,
                            efficiency: efficiency
                        });
                    }
                });
            }
        }
    });

    // 使用工具函数获取饰品效果
    const curiosEffects = get_all_curios_effects(player);
    curiosEffects.forEach(effect => {
        const existingEffect = effectData.find(e => e.name === effect.name);
        if (existingEffect) {
            existingEffect.level += effect.level;
            existingEffect.efficiency += effect.efficiency;
        } else {
            effectData.push({
                name: effect.name,
                level: effect.level,
                efficiency: effect.efficiency
            });
        }
    });

    return effectData;
}
/**
 * 获取玩家饰品栏中全部effect及其等级和效率值
 * @param {Internal.Player} player - 玩家实体
 * @returns {Array<{name: string, level: number, efficiency: number}>} 效果数组
 */
function get_all_curios_effects(player) {
    const effectData = [];
    let tetraCurios = getTetraCurios(player);

    if (tetraCurios && tetraCurios.length > 0) {
        tetraCurios.forEach(curio => {
            let effects = getAllEffects(curio);
            if (effects) {
                effects.forEach(effect => {
                    let existingEffect = effectData.find(e => e.name === effect.name);
                    if (existingEffect) {
                        existingEffect.level += effect.level;
                        existingEffect.efficiency += effect.efficiency;
                    } else {
                        effectData.push({
                            name: effect.name,
                            level: effect.level,
                            efficiency: effect.efficiency
                        });
                    }
                });
            }
        });
    }

    return effectData;
}
/**
 * 获取玩家饰品中某个effect的值和效率值
 * @param {Internal.Player} player - 玩家实体
 * @param {string} effectName - 要查询的effect名称
 * @returns {Object} 返回包含总等级和总效率值的对象 {level: number, efficiency: number}
 */
function getPlayerCuriosEffectData(player, effectName) {
    let totalLevel = 0;
    let totalEfficiency = 0;
    let tetraItems = getTetraCurios(player);

    if (!tetraItems) {
        return {
            level: 0,
            efficiency: 0
        };
    }

    for (const item of tetraItems) {
        let effects = getAllEffects(item);
        if (effects) {
            let effect = effects.find(e => e.name === effectName);
            if (effect) {
                totalLevel += effect.level;
                totalEfficiency += effect.efficiency;
            }
        }
    }

    return {
        level: totalLevel,
        efficiency: totalEfficiency
    };
}
/**
 * 获取物品上的所有效果
 * @param {ItemStack} item - 物品栈对象
 * @returns {Array<{name: string, level: number, efficiency: number}>} 效果数组
 */
function getAllEffects(item) {
    if (item.item instanceof $ModularItem) {
        // 从NBT中读取已排序的效果数据
        if (item.nbt && item.nbt.tetra_all_effect) {
            return item.nbt.tetra_all_effect
        } else {
            updateItemEffects(item)
            return item.nbt.tetra_all_effect
        }
    } else {
        return []
    }
};

/**
 * 更新物品的效果数据并存储到NBT中
 * @param {ItemStack} item - 物品栈对象
 * @returns {void}
 */
function updateItemEffects(item) {
    if (!item || !item.item) return;
    if (item.item instanceof $ModularItem) {
        $ModularItem.updateIdentifier(item);

        // 先获取实际效果
        let actualEffects = item.item.getEffects(item) || [];
        let effects;

        if (actualEffects.length > 0) {
            // 有实际效果，转换格式
            effects = actualEffects.map(effectName => ({
                name: effectName.key,
                level: simpleGetTetraEffectLevel(item, effectName.key),
                efficiency: item.item.getEffectEfficiency(item, effectName) || 0
            }));
        } else {
            // 无实际效果，使用默认值
            effects = [{
                name: 'default',
                level: 0,
                efficiency: 0
            }];
        }

        effects.sort((a, b) => a.name.localeCompare(b.name));

        // 存入 NBT
        if (item.nbt) {
            item.nbt.put('tetra_all_effect', effects);
        }
    }
}

/**
 * 获取盔甲某个effect的总等级和总效率值
 * @param {Internal.Player} player - 玩家实体
 * @param {string} effect - 要查询的effect名称
 * @returns {Object} 返回包含总等级和总效率值的对象 {level: number, efficiency: number}
 */
function arrmor_total_effect_level(player, effect) {
    let itemList = [
        player.getHeadArmorItem(),
        player.getChestArmorItem(),
        player.getLegsArmorItem(),
        player.getFeetArmorItem()
    ];
    let totalLevel = 0;
    let totalEfficiency = 0;

    itemList.forEach(armor => {
        let effects = getAllEffects(armor);
        if (effects) {
            let targetEffect = effects.find(e => e.name === effect);
            if (targetEffect) {
                totalLevel += targetEffect.level;
                totalEfficiency += targetEffect.efficiency;
            }
        }
    });

    return {
        level: totalLevel,
        efficiency: totalEfficiency
    };
}
//是否含有某effect
function hasItemEffect(item, effect) {
    let level = simpleGetTetraEffectLevel(item, effect)
    if (level > 0) return true;
    else return false;
}
/**
* 简单获取tetra的Effect等级
* @param {$ItemStack_} itemstack 
* @param {string} effectname 
* @returns {number}
*/
function simpleGetTetraEffectLevel(itemstack, effectname) {
    if (itemstack.item instanceof $ModularItem) {
        return itemstack.item.getEffectLevel(itemstack, $ItemEffect.get(effectname))
    } else return 0
}
function simpleGetEffectEfficiency(itemstack, effectname) {
    if (itemstack.item instanceof $ModularItem) {
        return itemstack.item.getEffectEfficiency(itemstack, effectname)
    } else return 0
}
/**
 * 检测玩家是否含有某个effect
 */
function all_effect_check(player, effect) {
    let effects = get_all_effect(player);

    for (let effectName in effects) {
        if (effectName === effect) {
            return true;
        }
    }

    return false;
}