/*
 * This file is part of LibKGAPI library
 *
 * SPDX-FileCopyrightText: 2013 Daniel Vrátil <dvratil@redhat.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
 */

#include "contactsgroupcreatejob.h"
#include "account.h"
#include "contactsgroup.h"
#include "contactsservice.h"
#include "debug.h"
#include "private/queuehelper_p.h"
#include "utils.h"

#include <QNetworkReply>
#include <QNetworkRequest>

using namespace KGAPI2;

class Q_DECL_HIDDEN ContactsGroupCreateJob::Private
{
public:
    QueueHelper<ContactsGroupPtr> groups;
};

ContactsGroupCreateJob::ContactsGroupCreateJob(const ContactsGroupsList &groups, const AccountPtr &account, QObject *parent)
    : CreateJob(account, parent)
    , d(new Private)
{
    d->groups = groups;
}

ContactsGroupCreateJob::ContactsGroupCreateJob(const ContactsGroupPtr &contactsGroup, const AccountPtr &account, QObject *parent)
    : CreateJob(account, parent)
    , d(new Private)
{
    d->groups << contactsGroup;
}

ContactsGroupCreateJob::~ContactsGroupCreateJob()
{
    delete d;
}

void ContactsGroupCreateJob::start()
{
    if (d->groups.atEnd()) {
        emitFinished();
        return;
    }

    const ContactsGroupPtr contact = d->groups.current();
    const QUrl url = ContactsService::createGroupUrl(account()->accountName());
    QNetworkRequest request(url);
    request.setRawHeader("GData-Version", ContactsService::APIVersion().toLatin1());

    QByteArray rawData = ContactsService::contactsGroupToXML(contact);
    rawData.prepend(
        "<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\" "
        "xmlns:gd=\"http://schemas.google.com/g/2005\" "
        "xmlns:gContact=\"http://schemas.google.com/contact/2008\">"
        "<atom:category scheme=\"http://schemas.google.com/g/2005#kind\" "
        "term=\"http://schemas.google.com/g/2008#group\"/>");
    rawData.append("</atom:entry>");

    QStringList headers;
    auto rawHeaderList = request.rawHeaderList();
    headers.reserve(rawHeaderList.size());
    for (const QByteArray &str : std::as_const(rawHeaderList)) {
        headers << QLatin1String(str) + QLatin1String(": ") + QLatin1String(request.rawHeader(str));
    }

    enqueueRequest(request, rawData, QStringLiteral("application/atom+xml"));
}

ObjectsList ContactsGroupCreateJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
{
    const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
    ContentType ct = Utils::stringToContentType(contentType);
    ObjectsList items;
    if (ct == KGAPI2::JSON) {
        items << ContactsService::JSONToContactsGroup(rawData);
        d->groups.currentProcessed();
    } else if (ct == KGAPI2::XML) {
        items << ContactsService::XMLToContactsGroup(rawData);
        d->groups.currentProcessed();
    } else {
        setError(KGAPI2::InvalidResponse);
        setErrorString(tr("Invalid response content type"));
        emitFinished();
    }

    // Enqueue next item or finish
    start();

    return items;
}
