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

using namespace av;
using namespace av::recorder;

int main() {
    // 初始化日志
    Logger::instance().setLevel(LogLevel::DEBUG);
    Logger::instance().info("开始测试音频录制器完整修复");
    
    try {
        // 创建音频录制器
        auto audioRecorder = std::make_unique<AudioRecorder>();
        
        // 设置录制参数
        RecorderParams params;
        params.type = MediaType::AUDIO;
        params.outputPath = "test_audio_complete.aac";
        params.audioParams.sampleRate = 44100;
        params.audioParams.channels = 2;
        params.audioParams.sampleFormat = AV_SAMPLE_FMT_S16;
        params.audioParams.bitrate = 128000;
        params.audioParams.codecName = "aac";
        params.audioParams.deviceId = "";
        
        std::cout << "初始化音频录制器..." << std::endl;
        ErrorCode result = audioRecorder->initialize(params);
        if (result != ErrorCode::SUCCESS) {
            std::cerr << "音频录制器初始化失败: " << static_cast<int>(result) << std::endl;
            return -1;
        }
        std::cout << "音频录制器初始化成功!" << std::endl;
        
        std::cout << "开始录制..." << std::endl;
        result = audioRecorder->startRecording();
        if (result != ErrorCode::SUCCESS) {
            std::cerr << "开始录制失败: " << static_cast<int>(result) << std::endl;
            return -1;
        }
        std::cout << "录制已开始!" << std::endl;
        
        // 录制5秒
        std::cout << "录制中，请说话..." << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(5));
        
        std::cout << "停止录制..." << std::endl;
        result = audioRecorder->stopRecording();
        if (result != ErrorCode::SUCCESS) {
            std::cerr << "停止录制失败: " << static_cast<int>(result) << std::endl;
            return -1;
        }
        std::cout << "录制已停止!" << std::endl;
        
        // 关闭录制器
        audioRecorder->close();
        std::cout << "音频录制器已关闭" << std::endl;
        
        std::cout << "测试完成! 输出文件: test_audio_complete.aac" << std::endl;
        
    } catch (const std::exception& e) {
        std::cerr << "测试过程中发生异常: " << e.what() << std::endl;
        return -1;
    }
    
    return 0;
}