#include "code/recorder/recorder_audio_recorder.h"
#include "code/base/logger.h"
#include <iostream>
#include <thread>
#include <chrono>

using namespace av::recorder;
using namespace av;

int main() {
    // 初始化日志
    Logger::instance().setLevel(LogLevel::DEBUG);
    
    std::cout << "测试音频录制器修复..." << std::endl;
    
    try {
        // 创建音频录制器
        AudioRecorder recorder;
        
        // 设置录制参数
        AudioRecorderParams params;
        params.capturerType = capture::CapturerType::AUDIO_MIC;
        params.sampleRate = 44100;
        params.channels = 2;  // 设置为2声道
        params.sampleFormat = AV_SAMPLE_FMT_S16;
        params.codecName = "aac";
        params.bitrate = 128000;
        params.outputPath = "./test_audio_fix.mp4";
        params.format = "mp4";
        
        std::cout << "初始化录制器参数: " << params.sampleRate << "Hz, " 
                  << params.channels << "ch, " << params.codecName << std::endl;
        
        // 初始化录制器
        ErrorCode result = recorder.initialize(params);
        
        if (result == ErrorCode::SUCCESS) {
            std::cout << "✓ 音频录制器初始化成功！" << std::endl;
            
            // 尝试开始录制
            result = recorder.startRecording();
            if (result == ErrorCode::SUCCESS) {
                std::cout << "✓ 音频录制开始成功！" << std::endl;
                
                // 录制3秒
                std::this_thread::sleep_for(std::chrono::seconds(3));
                
                // 停止录制
                result = recorder.stopRecording();
                if (result == ErrorCode::SUCCESS) {
                    std::cout << "✓ 音频录制停止成功！" << std::endl;
                } else {
                    std::cout << "✗ 停止录制失败，错误码: " << static_cast<int>(result) << std::endl;
                }
            } else {
                std::cout << "✗ 开始录制失败，错误码: " << static_cast<int>(result) << std::endl;
            }
            
            recorder.close();
        } else {
            std::cout << "✗ 音频录制器初始化失败，错误码: " << static_cast<int>(result) << std::endl;
            return 1;
        }
        
    } catch (const std::exception& e) {
        std::cout << "✗ 异常: " << e.what() << std::endl;
        return 1;
    }
    
    std::cout << "测试完成！" << std::endl;
    return 0;
}