#!/bin/bash

# 快速麦克风权限修复脚本
# 用于解决macOS上Qt程序的麦克风权限问题

set -e

echo "🎤 Qt语音识别程序 - 麦克风权限快速修复"
echo "============================================"
echo ""

# 检查是否为macOS系统
if [[ "$OSTYPE" != "darwin"* ]]; then
    echo "❌ 此脚本仅适用于macOS系统"
    exit 1
fi

# 检查程序文件
QT_APP="./cmake-build-debug/qt_speech_simple"
if [ ! -f "$QT_APP" ]; then
    echo "❌ Qt程序不存在: $QT_APP"
    echo "请先编译项目："
    echo "  mkdir -p cmake-build-debug"
    echo "  cd cmake-build-debug"
    echo "  cmake .."
    echo "  make"
    exit 1
fi

echo "✅ 找到Qt程序: $QT_APP"
echo ""

# 显示当前权限状态
echo "📋 当前麦克风权限状态："
echo "----------------------------------------"
if sqlite3 ~/Library/Application\ Support/com.apple.TCC/TCC.db \
   "SELECT client, auth_value FROM access WHERE service='kTCCServiceMicrophone';" 2>/dev/null | grep -q "qt_speech_simple"; then
    echo "✅ 找到程序的权限记录"
else
    echo "⚠️  未找到程序的权限记录"
fi
echo ""

# 提供修复选项
echo "🔧 请选择修复方法："
echo "----------------------------------------"
echo "1. 重置所有麦克风权限（推荐）"
echo "2. 打开系统设置手动配置"
echo "3. 直接启动程序测试"
echo "4. 退出"
echo ""

read -p "请输入选择 (1-4): " choice

case $choice in
    1)
        echo ""
        echo "🔄 重置麦克风权限..."
        if sudo tccutil reset Microphone; then
            echo "✅ 权限重置成功"
            echo ""
            echo "📱 即将启动Qt程序，请注意："
            echo "   1. 程序启动时会弹出权限请求对话框"
            echo "   2. 请点击 '允许' 或 'Allow'"
            echo "   3. 如果没有弹出对话框，请手动在系统设置中添加权限"
            echo ""
            read -p "按回车键启动程序..."
            
            # 重启音频服务确保权限生效
            echo "🔄 重启音频服务..."
            sudo killall coreaudiod 2>/dev/null || true
            sleep 2
            
            # 启动程序
            echo "🚀 启动Qt程序..."
            cd cmake-build-debug
            ./qt_speech_simple
        else
            echo "❌ 权限重置失败，可能需要管理员权限"
        fi
        ;;
    2)
        echo ""
        echo "📱 打开系统设置进行手动配置..."
        echo ""
        echo "请按以下步骤操作："
        echo "1. 系统设置 → 隐私与安全性 → 麦克风"
        echo "2. 点击右侧的 + 按钮"
        echo "3. 浏览到: $(pwd)/cmake-build-debug/qt_speech_simple"
        echo "4. 选择程序并确保开关为开启状态"
        echo ""
        
        # 尝试打开系统设置
        open "x-apple.systempreferences:com.apple.preference.security?Privacy_Microphone" 2>/dev/null || \
        open "/System/Library/PreferencePanes/Security.prefPane" 2>/dev/null || \
        echo "请手动打开系统设置"
        
        read -p "配置完成后按回车键启动程序..."
        cd cmake-build-debug
        ./qt_speech_simple
        ;;
    3)
        echo ""
        echo "🚀 直接启动程序进行测试..."
        cd cmake-build-debug
        ./qt_speech_simple
        ;;
    4)
        echo "👋 退出脚本"
        exit 0
        ;;
    *)
        echo "❌ 无效选择"
        exit 1
        ;;
esac

echo ""
echo "🎉 脚本执行完成！"
echo ""
echo "💡 如果仍有问题，请查看详细文档："
echo "   - docs/MICROPHONE_PERMISSION_FIX.md"
echo "   - docs/MICROPHONE_RECOGNITION_GUIDE.md"
echo ""
echo "或运行完整诊断脚本："
echo "   ./check_audio_permissions.sh"