#!/bin/bash

# 音频文件自动重命名脚本
# 使用方法: 将下载的音频文件放在Downloads文件夹，运行此脚本自动重命名并移动到正确位置

echo "🎵 音频文件重命名脚本"
echo "========================================"

# 目标目录
TARGET_DIR="app/src/main/res/raw"
DOWNLOADS_DIR="$HOME/Downloads"

# 创建目标目录
mkdir -p "$TARGET_DIR"

echo "📂 源目录: $DOWNLOADS_DIR"
echo "📂 目标目录: $TARGET_DIR"
echo ""

# 重命名函数
rename_and_move() {
    local pattern=$1
    local target_name=$2
    local description=$3
    
    echo "🔍 查找: $description ($pattern)"
    
    # 在Downloads目录中查找匹配的文件
    local found_file=$(find "$DOWNLOADS_DIR" -iname "*$pattern*" -type f \( -name "*.mp3" -o -name "*.wav" -o -name "*.ogg" \) | head -1)
    
    if [ -n "$found_file" ]; then
        echo "✅ 找到文件: $(basename "$found_file")"
        echo "📝 重命名为: $target_name"
        
        # 复制并重命名文件
        cp "$found_file" "$TARGET_DIR/$target_name"
        
        if [ $? -eq 0 ]; then
            echo "✅ 成功: $target_name"
            # 可选：删除原文件
            # rm "$found_file"
        else
            echo "❌ 失败: 无法移动 $target_name"
        fi
    else
        echo "❌ 未找到匹配文件，请手动下载"
        echo "   建议搜索: $pattern"
    fi
    echo ""
}

echo "🎼 开始处理背景音乐..."
rename_and_move "ambient*mystery" "ambient_mystery.mp3" "神秘氛围音乐"
rename_and_move "electronic*tension" "electronic_tension.mp3" "电子紧张音乐"
rename_and_move "orchestral*revelation" "orchestral_revelation.mp3" "管弦乐启示"
rename_and_move "epic*finale" "epic_finale.mp3" "史诗终章"

echo "🌟 开始处理环境音效..."
rename_and_move "ventilation" "ventilation_soft.mp3" "通风系统音效"
rename_and_move "heart*monitor" "heart_monitor.mp3" "心率监控音效"
rename_and_move "reactor*hum" "reactor_hum.mp3" "反应堆嗡鸣"
rename_and_move "space*silence" "space_silence.mp3" "太空寂静"

echo "⛈️ 开始处理天气音效..."
rename_and_move "wind*gentle" "wind_gentle.mp3" "微风音效"
rename_and_move "rain*light" "rain_light.mp3" "小雨音效"
rename_and_move "storm*cyber" "storm_cyber.mp3" "电子风暴"
rename_and_move "solar*storm" "solar_storm.mp3" "太阳风暴"

echo "🔘 开始处理UI音效..."
rename_and_move "button*click" "button_click.mp3" "按钮点击音效"
rename_and_move "notification*beep" "notification_beep.mp3" "通知提示音效"
rename_and_move "error*alert" "error_alert.mp3" "错误警报音效"

echo "🎭 开始处理事件音效..."
rename_and_move "discovery*chime" "discovery_chime.mp3" "发现音效"
rename_and_move "time*distortion" "time_distortion.mp3" "时间扭曲音效"
rename_and_move "oxygen*leak" "oxygen_leak_alert.mp3" "氧气泄漏警报"

echo ""
echo "📋 检查结果..."
echo "========================================"

# 检查所有必需的文件
required_files=(
    "ambient_mystery.mp3"
    "electronic_tension.mp3"
    "orchestral_revelation.mp3"
    "epic_finale.mp3"
    "ventilation_soft.mp3"
    "heart_monitor.mp3"
    "reactor_hum.mp3"
    "space_silence.mp3"
    "wind_gentle.mp3"
    "rain_light.mp3"
    "storm_cyber.mp3"
    "solar_storm.mp3"
    "button_click.mp3"
    "notification_beep.mp3"
    "error_alert.mp3"
    "discovery_chime.mp3"
    "time_distortion.mp3"
    "oxygen_leak_alert.mp3"
)

found_count=0
missing_files=()

for file in "${required_files[@]}"; do
    if [ -f "$TARGET_DIR/$file" ]; then
        echo "✅ $file"
        ((found_count++))
    else
        echo "❌ $file (缺失)"
        missing_files+=("$file")
    fi
done

echo ""
echo "📊 统计结果:"
echo "✅ 已找到: $found_count / ${#required_files[@]} 个文件"
echo "❌ 缺失: $((${#required_files[@]} - found_count)) 个文件"

if [ ${#missing_files[@]} -gt 0 ]; then
    echo ""
    echo "🔍 缺失的文件需要手动下载:"
    for file in "${missing_files[@]}"; do
        echo "   - $file"
    done
    echo ""
    echo "💡 建议:"
    echo "   1. 查看 AUDIO_DOWNLOAD_GUIDE.md 获取下载链接"
    echo "   2. 下载文件到 ~/Downloads 目录"
    echo "   3. 重新运行此脚本"
fi

echo ""
echo "🎉 重命名脚本执行完成！"

if [ $found_count -ge 3 ]; then
    echo "✨ 你现在可以编译并测试音频系统了:"
    echo "   ./gradlew assembleDebug"
else
    echo "⚠️  建议至少下载3个核心文件再测试音频系统:"
    echo "   - button_click.mp3 (UI测试)"
    echo "   - ambient_mystery.mp3 (背景音乐测试)"  
    echo "   - error_alert.mp3 (音效测试)"
fi
