#!/usr/bin/env bash
#
# 菜单显示和处理脚本

select_backup_config_menu() {
    local title="$1"
    local callback_single="$2"
    local callback_all="${3:-}"
    local prefix="backup-"
    local configs_map=()
    local config_ids=()
    for conf_file in "${CONF_DIR}"/"$prefix"*.conf; do
        [[ -f "$conf_file" ]] || continue
        local id repo
        id=$(basename "$conf_file" .conf)
        repo=$(get_value_from_conf "$conf_file" "RESTIC_REPOSITORY")
        configs_map+=("ID: ${id} (Repo: ${repo})")
        config_ids+=("$id")
    done
    if [[ ${#configs_map[@]} -eq 0 ]]; then
        msg_warn "配置目录 '${CONF_DIR}' 中没有任何 '${prefix}*.conf' 文件"
        pause
        return
    fi
    clear
    msg_info "--- ${title} ---"
    msg "选择一个备份配置:"
    local i=1
    for item in "${configs_map[@]}"; do
        msg " ${i}) ${item}"
        ((i++))
    done
    if [[ -n "$callback_all" ]]; then msg_ok " a) 所有配置"; fi
    msg_warn " b) 返回"
    local choice
    prompt_for_input "选择" choice
    if [[ "$choice" =~ ^[0-9]+$ ]] && [ "$choice" -ge 1 ] && [ "$choice" -le ${#configs_map[@]} ]; then
        local selected_id=${config_ids[$choice-1]}
        "$callback_single" "$selected_id"
    elif [[ -n "$callback_all" && "${choice,,}" == "a" ]]; then
        "$callback_all" "${config_ids[@]}"
    elif [[ "${choice,,}" == "b" ]]; then
        return
    else
        msg_err "无效选择"
    fi
    pause
}
select_notify_config_menu() {
    local title="$1"
    local callback_single="$2"
    local callback_all="${3:-}"
    local prefix="notify-"
    local configs_map=()
    local config_ids=()
    for conf_file in "${CONF_DIR}"/"$prefix"*.conf; do
        [[ -f "$conf_file" ]] || continue
        local id type dest
        id=$(basename "$conf_file" .conf)
        type=$(get_value_from_conf "$conf_file" "NOTIFY_TYPE")
        if [[ "$type" == "telegram" ]]; then
            dest=$(get_value_from_conf "$conf_file" "TELEGRAM_CHAT_ID")
        else
            dest=$(get_value_from_conf "$conf_file" "TO_ADDR")
        fi
        configs_map+=("ID: ${id} (Type: ${type}, To: ${dest})")
        config_ids+=("$id")
    done
    if [[ ${#configs_map[@]} -eq 0 ]]; then
        msg_warn "没有可用的通知配置"
        pause
        return
    fi
    clear
    msg_info "--- ${title} ---"
    msg "选择一个通知配置:"
    local i=1
    for item in "${configs_map[@]}"; do
        msg " ${i}) ${item}"
        ((i++))
    done
    if [[ -n "$callback_all" ]]; then msg_ok " a) 所有配置"; fi
    msg_warn " b) 返回"
    local choice
    prompt_for_input "选择" choice
    if [[ "$choice" =~ ^[0-9]+$ ]] && [ "$choice" -ge 1 ] && [ "$choice" -le ${#configs_map[@]} ]; then
        local selected_id=${config_ids[$choice-1]}
        "$callback_single" "$selected_id"
    elif [[ -n "$callback_all" && "${choice,,}" == "a" ]]; then
        "$callback_all" "${config_ids[@]}"
    elif [[ "${choice,,}" == "b" ]]; then
        return
    else
        msg_err "无效选择"
    fi
    pause
}

show_menu() {
    clear
    msg_info "====================================="
    msg_info "          Backup Tool $VERSION"
    msg_info "====================================="
    msg "选择操作:"
    msg_ok " 1. 添加备份"
    msg_ok " 2. 修改备份"
    msg_ok " 3. 查看备份"
    msg_ok " 4. 删除备份"
    msg_ok " 5. 应用备份"
    msg_ok ""
    msg_ok " 6. 添加通知"
    msg_ok " 7. 修改通知"
    msg_ok " 8. 查看通知"
    msg_ok " 9. 删除通知"
    msg_ok ""
    msg_ok "10. 高级"
    msg_ok "11. 卸载"
    msg_ok ""
    msg " q. 退出"
    msg_info "-------------------------------------"
}
advanced_settings_menu() {
    while true; do
        clear
        msg_info "====================================="
        msg_info "           高级功能"
        msg_info "====================================="
        msg_ok " 1. 立即备份"
        msg_ok " 2. 恢复备份"
        msg_ok " 3. 通知测试"
        msg_ok " 4. 更新脚本"
        msg_ok " 5. 安装 rclone"
        msg_warn " b. 返回主菜单"
        msg_info "-------------------------------------"
        prompt_for_input "选择" choice
        case "$choice" in
            1) select_backup_config_menu "立即备份" "backup_single_backup_config" ;;
            2) select_backup_config_menu "恢复备份" "restore_single_backup_config" ;;
            3) select_notify_config_menu "通知测试" "test_single_notify_config" ;;
            4) self_update ;;
            5) install_rclone ;;
            b|B) break ;;
            *) msg_warn "无效选择"; sleep 1 ;;
        esac
    done
}