/*
 * Use QDBus to launch the application
 *
 * Deepin system's application in /usr/share/applications/xxx.desktop has
 * read and write permission, you can't launch it even if you're root.
 * There is a right method to launch application through DBus system.
 *
 */
#include "deepinlauncher.h"
#include "oscallcontext.h"

#include <QFileInfo>
#include <QDebug>
#include <QDBusPendingCall>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QRegularExpression>
#include <QDir>
#include <DStandardPaths>
#include <QDBusObjectPath>

UOSAI_USE_NAMESPACE

DeepinLauncher::DeepinLauncher(const QStringList &deskPaths, QObject *parent) : QObject(parent)
    , m_defaultDesktopPaths(deskPaths)
{
    // Launcher interface
    m_oslauncher.reset(
        new QDBusInterface(
            deepinLancherService,
            deepinLancherPath,
            deepinLancherInterface,
            QDBusConnection::sessionBus(), this));
    m_oslauncher->setTimeout(m_callTimeout);

    // Start manager interface
    m_osStartManager.reset(
        new QDBusInterface(
            deepinStartManagerService,
            deepinStartManagerPath,
            deepinStartManagerInterface,
            QDBusConnection::sessionBus(), this));
    m_osStartManager->setTimeout(m_callTimeout);

    // Mime interface
    m_osMime.reset(
        new QDBusInterface(
            deepinMimeService,
            deepinMimePath,
            deepinMimeInterface,
            QDBusConnection::sessionBus(), this));
    m_osMime->setTimeout(m_callTimeout);
}

DeepinLauncher::~DeepinLauncher()
{
}

int DeepinLauncher::showLauncher()
{
    /* V23:
     *  qdbus --literal org.deepin.dde.Launcher1
     *      /org/deepin/dde/Launcher1
     *      org.deepin.dde.Launcher1.Show
     *
     * Other:
     *  qdbus --literal com.deepin.dde.Launcher
     *      /com/deepin/dde/Launcher
     *      com.deepin.dde.Launcher.Show
     * */


    int errorCode = OSCallContext::NonError;

    if (m_oslauncher->isValid()) {
        QList<QVariant> argumentList;
        auto reply = m_oslauncher->asyncCallWithArgumentList(
                         QStringLiteral("Show"), argumentList);

        reply.waitForFinished();

        if (reply.isError()) {
            qCritical() << "Show call failed:"
                        << reply.error();

            if (QDBusError::UnknownMethod == reply.error().type()) {
                errorCode = OSCallContext::NotImpl;
            } else {
                errorCode = OSCallContext::NonService;
            }
        }
    } else {
        errorCode = OSCallContext::NonService;
    }

    return errorCode;
}

int DeepinLauncher::launchDesktop(const QString &pathApp)
{
    int errorCode = OSCallContext::NonError;
    QFileInfo appFile(pathApp);
    //Query the desktop file paths if only supply
    //xxx.desktop file
    if (!appFile.isAbsolute()) {
        QString deskPath = getAppDesktopFile(pathApp);
        appFile.setFile(deskPath);
    }

    if (!appFile.exists()) {
        qCritical() << "Target file does not exist: " << pathApp;
        errorCode = OSCallContext::AppNotFound;
        return errorCode;
    }

    if (appFile.suffix() != "desktop") {
        qCritical() << "Target file's is not *.desktop: " << pathApp;
        errorCode = OSCallContext::AppNotFound;
        return errorCode;
    }

    /*
     * V23:
     *  qdbus --literal org.deepin.dde.StartManager1 /
     *      org/deepin/dde/StartManager1
     *      org.deepin.dde.StartManager1.Launch "<abs path>/xxx.desktop"
     *
     * Other:
     *  qdbus --literal com.deepin.SessionManager
     *      /com/deepin/StartManager
     *      com.deepin.StartManager.Launch "<abs path>/xxx.desktop"
     * */
    //Try launch app use v23's new application manager
#ifdef COMPILE_ON_V23
    QScopedPointer<QDBusInterface> appAM;
    QString appPath;
    QString appId = getAppIdFromAbsolutePath(pathApp);
    appPath = escapeToObjectPath(appId);
    /* V23 new AM:
     *
     * qdbus --literal org.desktopspec.ApplicationManager1
     *      /org/desktopspec/ApplicationManager1/<appPath>
     *      org.desktopspec.ApplicationManager1.Application.Launch
     *       "", [], {}
     *
     **/
    appAM.reset(new QDBusInterface(
                       deepinStartManagerService,
                       QString(deepinStartManagerPath) + QString("/%1").arg(appPath),
                       deepinStartManagerInterface,
                       QDBusConnection::sessionBus(), this));
    if (appAM->isValid()) {
        QList<QVariant> argumentList;
        argumentList << QVariant::fromValue<QString>(QString(""))
                     << QVariant::fromValue<QStringList>(QStringList(""))
                     << QVariant::fromValue<QVariantMap>(QVariantMap());

        auto reply = appAM->asyncCallWithArgumentList(
                         QStringLiteral("Launch"), argumentList);
        reply.waitForFinished();

        if (reply.isError()) {
            qCritical() << "Failed to lauch app:" << pathApp
                        << " Error message: " << reply.error();
            errorCode = OSCallContext::AppStartFailed;
        }

    } else {
        errorCode = OSCallContext::NonService;
    }
    //If launch ok with new AM, just exit.
    // else try to use old start manager.
    if (errorCode == OSCallContext::NonError) {
        return errorCode;
    }
#endif

    if (m_osStartManager->isValid()) {
        QList<QVariant> argumentList;
        argumentList << QVariant::fromValue<QString>(appFile.filePath());

        auto reply = m_osStartManager->asyncCallWithArgumentList(
                         QStringLiteral("Launch"), argumentList);

        reply.waitForFinished();

        if (reply.isError()) {
            qCritical() << "Launch call failed:" << appFile.filePath()
                        << " " << reply.error();

            if (QDBusError::UnknownMethod == reply.error().type()) {
                errorCode = OSCallContext::NotImpl;
            } else {
                errorCode = OSCallContext::NonService;
            }
        } else {
            bool success = reply.reply().arguments().at(0).toBool();
            if (!success) {
                qCritical() << "Start the " << pathApp
                            << " failed, Status: "
                            << success;
                errorCode = OSCallContext::AppStartFailed;
            }
        }
    } else {
        qCritical() << "m_osStartManager is invalid!";
        errorCode = OSCallContext::NonService;
    }
    return errorCode;
}

