#pragma once

#include <QDialog>
#include <QTreeWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include "db/dao/templatedao.h"
#include "templateeditordialog.h"

class TemplatesManagerDialog : public QDialog
{
    Q_OBJECT

public:
    explicit TemplatesManagerDialog(qint64 accountId = -1, QWidget *parent = nullptr);
    Template getSelectedTemplate() const { return m_selectedTemplate; }

signals:
    void templateSelected(const Template& tmpl);
    void templatesChanged();

private slots:
    void onNewTemplate();
    void onEditTemplate(QTreeWidgetItem* item);
    void onDeleteTemplate(QTreeWidgetItem* item);
    void onToggleDefault(QTreeWidgetItem* item);
    void onImportExport(bool import);
    void onTemplateSaved(const Template& tmpl);
    void refresh();

private:
    qint64 m_accountId;
    Template m_selectedTemplate;
    QTreeWidget* m_tree;
    QPushButton* m_btnNew;
    QPushButton* m_btnUse;
    QPushButton* m_btnImport;
    QPushButton* m_btnExport;
    QPushButton* m_btnClose;
    
    void setupUI();
    void loadTemplates();
    void addTemplateToTree(const Template& tmpl);
    Template itemToTemplate(QTreeWidgetItem* item) const;
};