#ifndef DATABASE_H
#define DATABASE_H

#include <QFuture>
#include <QObject>
#include <QQmlEngine>
#include <QThreadPool>
#include <QtConcurrent>

class Database : public QObject
{
    Q_OBJECT
    QML_SINGLETON
    QML_ELEMENT

public:
    static Database &getInstance();

    static Database *create(QQmlEngine *qmlEngine, QJSEngine *);

    Q_INVOKABLE QFuture<void> open(const QString &user,
                                   const QString &password,
                                   const QString &hostName,
                                   bool usePort,
                                   int portNumber,
                                   const QString &databaseName,
                                   const QString &connectOptions);
    Q_INVOKABLE QFuture<void> close();

    template<class Function>
    static auto query(Function &&f)
    {
        return QtConcurrent::run(&getInstance().m_pool, f);
    }

signals:
    void closed();
    void errorOcurred(const QString &errorMessage);
    void opened();

private:
    explicit Database(QObject *parent = nullptr);
    Q_DISABLE_COPY_MOVE(Database)

    QThreadPool m_pool;
};

#endif // DATABASE_H
