/*
    SPDX-FileCopyrightText: 2008 Michael Jansen <kde@michael-jansen.biz>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "component.h"

#include "globalshortcutcontext.h"
#include "globalshortcutsregistry.h"
#include "kglobalaccel_interface.h"
#include "logging.h"
#include <config-kglobalaccel.h>

#include <QKeySequence>
#include <QStringList>

#if HAVE_X11
#include <private/qtx11extras_p.h>
#endif

QSet<QKeySequence> Component::keysFromString(const QString &str)
{
    QSet<QKeySequence> ret;
    if (str == QLatin1String("none")) {
        return ret;
    }
    const QStringList strList = str.split(QLatin1Char('\t'));
    for (const QString &s : strList) {
        QKeySequence key = QKeySequence::fromString(s, QKeySequence::PortableText);
        ret.insert(key);
    }
    return ret;
}

QString Component::stringFromKeys(const QSet<QKeySequence> &keys)
{
    if (keys.isEmpty()) {
        return QStringLiteral("none");
    }

    QList<QKeySequence> sortedKeys(keys.begin(), keys.end());
    std::sort(sortedKeys.begin(), sortedKeys.end());

    if (keys == QSet{QKeySequence()}) {
        return QStringLiteral("none");
    }

    QString ret;
    for (const QKeySequence &key : sortedKeys) {
        ret.append(key.toString(QKeySequence::PortableText));
        ret.append(QLatin1Char('\t'));
    }
    ret.chop(1);
    return ret;
}

Component::Component(const QString &uniqueName, const QString &friendlyName, GlobalShortcutsRegistry *registry)
    : _uniqueName(uniqueName)
    , _friendlyName(friendlyName)
    , _registry(registry)
{
    // Make sure we do no get uniquenames still containing the context
    Q_ASSERT(uniqueName.indexOf(QLatin1Char('|')) == -1);

    const QString DEFAULT(QStringLiteral("default"));
    createGlobalShortcutContext(DEFAULT, QStringLiteral("Default Context"));
    _current = _contexts.value(DEFAULT);
}

Component::~Component()
{
    // We delete all shortcuts from all contexts
    qDeleteAll(_contexts);
}

bool Component::activateGlobalShortcutContext(const QString &uniqueName)
{
    if (!_contexts.value(uniqueName)) {
        createGlobalShortcutContext(uniqueName, QStringLiteral("TODO4"));
        return false;
    }

    // Deactivate the current contexts shortcuts
    deactivateShortcuts();

    // Switch the context
    _current = _contexts.value(uniqueName);

    return true;
}

void Component::activateShortcuts()
{
    for (GlobalShortcut *shortcut : std::as_const(_current->_actionsMap)) {
        shortcut->setActive();
    }
}

QList<GlobalShortcut *> Component::allShortcuts(const QString &contextName) const
{
    GlobalShortcutContext *context = _contexts.value(contextName);
    return context ? context->_actionsMap.values() : QList<GlobalShortcut *>{};
}

QList<KGlobalShortcutInfo> Component::allShortcutInfos(const QString &contextName) const
{
    GlobalShortcutContext *context = _contexts.value(contextName);
    return context ? context->allShortcutInfos() : QList<KGlobalShortcutInfo>{};
}

bool Component::cleanUp()
{
    bool changed = false;

    const auto actions = _current->_actionsMap;
    for (GlobalShortcut *shortcut : actions) {
        qCDebug(KGLOBALACCELD) << _current->_actionsMap.size();
        if (!shortcut->isPresent()) {
            changed = true;
            shortcut->unRegister();
        }
    }

    if (changed) {
        _registry->writeSettings();
        // We could be destroyed after this call!
    }

    return changed;
}

bool Component::createGlobalShortcutContext(const QString &uniqueName, const QString &friendlyName)
{
    if (_contexts.value(uniqueName)) {
        qCDebug(KGLOBALACCELD) << "Shortcut Context " << uniqueName << "already exists for component " << _uniqueName;
        return false;
    }
    _contexts.insert(uniqueName, new GlobalShortcutContext(uniqueName, friendlyName, this));
    return true;
}

GlobalShortcutContext *Component::currentContext()
{
    return _current;
}

QDBusObjectPath Component::dbusPath() const
{
    auto isNonAscii = [](QChar ch) {
        const char c = ch.unicode();
        const bool isAscii = c == '_' //
            || (c >= 'A' && c <= 'Z') //
            || (c >= 'a' && c <= 'z') //
            || (c >= '0' && c <= '9');
        return !isAscii;
    };

    QString dbusPath = _uniqueName;
    // DBus path can only contain ASCII characters, any non-alphanumeric char should
    // be turned into '_'
    std::replace_if(dbusPath.begin(), dbusPath.end(), isNonAscii, QLatin1Char('_'));

    // QDBusObjectPath could be a little bit easier to handle :-)
    return QDBusObjectPath(_registry->dbusPath().path() + QLatin1String("component/") + dbusPath);
}

void Component::deactivateShortcuts(bool temporarily)
{
    for (GlobalShortcut *shortcut : std::as_const(_current->_actionsMap)) {
        if (temporarily //
            && _uniqueName == QLatin1String("kwin") //
            && shortcut->uniqueName() == QLatin1String("Block Global Shortcuts")) {
            continue;
        }
        shortcut->setInactive();
    }
}

void Component::emitGlobalShortcutEvent(const GlobalShortcut &shortcut, ShortcutKeyState state)
{
#if HAVE_X11
    // pass X11 timestamp
    const long timestamp = QX11Info::appTime();
#else
    const long timestamp = 0;
#endif

    if (shortcut.context()->component() != this) {
        return;
    }

    switch (state) {
    case ShortcutKeyState::Pressed:
        Q_EMIT globalShortcutPressed(shortcut.context()->component()->uniqueName(), shortcut.uniqueName(), timestamp);
        break;
    case ShortcutKeyState::Repeated:
        Q_EMIT globalShortcutRepeated(shortcut.context()->component()->uniqueName(), shortcut.uniqueName(), timestamp);
        break;
    case ShortcutKeyState::Released:
        Q_EMIT globalShortcutReleased(shortcut.context()->component()->uniqueName(), shortcut.uniqueName(), timestamp);
        break;
    }
}

void Component::invokeShortcut(const QString &shortcutName, const QString &context)
{
    GlobalShortcut *shortcut = getShortcutByName(shortcutName, context);
    if (shortcut) {
        emitGlobalShortcutEvent(*shortcut, ShortcutKeyState::Pressed);
    }
}

QString Component::friendlyName() const
{
    return !_friendlyName.isEmpty() ? _friendlyName : _uniqueName;
}

QList<GlobalShortcut *> Component::getShortcutsByKey(const QKeySequence &key, KGlobalAccel::MatchType type) const
{
    QList<GlobalShortcut *> rc;
    for (GlobalShortcutContext *context : std::as_const(_contexts)) {
        rc += context->getShortcutsByKey(key, type);
    }
    return rc;
}

GlobalShortcut *Component::getShortcutByName(const QString &uniqueName, const QString &context) const
{
    const GlobalShortcutContext *shortcutContext = _contexts.value(context);
    return shortcutContext ? shortcutContext->_actionsMap.value(uniqueName) : nullptr;
}

QStringList Component::getShortcutContexts() const
{
    return _contexts.keys();
}

bool Component::isActive() const
{
    // The component is active if at least one of it's global shortcuts is
    // present.
    for (GlobalShortcut *shortcut : std::as_const(_current->_actionsMap)) {
        if (shortcut->isPresent()) {
            return true;
        }
    }
    return false;
}

bool Component::isShortcutAvailable(const QKeySequence &key, const QString &component, const QString &context) const
{
    qCDebug(KGLOBALACCELD) << key.toString() << component;

    // if this component asks for the key. only check the keys in the same
    // context
    if (component == uniqueName()) {
        return shortcutContext(context)->isShortcutAvailable(key);
    } else {
        for (auto it = _contexts.cbegin(), endIt = _contexts.cend(); it != endIt; ++it) {
            const GlobalShortcutContext *ctx = it.value();
            if (!ctx->isShortcutAvailable(key)) {
                return false;
            }
        }
    }
    return true;
}

GlobalShortcut *Component::registerShortcut(const QString &uniqueName,
                                            const QString &friendlyName,
                                            const QString &shortcutString,
                                            const QString &defaultShortcutString,
                                            uint64_t serial)
{
    if (!serial) {
        serial = _registry->nextSerial();
        Q_EMIT _registry->needsSave();
    }

    GlobalShortcut *shortcut = new GlobalShortcut(uniqueName, friendlyName, serial, currentContext(), _registry);
    shortcut->setKeys(keysFromString(shortcutString));
    shortcut->setDefaultKeys(keysFromString(defaultShortcutString));
    shortcut->setIsFresh(false);
    return shortcut;
}

void Component::loadSettings(const KConfigGroup &configGroup, const KConfigGroup &stateGroup)
{
    // GlobalShortcutsRegistry::loadSettings handles contexts.
    const auto listKeys = configGroup.keyList();
    for (const QString &confKey : listKeys) {
        const QStringList entry = configGroup.readEntry(confKey, QStringList());
        if (entry.size() != 3) {
            continue;
        }

        const uint64_t serial = stateGroup.readEntry<uint64_t>(confKey, 0);

        registerShortcut(confKey, entry[2], entry[0], entry[1], serial);
    }
}

void Component::setFriendlyName(const QString &name)
{
    _friendlyName = name;
}

GlobalShortcutContext *Component::shortcutContext(const QString &contextName)
{
    return _contexts.value(contextName);
}

GlobalShortcutContext const *Component::shortcutContext(const QString &contextName) const
{
    return _contexts.value(contextName);
}

QStringList Component::shortcutNames(const QString &contextName) const
{
    const GlobalShortcutContext *context = _contexts.value(contextName);
    return context ? context->_actionsMap.keys() : QStringList{};
}

QString Component::uniqueName() const
{
    return _uniqueName;
}

void Component::unregisterShortcut(const QString &uniqueName)
{
    // Now wrote all contexts
    for (GlobalShortcutContext *context : std::as_const(_contexts)) {
        if (context->_actionsMap.value(uniqueName)) {
            delete context->takeShortcut(context->_actionsMap.value(uniqueName));
        }
    }
}

void Component::writeSettings(KConfigGroup &configGroup, KConfigGroup &stateGroup) const
{
    // If we don't delete the current content global shortcut
    // registrations will never not deleted after forgetGlobalShortcut()
    configGroup.deleteGroup();
    stateGroup.deleteGroup();

    // Now write all contexts
    for (GlobalShortcutContext *context : std::as_const(_contexts)) {
        KConfigGroup contextGroup;

        if (context->uniqueName() == QLatin1String("default")) {
            contextGroup = configGroup;
            // Write the friendly name
            contextGroup.writeEntry("_k_friendly_name", friendlyName());
        } else {
            contextGroup = KConfigGroup(&configGroup, context->uniqueName());
            // Write the friendly name
            contextGroup.writeEntry("_k_friendly_name", context->friendlyName());
        }

        // qCDebug(KGLOBALACCELD) << "writing group " << _uniqueName << ":" << context->uniqueName();

        for (const GlobalShortcut *shortcut : std::as_const(context->_actionsMap)) {
            // qCDebug(KGLOBALACCELD) << "writing" << shortcut->uniqueName();

            // We do not write fresh shortcuts.
            // We do not write session shortcuts
            if (shortcut->isFresh() || shortcut->isSessionShortcut()) {
                continue;
            }
            // qCDebug(KGLOBALACCELD) << "really writing" << shortcut->uniqueName();

            QStringList entry(stringFromKeys(shortcut->keys()));
            entry.append(stringFromKeys(shortcut->defaultKeys()));
            entry.append(shortcut->friendlyName());

            contextGroup.writeEntry(shortcut->uniqueName(), entry);
            stateGroup.writeEntry(shortcut->uniqueName(), shortcut->serial());
        }
    }
}

#include "moc_component.cpp"