QString DeepinLauncher::getAppDesktopFile(const QString &DesktopName)
{
    QString appDesktopFile;

    foreach (auto path, m_defaultDesktopPaths) {
        QFileInfo f(path + "/" + DesktopName);

        if (f.isFile() && f.exists()) {
            appDesktopFile = f.filePath();
            break;
        }
    }

    return appDesktopFile;
}

int DeepinLauncher::launchDefault(const QString &mineType)
{
    int errorCode = OSCallContext::NonError;

#ifdef COMPILE_ON_V23
    QScopedPointer<QDBusInterface> appMime;
    appMime.reset(
        new QDBusInterface(
            deepinNewMimeService,
            deepinNewMimePath,
            deepinNewMimeInterface,
            QDBusConnection::sessionBus(), this));
    if (appMime->isValid()) {
        QList<QVariant> argumentList;
        argumentList << QVariant::fromValue<QString>(mineType);
        auto reply = appMime->asyncCallWithArgumentList(
                         QStringLiteral("queryDefaultApplication"), argumentList);
        reply.waitForFinished();
        if (reply.isError()) {
            qCritical() << "Failed (NewMIME) to get default apps:" << mineType
                        << " Error message: " << reply.error();
        } else {
            QString mimeType = reply.reply().arguments().value(0).toString();
            QString defaultAppPath = reply.reply().arguments().value(1).value<QDBusObjectPath>().path();
            QScopedPointer<QDBusInterface> appAM;
            appAM.reset(new QDBusInterface(
                               deepinStartManagerService,
                               defaultAppPath,
                               deepinStartManagerInterface,
                               QDBusConnection::sessionBus(), this));
            if (appAM->isValid()) {
                QList<QVariant> argumentList;
                argumentList << QVariant::fromValue<QString>(QString(""))
                             << QVariant::fromValue<QStringList>(QStringList(""))
                             << QVariant::fromValue<QVariantMap>(QVariantMap());

                auto reply = appAM->asyncCallWithArgumentList(
                                 QStringLiteral("Launch"), argumentList);
                reply.waitForFinished();
                if (reply.isError()) {
                    qCritical() << "Failed to lauch app:" << defaultAppPath
                                << " Error message: " << reply.error();
                    errorCode = OSCallContext::AppStartFailed;
                }
            } else {
                errorCode = OSCallContext::NonService;
            }
            //If launch ok with new AM, just exit.
            // else try to use old start manager.
            if (errorCode == OSCallContext::NonError) {
                return errorCode;
            }
        }
    } else {
        qWarning() << "V23 new application service isn't avaiable.";
    }
#endif
    if (m_osMime->isValid()) {
        QList<QVariant> argumentList;
        argumentList << QVariant::fromValue<QString>(mineType);
        auto reply = m_osMime->asyncCallWithArgumentList(
                         QStringLiteral("GetDefaultApp"), argumentList);
        reply.waitForFinished();
        if (reply.isError()) {
            qCritical() << "Failed to get default apps:" << mineType
                        << " Error message: " << reply.error();

            if (QDBusError::UnknownMethod == reply.error().type()) {
                errorCode = OSCallContext::NotImpl;
            } else {
                errorCode = OSCallContext::NonService;
            }
        } else {
            QString defaultAppInfo = reply.reply().arguments().at(0).toString();

            QJsonParseError jsonError;
            QJsonDocument jsonApp = QJsonDocument::fromJson(
                                        defaultAppInfo.toUtf8(), &jsonError);
            QJsonObject defaultAppObj = jsonApp.object();

            if (jsonApp.isNull() || !jsonApp.isObject()) {
                qCritical() << "Parse default app json error:" << defaultAppInfo;
                errorCode = OSCallContext::AppStartFailed;
            } else {
                QString strDefaultApp = defaultAppObj["Id"].toString();

                errorCode = launchDesktop(strDefaultApp);
            }
        }
    } else {
        qCritical() << "m_osMime is invalid!";
        errorCode = OSCallContext::NonService;
    }

    return errorCode;
}

