/*
   SPDX-FileCopyrightText: 1999-2001 Lubos Lunak <l.lunak@kde.org>
   SPDX-FileCopyrightText: 2008 Michael Jansen <kde@michael-jansen.biz>

   SPDX-License-Identifier: LGPL-2.0-only
*/

#include "action_data/action_data.h"
#include "triggers/triggers.h"
#include "windows_handler.h"

#include <KConfigGroup>
#include <KGlobalAccel>
#include <QDebug>

#include <QAction>

#include "shortcuts_handler.h"

namespace KHotKeys
{
ShortcutTriggerVisitor::~ShortcutTriggerVisitor()
{
}

ShortcutTrigger::ShortcutTrigger(ActionData *data_P, const QKeySequence &shortcut, const QUuid &uuid)
    : Trigger(data_P)
    , _uuid(uuid)
    , _active(false)
    , _shortcut(shortcut)
{
}

ShortcutTrigger::~ShortcutTrigger()
{
    keyboard_handler->removeAction(_uuid.toString());
}

void ShortcutTrigger::accept(TriggerVisitor &visitor)
{
    if (ShortcutTriggerVisitor *v = dynamic_cast<ShortcutTriggerVisitor *>(&visitor)) {
        v->visit(*this);
    } else {
        qDebug() << "Visitor error";
    }
}

void ShortcutTrigger::aboutToBeErased()
{
    disable();
}

void ShortcutTrigger::activate(bool newState)
{
#ifdef KHOTKEYS_TRACE
    qDebug() << "new:" << newState << "old:" << _active;
#endif
    // If there is no change in state just return.
    if (newState == _active)
        return;

    _active = newState;

    if (_active) {
        QString name = data ? data->name() : "TODO";

        // FIXME: The following workaround tries to prevent having two actions with
        // the same uuid. That happens wile exporting/importing actions. The uuid
        // is exported too.
        QAction *act = keyboard_handler->addAction(_uuid.toString(), name, _shortcut);
        // addAction can change the uuid. That's why we store the uuid from the
        // action
        _uuid = act->objectName();

        connect(act, SIGNAL(triggered(bool)), this, SLOT(trigger()));
    } else {
        // Disable the trigger. Delete the action.
        QAction *action = keyboard_handler->getAction(_uuid.toString());
        if (action) {
            // In case the shortcut was changed from the kcm.
            auto shortcuts = KGlobalAccel::self()->shortcut(action);
            if (!shortcuts.isEmpty()) {
                _shortcut = QKeySequence(shortcuts.first());
            } else {
                _shortcut = QKeySequence();
            }
            keyboard_handler->removeAction(_uuid.toString());
        }
    }
}

QString ShortcutTrigger::shortcuts() const
{
    const auto shortcuts = shortcut();
    QString shortcutString;
    for (auto it = shortcuts.constBegin(); it != shortcuts.constEnd(); ++it) {
        if (it != shortcuts.constBegin()) {
            shortcutString.append(QStringLiteral(";"));
        }
        shortcutString.append((*it).toString());
    }
    return shortcutString;
}

void ShortcutTrigger::cfg_write(KConfigGroup &cfg_P) const
{
    base::cfg_write(cfg_P);
    cfg_P.writeEntry("Key", shortcuts());
    cfg_P.writeEntry("Type", "SHORTCUT"); // overwrites value set in base::cfg_write()
    cfg_P.writeEntry("Uuid", _uuid.toString());
}

ShortcutTrigger *ShortcutTrigger::copy(ActionData *data_P) const
{
    return new ShortcutTrigger(data_P ? data_P : data, shortcut().first(), QUuid::createUuid());
}

const QString ShortcutTrigger::description() const
{
    return i18n("Shortcut trigger: ") + shortcuts();
}

void ShortcutTrigger::disable()
{
    activate(false);

    // Unregister the shortcut with kglobalaccel
    QAction *action = keyboard_handler->getAction(_uuid.toString());
    if (action) {
        // In case the shortcut was changed from the kcm.
        auto shortcuts = KGlobalAccel::self()->shortcut(action);
        if (!shortcuts.isEmpty()) {
            _shortcut = QKeySequence(shortcuts.first());
        } else {
            _shortcut = QKeySequence();
        }

        // Unregister the global shortcut.
        KGlobalAccel::self()->removeAllShortcuts(action);
        keyboard_handler->removeAction(_uuid.toString());
    }
}

void ShortcutTrigger::enable()
{
    // To enable the shortcut we have to just register it once with
    // kglobalaccel and deactivate it immediately
    activate(true);
    activate(false);
}

void ShortcutTrigger::set_key_sequence(const QKeySequence &seq)
{
    // Get the action from the keyboard handler
    QAction *action = keyboard_handler->getAction(_uuid.toString());
    if (!action) {
        _shortcut = seq;
    } else {
        KGlobalAccel::self()->setShortcut(action, QList<QKeySequence>() << seq, KGlobalAccel::KGlobalAccel::NoAutoloading);
    }
}

QList<QKeySequence> ShortcutTrigger::shortcut() const
{
    // Get the action from the keyboard handler
    QAction *action = keyboard_handler->getAction(_uuid.toString());
    if (!action) {
        // Not active!
        return {_shortcut};
    }

    return KGlobalAccel::self()->shortcut(action);
}

QString ShortcutTrigger::primaryShortcut() const
{
    const auto shortcuts = shortcut();
    if (!shortcuts.isEmpty()) {
        return shortcuts.first().toString();
    } else {
        return QString();
    }
}

void ShortcutTrigger::trigger()
{
    if (khotkeys_active()) {
        windows_handler->set_action_window(0); // use active window
        data->execute();
    }
}

} // namespace KHotKeys
