// 基础设施测试程序
#include "code/base/logger.h"
#include "code/base/media_thread_base.h"
#include "code/base/media_common.h"
#include <iostream>
#include <thread>
#include <chrono>

using namespace av;

// 测试线程类
class TestThread : public ThreadBase {
public:
    TestThread() {
        setThreadName("TestThread");
    }
    
protected:
    void run() override {
        AV_LOGGER_INFO("Test thread started");
        
        for (int i = 0; i < 10 && !shouldExit(); ++i) {
            AV_LOGGER_INFOF("Test thread iteration: {}", i);
            
            if (!waitForMs(100)) {
                break; // 收到退出信号
            }
        }
        
        AV_LOGGER_INFO("Test thread finished");
    }
};

int main() {
    std::cout << "=== AV基础设施测试 ===" << std::endl;
    
    // 1. 测试日志系统
    std::cout << "1. 测试日志系统..." << std::endl;
    Logger::initialize("test_log.txt", LogLevel::DEBUG, false, true);

    AV_LOGGER_DEBUG("这是一条调试信息");
    AV_LOGGER_INFO("这是一条信息");
    AV_LOGGER_WARNING("这是一条警告");
    AV_LOGGER_ERROR("这是一条错误");
    
    AV_LOGGER_INFOF("格式化日志测试: {} + {} = {}", 1, 2, 3);
    
    // 2. 测试时间工具
    std::cout << "2. 测试时间工具..." << std::endl;
    {
        AV_SCOPED_TIMER("测试代码块");
        std::this_thread::sleep_for(std::chrono::milliseconds(50));
    }
    
    time_utils::Timer timer;
    timer.start();
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    timer.stop();
    AV_LOGGER_INFOF("计时器测试: {} ms", timer.elapsedMs());
    
    // 3. 测试字符串工具
    std::cout << "3. 测试字符串工具..." << std::endl;
    std::string testStr = "  Hello, World!  ";
    AV_LOGGER_INFOF("原始字符串: '{}'", testStr);
    AV_LOGGER_INFOF("修剪后: '{}'", string_utils::trim(testStr));
    AV_LOGGER_INFOF("转大写: '{}'", string_utils::toUpper(testStr));
    
    auto parts = string_utils::split("a,b,c,d", ',');
    AV_LOGGER_INFOF("分割结果数量: {}", parts.size());
    
    // 4. 测试路径工具
    std::cout << "4. 测试路径工具..." << std::endl;
    std::string path = "C:/test/file.txt";
    AV_LOGGER_INFOF("路径: {}", path);
    AV_LOGGER_INFOF("目录: {}", path_utils::getDirectory(path));
    AV_LOGGER_INFOF("文件名: {}", path_utils::getFilename(path));
    AV_LOGGER_INFOF("扩展名: {}", path_utils::getExtension(path));
    
    // 5. 测试线程基类
    std::cout << "5. 测试线程基类..." << std::endl;
    {
        TestThread testThread;
        
        testThread.setOnStarted([]() {
            AV_LOGGER_INFO("线程启动回调被调用");
        });
        
        testThread.setOnFinished([]() {
            AV_LOGGER_INFO("线程完成回调被调用");
        });
        
        AV_LOGGER_INFO("启动测试线程...");
        if (testThread.start()) {
            AV_LOGGER_INFO("测试线程启动成功");
            
            // 让线程运行一段时间
            std::this_thread::sleep_for(std::chrono::milliseconds(300));
            
            AV_LOGGER_INFO("停止测试线程...");
            testThread.stop();
            testThread.join();
            
            AV_LOGGER_INFO("测试线程已停止");
        } else {
            AV_LOGGER_ERROR("测试线程启动失败");
        }
    }
    
    // 6. 测试工作线程
    std::cout << "6. 测试工作线程..." << std::endl;
    {
        int counter = 0;
        auto worker = makeWorkerThread([&counter]() {
            counter++;
            AV_LOGGER_INFOF("工作线程计数: {}", counter);
        }, 50);
        
        worker->setThreadName("WorkerThread");
        worker->start();
        
        std::this_thread::sleep_for(std::chrono::milliseconds(200));
        
        worker->stop();
        worker->join();
        
        AV_LOGGER_INFOF("工作线程最终计数: {}", counter);
    }
    
    // 7. 测试内存池
    std::cout << "7. 测试内存池..." << std::endl;
    {
        memory_utils::MemoryPool pool(64, 10);
        
        std::vector<void*> ptrs;
        for (int i = 0; i < 5; ++i) {
            void* ptr = pool.allocate();
            if (ptr) {
                ptrs.push_back(ptr);
                AV_LOGGER_INFOF("分配内存块 {}", i);
            }
        }
        
        AV_LOGGER_INFOF("剩余可用块: {}", pool.getAvailableBlocks());
        
        for (void* ptr : ptrs) {
            pool.deallocate(ptr);
        }
        
        AV_LOGGER_INFOF("释放后可用块: {}", pool.getAvailableBlocks());
    }
    
    AV_LOGGER_INFO("=== 所有测试完成 ===");
    std::cout << "测试完成，请查看 test_log.txt 文件" << std::endl;
    
    return 0;
}
