#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QApplication>
#include<QMessageBox>
#include <QSettings>

#include "TestService.h"
#include "Standard.h"
#include "Calibrationpara.h"
#include "backendlogic.h"

#include "DLog.h"

#if 0
#define AUTO_RUN_KEY	"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"
//设置/取消自启动
//isStart: true(开机启动)    false(开机不启动)
void ServiceMediaPlayer::setMyAppAutoRun(bool isStart)
{
    QString application_name = QApplication::applicationName();//获取应用名称
    QSettings *settings = new QSettings(AUTO_RUN_KEY, QSettings::NativeFormat);//创建QSetting, 需要添加QSetting头文件
    if(isStart)
    {
        QString application_path = QApplication::applicationFilePath();//找到应用的目录
        settings->setValue(application_name, application_path.replace("/", "\\"));//写入注册表
    }
    else
    {
        settings->remove(application_name);		//从注册表中删除
    }
}

#endif

void SetProcessAutoRunSelf(const QString &appPath)
{
    //注册表路径需要使用双反斜杠，如果是32位系统，要使用QSettings::Registry32Format
    QSettings settings("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
                       QSettings::Registry64Format);

    //以程序名称作为注册表中的键
    //根据键获取对应的值（程序路径）
    QFileInfo fInfo(appPath);
    QString name = fInfo.baseName();
    QString path = settings.value(name).toString();

    //如果注册表中的路径和当前程序路径不一样，
    //则表示没有设置自启动或自启动程序已经更换了路径
    //toNativeSeparators的意思是将"/"替换为"\"
    QString newPath = QDir::toNativeSeparators(appPath);
    if (path != newPath)
    {

        settings.setValue(name, newPath);
    }
}


QObject *standardmananger_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
{
    Q_UNUSED(engine)
    Q_UNUSED(scriptEngine)
    return StandardManager::instance();
}

QObject *calibrationpara_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
{
    Q_UNUSED(engine)
    Q_UNUSED(scriptEngine)
    return CalibrationPara::instance();
}


int main(int argc, char *argv[])
{
    qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
    QApplication app(argc, argv);

#ifdef QT_NO_DEBUG
    SetProcessAutoRunSelf(qApp->applicationFilePath());//创建程序自启注册表
    qInstallMessageHandler(myMsgOutput);
#endif
    DLog_Init();

    //QMessageBox::critical(NULL, "critical", "Content", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);


    qmlRegisterSingletonType<StandardManager>("TService", 1, 0, "StandardManager",standardmananger_provider);
    qmlRegisterSingletonType<StandardManager>("TService", 1, 0, "CalibrationPara",calibrationpara_provider);

    qmlRegisterType<TestService>("TService",1,0,"TestService");
    qmlRegisterType<BackendLogic>("BackendLogic", 1, 0, "BackendLogic");

    StandardManager::instance();

    QQmlApplicationEngine engine;
    engine.setOfflineStoragePath("D:/VaporRecoverySystemTest/");  //设置数据库存储路径
    //engine.rootContext()->setContextProperty("TService",new TestService);
    const QUrl url(QStringLiteral("qrc:/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();
}
