#ifndef SQLITEEXTENSIONEDITOR_H
#define SQLITEEXTENSIONEDITOR_H

#include "gui_global.h"
#include "icon.h"
#include "mdichild.h"
#include <QItemSelection>
#include <QWidget>

namespace Ui {
    class SqliteExtensionEditor;
}

class QToolBar;
class SqliteExtensionEditorModel;
class QSortFilterProxyModel;
class SelectableDbModel;
class Db;
class LazyTrigger;

CFG_KEY_LIST(SqliteExtensionEditor, QObject::tr("A SQLite extension editor window"),
    CFG_KEY_ENTRY(COMMIT,     QKeySequence::Save,        QObject::tr("Commit the pending changes"))
    CFG_KEY_ENTRY(ROLLBACK,   QKeySequence::Cancel,      QObject::tr("Rollback the pending changes"))
)

class GUI_API_EXPORT SqliteExtensionEditor : public MdiChild
{
        Q_OBJECT

    public:
        enum Action
        {
            COMMIT,
            ROLLBACK,
            ADD,
            DELETE,
            IMPORT,
            EXPORT,
            HELP
        };
        Q_ENUM(Action)

        enum ToolBar
        {
            TOOLBAR
        };

        explicit SqliteExtensionEditor(QWidget *parent = nullptr);
        ~SqliteExtensionEditor();

        bool restoreSessionNextTime() override;
        bool isUncommitted() const override;
        QString getQuitUncommittedConfirmMessage() const override;

    protected:
        QVariant saveSession() override;
        bool restoreSession(const QVariant &sessionValue) override;
        Icon* getIconNameForMdiWindow() override;
        QString getTitleForMdiWindow() override;
        void createActions() override;
        void setupDefShortcuts() override;
        QToolBar* getToolBar(int toolbar) const override;

    private:
        void init();
        QModelIndex getCurrentExtensionIndex() const;
        QModelIndex extRowToSrc(const QModelIndex& idx) const;
        void extensionDeselected(const QModelIndex& idx);
        void extensionSelected(const QModelIndex& idx);
        void clearEdits();
        void selectExtension(const QModelIndex& idx);
        QStringList getCurrentDatabases() const;
        bool tryToLoad(const QString& filePath, const QString& initFunc, QString* resultError);
        bool validateExtension(bool* fileOk = nullptr,
                               bool* initOk = nullptr,
                               QString* fileError = nullptr);
        bool validateExtension(const QModelIndex& idx);
        bool validateCurrentExtension();
        bool validateExtension(const QString& filePath,
                               const QString& initFunc,
                               bool* fileOk = nullptr,
                               bool* initOk = nullptr,
                               QString* fileError = nullptr);
        void initStateForAll();

        Ui::SqliteExtensionEditor *ui;
        SqliteExtensionEditorModel* dataModel = nullptr;
        QSortFilterProxyModel* viewModel = nullptr;
        SelectableDbModel* dbListModel = nullptr;
        bool currentModified = false;
        bool updatesForSelection = false;
        Db* probingDb = nullptr;
        LazyTrigger* statusUpdateTrigger = nullptr;
        bool nameGenerationActive = true;

    private slots:
        void help();
        void commit();
        void rollback();
        void newExtension();
        void deleteExtension();
        void updateState();
        void updateCurrentExtensionState();
        void extensionSelected(const QItemSelection& selected, const QItemSelection& deselected);
        void updateModified();
        void generateName();
        void applyFilter(const QString& value);
        void browseForFile();
        void cfgExtensionListChanged();
        void importExtensions();
        void exportExtensions();
};

#endif // SQLITEEXTENSIONEDITOR_H
