/*
 * Copyright 2013 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "contenthubplugin.h"
#include "../../../src/com/lomiri/content/debug.h"

#include "contenthandler.h"
#include "contenthub.h"
#include "contenticonprovider.h"
#include "contentitem.h"
#include "contentpeer.h"
#include "contentpeermodel.h"
#include "contentscope.h"
#include "contentstore.h"
#include "contenttransfer.h"
#include "contenttype.h"

#include <qqml.h>
#include <QQmlContext>
#include <QQmlEngine>
#include <QDebug>
#include <QIcon>

#include <mutex>

/*!
 * \brief qml_content_hub function to unstatinate the ContentHub as a singleton in QML
 */
static QObject *qml_content_hub(QQmlEngine *engine, QJSEngine *scriptEngine)
{
    Q_UNUSED(engine)
    Q_UNUSED(scriptEngine)
    TRACE() << Q_FUNC_INFO;
    return ContentHub::instance();
}

/*!
 * \reimp
 */
void ContentHubPlugin::initializeEngine(QQmlEngine * engine, const char * uri)
{
    Q_UNUSED(uri)
    TRACE() << Q_FUNC_INFO;

    static std::once_flag initOnceFlag;

    std::call_once(initOnceFlag, [&engine]() {
        QIcon::setThemeName("suru");
        QIcon::setThemeSearchPaths(QStringList() << ("/usr/share/icons/"));
        ContentIconProvider *iconProvider = ContentIconProvider::instance();
        engine->addImageProvider("lomiri-content-hub", iconProvider);
    });
}

/*!
 * \reimp
 */
void ContentHubPlugin::registerTypes(const char *uri)
{
    TRACE() << Q_FUNC_INFO;
#ifdef ENABLE_UBUNTU_COMPAT
    Q_ASSERT(uri == QLatin1String("Lomiri.Content") || uri == QLatin1String("Ubuntu.Content"));
#else
    Q_ASSERT(uri == QLatin1String("Lomiri.Content"));
#endif

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QVector<QPair<int, int>> versions;
    versions.append(QPair<int, int>(0, 1));
    versions.append(QPair<int, int>(1, 0));
    versions.append(QPair<int, int>(1, 1));
    versions.append(QPair<int, int>(1, 3));

    // Register common types for version 0.1, 1.0, 1.1 and 1.3
    for(int i = 0; i < versions.length(); i++) {
        int versionMajor = versions[i].first;
        int versionMinor = versions[i].second;

        qmlRegisterUncreatableType<ContentHandler>(uri, versionMajor, versionMinor, "ContentHandler", "Not creatable as an object, use only to retrieve handler enums (e.g. ContentHandler.Source)");
        qmlRegisterSingletonType<ContentHub>(uri, versionMajor, versionMinor, "ContentHub", qml_content_hub);
        qmlRegisterType<ContentItem>(uri, versionMajor, versionMinor, "ContentItem");
        qmlRegisterType<ContentPeer>(uri, versionMajor, versionMinor, "ContentPeer");
        qmlRegisterType<ContentPeerModel>(uri, versionMajor, versionMinor, "ContentPeerModel");
        qmlRegisterType<ContentScope>(uri, versionMajor, versionMinor, "ContentScope");
        qmlRegisterType<ContentStore>(uri, versionMajor, versionMinor, "ContentStore");
        qmlRegisterUncreatableType<ContentTransfer>(uri, versionMajor, versionMinor, "ContentTransfer", "Not creatable as an object, please use ContentPeer::request()");
        qmlRegisterUncreatableType<ContentType>(uri, versionMajor, versionMinor, "ContentType", "Use only the type");
    }

    // added copy method to ContentItem
    qmlRegisterType<ContentItem>(uri, 1, 4, "ContentItem");
#else
    qmlRegisterUncreatableType<ContentHandler>(uri, 2, 0, "ContentHandler", "Not creatable as an object, use only to retrieve handler enums (e.g. ContentHandler.Source)");
    qmlRegisterSingletonType<ContentHub>(uri, 2, 0, "ContentHub", qml_content_hub);
    qmlRegisterType<ContentItem>(uri, 2, 0, "ContentItem");
    qmlRegisterType<ContentPeer>(uri, 2, 0, "ContentPeer");
    qmlRegisterType<ContentPeerModel>(uri, 2, 0, "ContentPeerModel");
    qmlRegisterType<ContentScope>(uri, 2, 0, "ContentScope");
    qmlRegisterType<ContentStore>(uri, 2, 0, "ContentStore");
    qmlRegisterUncreatableType<ContentTransfer>(uri, 2, 0, "ContentTransfer", "Not creatable as an object, please use ContentPeer::request()");
    qmlRegisterUncreatableType<ContentType>(uri, 2, 0, "ContentType", "Use only the type");
    qmlRegisterType<ContentItem>(uri, 2, 0, "ContentItem");
#endif
}
