#pragma once

#include <QDialog>
#include <QTreeWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include "db/dao/ruledao.h"
#include "ruleditordialog.h"

class RulesManagerDialog : public QDialog
{
    Q_OBJECT

public:
    explicit RulesManagerDialog(qint64 accountId = -1, QWidget *parent = nullptr);

signals:
    void rulesChanged();

private slots:
    void onNewRule();
    void onEditRule(QTreeWidgetItem* item);
    void onDeleteRule(QTreeWidgetItem* item);
    void onToggleEnabled(QTreeWidgetItem* item);
    void onRunRules();
    void onImportExport(bool import);
    void onRuleSaved(const Rule& rule);
    void refresh();

private:
    qint64 m_accountId;
    QTreeWidget* m_tree;
    QPushButton* m_btnNew;
    QPushButton* m_btnRun;
    QPushButton* m_btnImport;
    QPushButton* m_btnExport;
    QPushButton* m_btnClose;

    void setupUI();
    void loadRules();
    void addRuleToTree(const Rule& rule);
    Rule itemToRule(QTreeWidgetItem* item) const;
};