// globals.cpp
#include "globals.h"

// camera.cpp 顶部或 globals.cpp 中
long long g_valveActionCount = 0;

// 初始化图片显示互斥锁和MIL_ID
QMutex gDispPicMutex0;
MIL_ID gDispCurrentPicId0 = 0;

QMutex gDispPicMutex1;
MIL_ID gDispCurrentPicId1 = 0;

// 初始化掩膜互斥锁和MIL_ID
QMutex gMaskMutex0;
MIL_ID gMask0 = 0;

QMutex gMaskMutex1;
MIL_ID gMask1 = 0;
//艳丽检测参数
std::map<std::string, int> params;
std::vector<std::string> colors;

// 初始化 CameraData 实例
DetectionResult g_detection_result[2];

// 初始化检测准备信号量
QSemaphore detection_ready(0);

// 初始化全局存储队列
ThreadSafeQueue<ImageData> g_storageQueue;

// 初始化全局图像队列和结果队列
ThreadSafeQueue<ImageData>* g_img_Queue[2] = { nullptr, nullptr };
ThreadSafeQueue<ImageData>* g_result_Queue[2] = { nullptr, nullptr };

// 初始化全局识别线程指针
std::thread* g_recognitionThread[2] = { nullptr, nullptr };

// 初始化线程控制变量指针
std::atomic<bool>* g_recognitionRunning[2] = { nullptr, nullptr };

bool g_dl_enable[2] = { true, true };
bool g_traditional_enable[2] = { true, true };


// 初始化全局ONNXRunner实例数组
ONNXRunner* g_runner_array[2] = { nullptr, nullptr };

// 实现获取保存目录和配置目录的函数
QString getSaveDirectory() {
    QDir appDir(QCoreApplication::applicationDirPath());
    QString saveDir = appDir.filePath("images");

    if (!appDir.exists(saveDir)) {
        appDir.mkdir("images"); // 创建目录，如果不存在
    }

    return saveDir;
}

QString getConfigDirectory() {
    // 获取应用程序的目录路径
    QString appPath = QCoreApplication::applicationDirPath();
    QDir appDir(appPath);

    // 获取绝对路径的 config 目录
    QString configDir = appDir.absoluteFilePath("config");

    // 检查 config 目录是否存在
    if (!appDir.exists("config")) {
        // 尝试创建 config 目录
        if (!appDir.mkdir("config")) {
            qCritical() << "无法创建配置目录:" << configDir;
            // 根据需求，可以选择返回空字符串、抛出异常或采取其他错误处理措施
            return QString();
        }
    }
    return configDir;
}

// 初始化全局图像队列和结果队列
void initializeGlobalQueues() {
    for(int i = 0; i < 2; ++i)
    {
        g_img_Queue[i] = new ThreadSafeQueue<ImageData>();
        g_result_Queue[i] = new ThreadSafeQueue<ImageData>();
    }
}

// 初始化线程控制变量和识别线程
void initializeGlobalThreads() {
    for(int i = 0; i < 2; ++i)
    {
        g_recognitionRunning[i] = new std::atomic<bool>(false);
        g_recognitionThread[i] = nullptr;
    }
}

// 初始化全局ONNXRunner实例数组
void initializeGlobalONNXRunner() {
    for(int i = 0; i < 2; ++i)
    {
        g_runner_array[i] = new ONNXRunner();
        std::string modelPath = (getConfigDirectory() + "/dimo_369_640.onnx").toStdString();
        qDebug() << modelPath;
        g_runner_array[i]->load(modelPath);
    }
}

// 清理全局图像队列和结果队列
void cleanupGlobalQueues() {
    for(int i = 0; i < 2; ++i)
    {
        if(g_img_Queue[i])
        {
            g_img_Queue[i]->stop();
            delete g_img_Queue[i];
            g_img_Queue[i] = nullptr;
        }

        if(g_result_Queue[i])
        {
            g_result_Queue[i]->stop();
            delete g_result_Queue[i];
            g_result_Queue[i] = nullptr;
        }
    }
}

// 清理线程控制变量和识别线程
void cleanupGlobalThreads() {
    for(int i = 0; i < 2; ++i)
    {
        if(g_recognitionRunning[i])
        {
            delete g_recognitionRunning[i];
            g_recognitionRunning[i] = nullptr;
        }

        if(g_recognitionThread[i])
        {
            if(g_recognitionThread[i]->joinable())
            {
                g_recognitionThread[i]->join();
            }
            delete g_recognitionThread[i];
            g_recognitionThread[i] = nullptr;
        }
    }
}

// 清理全局ONNXRunner实例数组
void cleanupGlobalONNXRunner() {
    for(int i = 0; i < 2; ++i)
    {
        if(g_runner_array[i])
        {
            delete g_runner_array[i];
            g_runner_array[i] = nullptr;
        }
    }
}

// 在程序初始化时调用
struct GlobalsInitializer {
    GlobalsInitializer() {
        initializeGlobalQueues();
        initializeGlobalThreads();
        initializeGlobalONNXRunner(); // 添加此行
    }

    ~GlobalsInitializer() {
        cleanupGlobalThreads();
        cleanupGlobalQueues();
        cleanupGlobalONNXRunner(); // 添加此行
    }
} globalsInitializerInstance; // 全局实例，确保在程序结束时清理