int DeepinLauncher::listApps(const QString &mime)
{
    int count = 0;

    int errorCode = OSCallContext::NonError;

    if (m_osMime->isValid()) {
        QList<QVariant> argumentList;
        argumentList << QVariant::fromValue<QString>(mime);
        auto reply = m_osMime->asyncCallWithArgumentList(
                         QStringLiteral("ListApps"), argumentList);

        reply.waitForFinished();

        if (reply.isError()) {
            qCritical() << "Failed to get default apps list:" << mime
                        << " Error message: " << reply.error();

            if (QDBusError::UnknownMethod == reply.error().type()) {
                errorCode = OSCallContext::NotImpl;
            } else {
                errorCode = OSCallContext::NonService;
            }
        } else {
            QString defaultAppInfos = reply.reply().arguments().at(0).toString();

            QJsonParseError jsonError;
            QJsonDocument jsonApp = QJsonDocument::fromJson(
                                        defaultAppInfos.toUtf8(), &jsonError);
            QJsonArray defaultAppArr = jsonApp.array();

            if (jsonApp.isNull() || !jsonApp.isArray()) {
                qCritical() << "Parse default app json error:" << defaultAppInfos;
                errorCode = OSCallContext::AppStartFailed;
            } else {
                count = defaultAppArr.size();
            }
        }
    } else {
        errorCode = OSCallContext::NonService;
    }

    Q_UNUSED(errorCode);

    return count;
}

QString DeepinLauncher::escapeToObjectPath(const QString &str)
{
    if (str.isEmpty()) {
        return "_";
    }

    auto ret = str;
    QRegularExpression re{R"([^a-zA-Z0-9])"};
    auto matcher = re.globalMatch(ret);
    while (matcher.hasNext()) {
        auto replaceList = matcher.next().capturedTexts();
        replaceList.removeDuplicates();
        for (const auto &c : replaceList) {
            auto hexStr = QString::number(static_cast<uint>(c.front().toLatin1()), 16);
            ret.replace(c, QString{R"(_%1)"}.arg(hexStr));
        }
    }
    return ret;
}

QString DeepinLauncher::unescapeFromObjectPath(const QString &str)
{
    auto ret = str;
    for (int i = 0; i < str.size(); ++i) {
        if (str[i] == '_' and i + 2 < str.size()) {
            auto hexStr = str.mid(i + 1, 2);
            ret.replace(QString{"_%1"}.arg(hexStr), QChar::fromLatin1(hexStr.toUInt(nullptr, 16)));
            i += 2;
        }
    }
    return ret;
}

QString DeepinLauncher::getAppIdFromAbsolutePath(const QString &path)
{
    static QString desktopSuffix{u8".desktop"};
    const auto &appDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
    if (!path.endsWith(desktopSuffix) or
    !std::any_of(appDirs.cbegin(), appDirs.constEnd(), [&path](const QString & dir) { return path.startsWith(dir); })) {
        return {};
    }

    auto tmp = path.chopped(desktopSuffix.size());
#if QT_VERSION < QT_VERSION_CHECK(5,14,0)
    auto components = tmp.split(QDir::separator(), QString::SkipEmptyParts);
#else
    auto components = tmp.split(QDir::separator(), Qt::SkipEmptyParts);
#endif
    auto location = std::find(components.cbegin(), components.cend(), "applications");
    if (location == components.cend()) {
        return {};
    }

    QStringList tmpLocation;
    for (auto it = location + 1; it != components.cend(); ++it)
        tmpLocation << *it;

    auto appId = tmpLocation.join('-');
    return appId;
}
