#if 0
Ikarus Technologies CONFIDENTIAL
Copyright (c) 2017 - 2018 Ikarus Technologies, All Rights Reserved.

NOTICE:
All information contained herein is, and remains the property of Ikarus Technologies. The intellectual
and technical concepts contained
herein are proprietary to Ikarus Technologies and may be covered by U.S. and Foreign Patents, patents in process,
       and are protected by trade secret or copyright law.
       Dissemination of this information
       or reproduction of this material is strictly forbidden unless prior written permission is obtained
       from Ikarus Technologies.  Access to the source code contained herein is hereby forbidden to anyone except current Ikarus
       Technologies employees, managers or contractors who have executed
       Confidentiality and Non - disclosure agreements explicitly covering such access.

       The copyright notice above does not evidence any actual or intended publication
       or disclosure  of  this source code, which includes
       information that is confidential and / or proprietary,
       and is a trade secret, of  Ikarus Technologies.   ANY REPRODUCTION, MODIFICATION, DISTRIBUTION, PUBLIC  PERFORMANCE,
       OR PUBLIC DISPLAY OF OR THROUGH USE  OF THIS  SOURCE CODE  WITHOUT  THE EXPRESS WRITTEN CONSENT OF Ikarus Technologies IS
       STRICTLY PROHIBITED, AND IN VIOLATION OF APPLICABLE
       LAWS AND INTERNATIONAL TREATIES.  THE RECEIPT OR POSSESSION OF  THIS SOURCE CODE AND / OR RELATED INFORMATION DOES NOT
       CONVEY OR IMPLY ANY RIGHTS
       TO REPRODUCE, DISCLOSE OR DISTRIBUTE ITS CONTENTS, OR TO MANUFACTURE, USE, OR SELL ANYTHING THAT IT  MAY DESCRIBE,
       IN WHOLE OR IN PART.
#endif

       // src/main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickStyle>
#include <QIcon>
#include <QDateTime>
#include <QMessageLogContext>
#include <QTextStream>
#include <QFile>

#include "devicediscovery.h"
#include "thememanager.h"
#include "imagedownloader.h"
#include "qmdns.h"

#include "stellarmatetools_version.h"

       // Custom message handler to add timestamps
       void customMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    QByteArray localMsg = msg.toLocal8Bit();
    QTextStream ts(stderr);
    QString timestamp = QDateTime::currentDateTime().toString("hh:mm:ss.zzz");

    switch (type)
    {
        case QtDebugMsg:
            ts << QString("[%1] DEBUG: %2 (%3:%4, %5)\n").arg(timestamp, localMsg.constData(), context.file,
                    QString::number(context.line), context.function);
            break;
        case QtInfoMsg:
            ts << QString("[%1] INFO: %2 (%3:%4, %5)\n").arg(timestamp, localMsg.constData(), context.file,
                    QString::number(context.line), context.function);
            break;
        case QtWarningMsg:
            ts << QString("[%1] WARNING: %2 (%3:%4, %5)\n").arg(timestamp, localMsg.constData(), context.file,
                    QString::number(context.line), context.function);
            break;
        case QtCriticalMsg:
            ts << QString("[%1] CRITICAL: %2 (%3:%4, %5)\n").arg(timestamp, localMsg.constData(), context.file,
                    QString::number(context.line), context.function);
            break;
        case QtFatalMsg:
            ts << QString("[%1] FATAL: %2 (%3:%4, %5)\n").arg(timestamp, localMsg.constData(), context.file,
                    QString::number(context.line), context.function);
            abort();
    }
}

int main(int argc, char *argv[])
{
    // Parse command-line arguments for verbose flag before installing message handler
    bool verboseMode = false;
    for (int i = 1; i < argc; ++i)
    {
        QString arg = QString::fromUtf8(argv[i]);
        if (arg == "--verbose" || arg == "-v")
        {
            verboseMode = true;
            break;
        }
    }

    // Enable verbose mode in qMDNS and DeviceDiscovery if requested
    if (verboseMode)
    {
        qMDNS::setVerbose(true);
        DeviceDiscovery::setVerbose(true);
    }

    qInstallMessageHandler(customMessageHandler); // Install custom message handler

    QGuiApplication app(argc, argv);

    // Set application icon
    app.setWindowIcon(QIcon(":/assets/logo.png"));

    // Set application properties
    app.setApplicationName("StellarMate Discovery");
    app.setApplicationVersion(STELLARMATETOOLS_VERSION_STRING); // Use version from CMake
    app.setOrganizationName("StellarMate");
    app.setOrganizationDomain("stellarmate.com");

    // Set QuickControls style
    QQuickStyle::setStyle("Basic");

    // Register QML types
    qmlRegisterType<DeviceDiscovery>("StellarMateTools", 1, 0, "DeviceDiscovery");
    qmlRegisterType<StellarMateDevice>("StellarMateTools", 1, 0, "StellarMateDevice");
    qmlRegisterType<ThemeManager>("StellarMateTools", 1, 0, "ThemeManager");
    qmlRegisterType<ImageDownloader>("StellarMateTools", 1, 0, "ImageDownloader");


    // Create theme manager
    ThemeManager themeManager;

    // Create device discovery manager
    DeviceDiscovery deviceDiscovery;

    QQmlApplicationEngine engine;

    // Expose C++ objects to QML
    engine.rootContext()->setContextProperty("deviceDiscovery", &deviceDiscovery);
    engine.rootContext()->setContextProperty("themeManager", &themeManager);
    engine.rootContext()->setContextProperty("appVersion", STELLARMATETOOLS_VERSION_STRING);

    const QUrl url(QStringLiteral("qrc:/qt/qml/StellarMateTools/qml/Main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject * obj, const QUrl & objUrl)
    {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);

    // Try to load the QML file
    engine.load(url);

    // If that fails, try loading from the module
    if (engine.rootObjects().isEmpty())
    {
        qDebug() << "Failed to load from resource, trying module import...";
        engine.loadFromModule("StellarMateTools", "Main");
    }

    // Start device discovery
    deviceDiscovery.startDiscovery();

    return app.exec();
}
