#include "qmoviedb.h"
#include "QNetworkReply"
#include "QDebug"

QMovieDB::QMovieDB(QString apiKey, QObject *parent)
    : QObject{parent}, netMan(new QNetworkAccessManager(this)) {
    movieDbApiKey = apiKey;
    QObject::connect(netMan, &QNetworkAccessManager::finished, this, &QMovieDB::receiveReply);
}

void QMovieDB::searchMovieTitle(const QString title)
{
    QNetworkRequest req;
    req.setRawHeader("Accept", "application/json");

    QString bearer = "Bearer " + movieDbApiKey;
    req.setRawHeader("Authorization", bearer.toUtf8());

    QByteArray URL = QUrl::toPercentEncoding(title);
    URL = "https://api.themoviedb.org/3/search/movie?query=" + URL + "&include_adult=false&language=en-US&page=1";

    req.setUrl(QUrl(URL));
    netMan->get(req);
}

void QMovieDB::receiveReply(QNetworkReply *reply)
{
    qInfo() << reply->readAll();
}
