#ifndef PRODUCTSWIDGET_H
#define PRODUCTSWIDGET_H

#include "../databaseinfo/databaseinfo.h"

#include <QSqlQueryModel>

#include <QLabel>

#include <QTreeView>

#include <QTableView>

#include <QPushButton>

template <unsigned idColumnIndex, unsigned parentidColumnIndex,
          unsigned dataColumnIndex, typename DataType>
class SqlTreeModel;

class ProductsWidget : public QWidget {
  Q_OBJECT

private:
  using SqlTreeModel = SqlTreeModel<INDEX(COLUMN::ProductTypes_Id),
                                    INDEX(COLUMN::ProductTypes_ParentId),
                                    INDEX(COLUMN::ProductTypes_Name), QString>;

private:
  // tree
  QLabel *m_treeLabel;
  QTreeView *m_treeView;
  SqlTreeModel *m_treeModel;
  QWidget *m_treeWidget;
  QPushButton *m_expandAll;
  QPushButton *m_collapseAll;
  QPushButton *m_deleteProductType;
  QPushButton *m_renameProductType;
  QPushButton *m_addProductType;

  // table
  QLabel *m_tableLabel;
  QTableView *m_tableView;
  QSqlQueryModel *m_tableModel;
  QWidget *m_tableWidget;
  QPushButton *m_showAll;
  QPushButton *m_deleteProduct;
  QPushButton *m_renameProduct;
  QPushButton *m_editProduct;
  QPushButton *m_addProduct;

private:
  void createTreeWidget();
  void createTableWidget();
  void createLayout();
  void configureConnections();
  void configureDisplayedData();

private:
  QModelIndex treeSelectedItem();

public:
  explicit ProductsWidget(QWidget *parent = nullptr);
  ~ProductsWidget();

private slots:
  // product types
  void showDeleteProductTypeDialogWindow();
  void showRenameProductTypeDialogWindow();
  void showAddProductTypeDialogWindow();
  void showSameGroup(const QModelIndex &treeModelIndex);
  void updateTreePushButtonsPropertyEnabled();
  void selectProductType(const QModelIndex &tableModelIndex);
  // products
  void showAllProducts();
  void showRenameProductDialogWindow();
  void showEditProductDialogWindow();
  void showDeleteProductDialogWindow();
  void showAddProductDialogWindow();
  void refreshTableModel();
  void refreshTableView();
  void updateTablePushButtonsPropertyEnabled();

signals:
  void changedTableWidgetModel();
};

#endif // PRODUCTSWIDGET_H
