#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <qqml.h>
#include "user/config.h"
#include "user/lock.h"
#include "user/rfid.h"
#include "user/pro.h"
#include "user/httpclient.h"
#include "user/finger.h"
#include "user/logoutput.h"
#include "user/timeout.h"
#include <QLoggingCategory>

#include "Interactive/InteractiveCore.h"
#include "Interactive/InteractiveWork.h"
#include "Interactive/InteractiveData.h"
#include "Interactive/InteractiveFace.h"
#include "Interactive/InteractiveHand.h"

#include <windows.h>

config *pconfig = new config();


int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);


    if(false == pconfig->configRead())
    {
        pconfig->configWrite();
    }

    // qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));

    // 日志模块初始化(将QDebug重定向到文件)
    if(pconfig->logfileStat == true)
    {
        LogOutput::getInstance()->install();
    }

    // 注册 Worker 类到 QML
    qmlRegisterType<lock>("com.lock", 1, 0, "Lock");
    qmlRegisterType<rfid>("com.rfid", 1, 0, "Rfid");
    qmlRegisterType<pro>("com.pro", 1, 0, "Pro");
    qmlRegisterType<httpClient>("com.httpclient", 1, 0, "Httpclient");
    qmlRegisterType<finger>("com.finger", 1, 0, "Finger");
    qmlRegisterType<timeout>("com.timeout", 1, 0, "Timeout");



    qmlRegisterType<InteractiveTask>("com.InteractiveTask", 1, 0, "InteractiveTask");   // 将任务操作类注册到qml
    // qmlRegisterType<InteractiveCore>("com.InteractiveCore", 1, 0, "InteractiveCore");
    // qmlRegisterType<InteractiveData>("com.InteractiveData", 1, 0, "InteractiveData");
    // qmlRegisterType<InteractiveFace>("com.InteractiveFace", 1, 0, "InteractiveFace");


    // 创建全局单例 数据缓存
    InteractiveCore *pSingletonCore = SINGLETON_CREATE(InteractiveCore)();  // 核心模块
    InteractiveWork *pSingletonWork = SINGLETON_CREATE(InteractiveWork)();  // 工作模块
    InteractiveData *pSingletonData = SINGLETON_CREATE(InteractiveData)();  // 数据模块
    InteractiveFace *pSingletonFace = SINGLETON_CREATE(InteractiveFace)();  // 人脸模块
    InteractiveHand *pSingletonHand = SINGLETON_CREATE(InteractiveHand)();  // 指纹模块

    QQmlApplicationEngine engine;

    // 注册单例类到qml
    engine.rootContext()->setContextProperty("interactiveCore", pSingletonCore);
    engine.rootContext()->setContextProperty("interactiveData", pSingletonData);
    engine.rootContext()->setContextProperty("interactiveFace", pSingletonFace);
    engine.rootContext()->setContextProperty("interactiveHand", pSingletonHand);

    // 注册图像地址(人脸/指纹)到qml
    engine.addImageProvider(INTERACTIVE_FACE_IMAGE_URL, pSingletonFace);
    engine.addImageProvider(INTERACTIVE_HAND_IMAGE_URL, pSingletonHand);

    // 注册模型到qml
    engine.rootContext()->setContextProperty("materialreplace_modelMaterialType", &pSingletonData->m_modelMaterialType);
    engine.rootContext()->setContextProperty("materialreplace_modelMaterialInfo", &pSingletonData->m_modelMaterialInfo);
    engine.rootContext()->setContextProperty("materialreplace_modelMaterialManualReplace", &pSingletonData->m_modelMaterialManualReplace);
    engine.rootContext()->setContextProperty("materialexception_modelMaterialException", &pSingletonData->m_modelMaterialException);

    engine.rootContext()->setContextProperty("appDir",
                                             QString("file:///") +
                                                 qApp->applicationDirPath());
    engine.rootContext()->setContextProperty("pConfig", pconfig);
    const QUrl url(QStringLiteral("qrc:/Cabinet/main.qml"));
    QObject::connect(
        &engine,
        &QQmlApplicationEngine::objectCreated,
        &app,
        [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        },
        Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

