#ifndef DATABASEVIEW_H
#define DATABASEVIEW_H

#include <QDialog>
#include <QWidget>
#include <QSqlQueryModel>
#include <QTableView>
#include <QListWidget>
#include <QComboBox>
#include <QLineEdit>
#include "database.h"

class SelectColumnsDialog : public QDialog
{
    Q_OBJECT
    QListWidget *m_listWidget;
public:
    SelectColumnsDialog(QWidget *parent = nullptr);
    void setColumns(QStringList columns);
    QStringList selectedColumns();
};

class FITSFileModel : public QSqlQueryModel
{
    Q_OBJECT
    QStringList m_columns;
    QString m_sort;
    QStringList m_key;
    QStringList m_value;
public:
    FITSFileModel(QObject *parent = nullptr);
    void sort(int column, Qt::SortOrder order);
    void setColumns(const QStringList &columns);
    void setFilter(const QStringList &key, const QStringList &value);
protected:
    void prepareQuery();
};

class DataBaseView : public QWidget
{
    Q_OBJECT
    Database *m_database;
    QTableView *m_tableView;
    FITSFileModel *m_model;
    QComboBox *m_filterKeyword[3];
    QLineEdit *m_search[3];
public:
    explicit DataBaseView(Database *database, QWidget *parent = nullptr);
    ~DataBaseView();
public slots:
    void selectColumns();
    void loadDatabase();
    void itemActivated(const QModelIndex &index);
    void applyFilter();
signals:
    void loadFile(QString file);
};

#endif // DATABASEVIEW_H
