/***************************************************************
 * Copyright (C) 2020, 2026, Val Doroshchuk <valbok@gmail.com> *
 *                                                             *
 * This file is part of QtAVPlayer.                            *
 * Free Qt Media Player based on FFmpeg.                       *
 ***************************************************************/

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

#include "playercontroller.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    app.setApplicationName("QtAVPlayer QML Demo");
    app.setApplicationVersion("1.0");

    qmlRegisterType<SubtitleItem>(
        "Subtitles",
        1,
        0,
        "SubtitleItem");
    QQmlApplicationEngine engine;

    // Register PlayerController so QML can instantiate it or we expose it via context
    PlayerController controller;
    engine.rootContext()->setContextProperty("playerController", &controller);

    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);
    auto rootObject = engine.rootObjects().first();
    auto subtitleItem = rootObject->findChild<SubtitleItem *>(QString::fromLatin1("subtitleImage"));
    controller.setSubtitleItem(subtitleItem);
    return app.exec();
}
