/*
    SPDX-FileCopyrightText: 2014 David Edmundson <davidedmundson@kde.org>
    SPDX-FileCopyrightText: 2020 David Redondo <kde@david-redondo.de>
    SPDX-FileCopyrightText: 2024 Arjen Hiemstra <ahiemstra@heimr.nl>

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

#ifndef KEYSEQUENCEVALIDATOR_H
#define KEYSEQUENCEVALIDATOR_H

#include <QKeySequence>
#include <QObject>
#include <QQuickItem>

#include "enums.h"

class KeySequenceForeign
{
    Q_GADGET
    QML_ANONYMOUS
    QML_FOREIGN(QKeySequence)
};

class KeySequenceValidator : public QObject
{
    Q_OBJECT
    QML_ELEMENT
    Q_PROPERTY(QKeySequence currentKeySequence READ currentKeySequence WRITE setCurrentKeySequence NOTIFY currentKeySequenceChanged)
    Q_PROPERTY(int validateTypes READ validateTypes WRITE setValidateTypes NOTIFY validateTypesChanged)

public:
    /**
     * Constructor.
     */
    explicit KeySequenceValidator(QObject *parent = nullptr);

    QKeySequence currentKeySequence() const;
    void setCurrentKeySequence(const QKeySequence &sequence);
    Q_SIGNAL void currentKeySequenceChanged();

    int validateTypes() const;
    void setValidateTypes(int types);
    Q_SIGNAL void validateTypesChanged();

    Q_INVOKABLE void validateSequence(const QKeySequence &keySequence);
    Q_INVOKABLE void accept();
    Q_INVOKABLE void reject();

    Q_SIGNAL void error(const QString &title, const QString &message);
    Q_SIGNAL void question(const QString &title, const QString &message);
    Q_SIGNAL void finished(const QKeySequence &keySequence);

private:
    enum class ValidationResult {
        Reject,
        Accept,
        QuestionPending,
    };

    ValidationResult validateGlobalShortcut(const QKeySequence &keySequence);
    ValidationResult validateStandardShortcut(const QKeySequence &keySequence);

    KeySequenceEnums::ShortcutTypes m_validateTypes = KeySequenceEnums::GlobalShortcuts | KeySequenceEnums::StandardShortcuts;
    QKeySequence m_currentKeySequence;
    QKeySequence m_pendingKeySequence;
    bool m_validateGlobalPending = false;
};

#endif // KEYSEQUENCEVALIDATOR_H
