#include <QGuiApplication>
#include <QInputMethod>
#include <QQmlApplicationEngine>
#include <QDateTime>

#include "./interactive/InteractiveCAN.h"
#include "./interactive/InteractiveData.h"
#include "./interactive/InteractiveFace.h"
#include "./usr/LotoQmlPlugin.h"
#include "./usr/config.h"

int main(int argc, char **argv)
{
    // 设置日志格式：时间 [类型] 消息
    qSetMessagePattern("[%{time yyyy-MM-dd hh:mm:ss.zzz}] [%{type}] %{message}");
    
    QGuiApplication app(argc, argv);

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

    // 先加载配置（含 tenant_id），若 config.xml 存在则覆盖默认值
    Config()->configRead();
    qDebug() << "[Config] tenant_id =" << Config()->tenant_id << "httpHost =" << Config()->httpHost;

    InteractiveCAN::instance();

    QQmlApplicationEngine engine;

    // 注册C++类/对象到QML
    LotoQmlTypes::registerTypes();

    // 系统启动时清空 token（未登录状态）
    GetInteractiveData()->clearToken();

    // 人脸采集 ImageProvider，供 QML 中 Image source="image://InteractiveFaceImage/xxx" 使用
    engine.addImageProvider(QStringLiteral("InteractiveFaceImage"), GetInteractiveFace());

    const QUrl url = QUrl(QStringLiteral("qrc:/qml/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();
}
