#ifndef SQLTREEMODEL_H
#define SQLTREEMODEL_H

#include "../virtualsqltreenodeadapter/virtualsqltreenodeadapter.h"

#include "../virtualtreemodel/virtualtreemodel.h"

#include <QTreeView>

template <unsigned idColumnIndex, unsigned parentidColumnIndex,
          unsigned dataColumnIndex, typename DataType>
class SqlTreeModel : public VirtualTreeModel {
public:
private:
  using SqlTreeNode = SqlTreeNode<DataType>;
  using VirtualSqlTreeNodeAdapter = VirtualSqlTreeNodeAdapter<DataType>;
  using IdType = size_t;

private:
  QString m_tableName;
  SqlTreeNode m_root;
  std::map<IdType, SqlTreeNode *> m_nodes;
  VirtualSqlTreeNodeAdapter m_adapter;

public:
  explicit SqlTreeModel(QObject *parent = nullptr);
  explicit SqlTreeModel(QString tableName, QObject *parent = nullptr);
  ~SqlTreeModel();

public:
  SqlTreeNode *getNode(const QModelIndex &index);
  SqlTreeNode *getNode(IdType productTypeId);

public:
  QModelIndex insertProductType(int productTypeId, const QString &newTypeName);
  void selectFromTable(const QString &tableName);
  void specifyProductType(int productTypeId, const QString &newTypeName);
  void renameProductType(int productTypeId, const QString &newTypeName);

private:
  SqlTreeNode *currentNode();
};

#include "sqltreemodel.tpp"

#endif // SQLTREEMODEL_H
