#ifndef TEST_QGSETTINGS_H
#define TEST_QGSETTINGS_H

#include <QByteArray>
#include <QHash>
#include <QObject>
#include <QStringList>
#include <QVariant>

class QGSettings : public QObject
{
    Q_OBJECT
public:
    explicit QGSettings(const QByteArray &schema, QObject *parent = nullptr)
        : QObject(parent),
          m_schema(QString::fromLatin1(schema))
    {
    }

    static bool isSchemaInstalled(const QByteArray &schema)
    {
        return s_installedSchemas.contains(QString::fromLatin1(schema));
    }

    static void installSchema(const QByteArray &schema, const QStringList &keys = {})
    {
        const QString schemaName = QString::fromLatin1(schema);
        s_installedSchemas.insert(schemaName, true);
        s_keys.insert(schemaName, keys);
    }

    static void uninstallSchema(const QByteArray &schema)
    {
        const QString schemaName = QString::fromLatin1(schema);
        s_installedSchemas.remove(schemaName);
        s_keys.remove(schemaName);
        s_values.remove(schemaName);
    }

    static void resetTestData()
    {
        s_installedSchemas.clear();
        s_keys.clear();
        s_values.clear();
    }

    static void setValue(const QByteArray &schema, const QString &key, const QVariant &value)
    {
        s_values[QString::fromLatin1(schema)].insert(key, value);
    }

    QStringList keys() const
    {
        return s_keys.value(m_schema);
    }

    QVariant get(const QString &key) const
    {
        return s_values.value(m_schema).value(key);
    }

    void set(const QString &key, const QVariant &value)
    {
        s_values[m_schema].insert(key, value);
        Q_EMIT changed(key);
    }

Q_SIGNALS:
    void changed(const QString &key);

private:
    QString m_schema;
    inline static QHash<QString, bool> s_installedSchemas;
    inline static QHash<QString, QStringList> s_keys;
    inline static QHash<QString, QHash<QString, QVariant>> s_values;
};

#endif // TEST_QGSETTINGS_H
