#pragma once

#include "source.h"
#include <QDockWidget>
#include <memory>
#include <vector>

class QTreeWidget;
class QTreeWidgetItem;

/**
 * Left-side dock widget: signal tree.
 *
 * Each Source is a top-level tree item; each signal is a child.
 * Signals can be dragged onto PlotWidget drop targets.
 *
 * Drag data (MIME type "application/x-signal"):
 *   2 x int32_t: src_idx, sig_idx
 */
class Sidebar : public QDockWidget {
    Q_OBJECT
public:
    explicit Sidebar(QWidget* parent = nullptr);

    void addSource(std::shared_ptr<Source> src, int srcIdx);
    void refresh();   // called each timer tick to update connected/count state

    bool eventFilter(QObject* obj, QEvent* ev) override;

signals:
    void addSourceRequested();

private slots:
    void onStartDrag(Qt::DropActions);

private:
    QTreeWidget* tree_ = nullptr;

    struct SourceEntry {
        std::shared_ptr<Source> src;
        int srcIdx;
        QTreeWidgetItem* item;
    };
    std::vector<SourceEntry> entries_;
};
