#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QMediaInfo.h>
#include <QHandbrake.h>
#include "qmoviedb.h"

#include <QFile>
#include <QQmlContext>
#include <QSettings>


int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QCoreApplication::setOrganizationName("abissen");
    QCoreApplication::setApplicationName("DoViGUI");
    QSettings settings;


    QMediaInfo mInfo(&app);

    if (QString::compare(settings.value("MovieDB API Key").toString(), "", Qt::CaseInsensitive) == 0) {
        qWarning() << "Set MovieDB API key in settings file.";
        settings.setValue("MovieDB API Key", "");
    }
    QMovieDB movieDB(settings.value("MovieDB API Key").toString());

    //movieDB.searchMovieTitle("TheWitcher"); //No Result Example
    //movieDB.searchMovieTitle("10 cloverfield lane"); //Single Result Example
    //movieDB.searchMovieTitle("The Shawshank Redemption"); //Multi-result

    QHandbrake handbrake;
    #ifdef linux
    if (handbrake.setPath("/usr/bin/HandBrakeCLI")) {
        qInfo() << "Handbrake executable set";
    } else {
        qWarning() << "Handbrake executable not found.";
    }
    #else
    if (handbrake.setPath(settings.value("HandbrakeExe_Path").toString())) {
        qInfo() << "Handbrake executable set";
    }
    else {
        qWarning() << "Handbrake executable not found.";
    }
    #endif

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("mediaInfo", &mInfo);
    engine.rootContext()->setContextProperty("handbrake", &handbrake);
    engine.rootContext()->setContextProperty("movieDB", &movieDB);

    const QUrl url(u"qrc:/DoViGUI/Main.qml"_qs);
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
        &app, []() { QCoreApplication::exit(-1); },
        Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}
