#ifndef SQLQUERYITEMDELEGATE_H
#define SQLQUERYITEMDELEGATE_H

#include "gui_global.h"
#include "sqlqueryitem.h"
#include <QStyledItemDelegate>
#include <QSet>

class FkComboBox;
class SqlQueryItem;
class QComboBox;
class QStandardItemModel;
class SqlQueryModel;
class SqlQueryView;

class GUI_API_EXPORT SqlQueryItemDelegate : public QStyledItemDelegate
{
        Q_OBJECT
    public:
        enum class CommitReason
        {
            EnterKey,
            Other
        };

        explicit SqlQueryItemDelegate(QObject *parent = 0);

        void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
        QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
        void destroyEditor(QWidget *editor, const QModelIndex &index) const override;
        QString	displayText(const QVariant & value, const QLocale & locale) const override;
        void setEditorData(QWidget * editor, const QModelIndex & index) const override;
        void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const override;
        void mouseLeftIndex(const QModelIndex& index);
        bool eventFilter(QObject* editor, QEvent* event) override;
        CommitReason getLastCommitReason() const;

        static void handleUncommitedPainting(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index);
        static SqlQueryItem* getItem(const QModelIndex &index);

    private:
        QWidget* getEditor(SqlQueryItem* item, int type, bool shouldSkipInitialSelection, QWidget* parent) const;
        QWidget* getFkEditor(SqlQueryItem* item, bool shouldSkipInitialSelection, QWidget* parent, const SqlQueryModel *model) const;
        void setEditorDataForLineEdit(QLineEdit* le, const QModelIndex& index) const;
        void setEditorDataForFk(QComboBox* cb, const QModelIndex& index) const;
        void setModelDataForFk(FkComboBox* editor, QAbstractItemModel* model, const QModelIndex& index) const;
        void setModelDataForLineEdit(QLineEdit* editor, QAbstractItemModel* model, const QModelIndex& index) const;
        qlonglong getRowCountForFkEditor(Db* db, const QString& query, bool *isError) const;
        int getFkViewHeaderWidth(SqlQueryView* fkView, bool includeScrollBar) const;

        QAction* resetValueAction = nullptr;
        mutable CommitReason lastCommitReason = CommitReason::Other;

        static bool warnedAboutHugeContents;
        static const int HUGE_CONTENTS_WARNING_LIMIT = 32767; // pow(2, 16) / 2 - 1
};

#endif // SQLQUERYITEMDELEGATE_H
