#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("10 cloverfield lane"); //Single Result Example
    movieDB.searchMovieTitle("The Shawshank Redemption"); //Multi-result

//    //Check if HDR/DolbyVision
//    enum QMediaInfo::HDR_Type hdrType = mInfo.getHDRType(__T("Cosmos.mkv"));
//    if (hdrType >= 0) {
//        qInfo() << hdrType;
//    } else {
//        qWarning() << "Could not open file.";
//    }

    QHandbrake handbrake;
    if (handbrake.setPath("J:\\Video\\VideoTools\\HandBrakeCLI.exe") ==1 ) {
        qInfo() << "Success";
        //handbrake.startEncode();
    }
    else if (handbrake.setPath("/usr/bin/HandBrakeCLI")) {
        qInfo() << "Succes Linux";
    }
    else {
        qInfo() << "fail";
    }


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

    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();
}
