#pragma once

#include "config.h"
#include "layout_picker.h"
#include "source.h"
#include "trigger_engine.h"
#include "trigger_toolbar.h"

#include <QMainWindow>
#include <QTimer>
#include <memory>
#include <utility>
#include <vector>

class Sidebar;
class PlotWidget;
class StatsPanel;
class QSplitter;
class QLabel;
class QAction;
class QComboBox;
class QPushButton;
class QToolBar;

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    explicit MainWindow(const AppConfig& cfg, QWidget* parent = nullptr);
    ~MainWindow() override;

    void addSource(const std::string& label,
                   const std::string& host, uint16_t port,
                   const std::string& multicast_group = "",
                   uint16_t data_port = 0);

    enum class Layout { L1x1, L1x2, L1x3, L1x4,
                        L2x1, L2x2, L3x1, L4x1 };

private slots:
    void onTimer();
    void onPauseToggled(bool paused);
    void onWindowSecChanged(double secs);
    void onLayoutChanged(int index);
    void onAddSource();
    void onTriggerArm();
    void onTriggerDisarm();
    void onCursorAPlaced(double t);
    void onCursorBPlaced(double t);
    void onAutoZoom();
    void onZoomBack();
    void onExportCsv();

private:
    void buildToolbar();
    void buildPlotGrid();
    void applyLayout(Layout lay);
    void refreshStatus();
    void applyStylesheet();

    AppConfig cfg_;

    std::vector<std::shared_ptr<Source>> sources_;

    // UI
    Sidebar*        sidebar_      = nullptr;
    StatsPanel*     statsPanel_   = nullptr;
    TriggerToolBar* triggerPanel_ = nullptr;
    QWidget*        plotGrid_     = nullptr;
    QWidget*        centralArea_  = nullptr;

    std::vector<PlotWidget*> plots_;

    // Toolbar controls
    QPushButton*  pauseBtn_    = nullptr;
    QComboBox*    windowCombo_ = nullptr;
    LayoutPicker* layoutPicker_= nullptr;
    QPushButton*  cursorABtn_  = nullptr;
    QPushButton*  cursorBBtn_  = nullptr;
    QLabel*       deltaLabel_  = nullptr;
    QPushButton*  autoBtn_     = nullptr;   // "Live" — visible only when zoomed
    QPushButton*  backBtn_     = nullptr;   // "← Back" — visible when history non-empty
    QPushButton*  exportBtn_   = nullptr;

    QLabel*  statsLabel_ = nullptr;
    QTimer*  timer_      = nullptr;

    // A/B cursor state (global, pushed to all plots)
    bool   cursorAActive_ = false;
    bool   cursorBActive_ = false;
    double cursorATime_   = 0.0;
    double cursorBTime_   = 0.0;

    bool   paused_    = false;
    double windowSec_ = 5.0;
    double viewTMax_  = 0.0;
    Layout layout_    = Layout::L1x1;

    // Zoom history: each entry is a (lo, hi) pair; {-1,-1} = rolling sentinel
    std::vector<std::pair<double,double>> zoomHistory_;
    bool zoomed_ = false;   // any plot is currently in user-zoomed state

    // Trigger engine
    TriggerEngine               trigger_;
    std::shared_ptr<Source>     triggerSrc_;
    int                         triggerSigIdx_   = -1;
    size_t                      triggerLastHead_ = 0;
    static constexpr size_t     kTriggerFeedPts  = 10'000;
    std::vector<double>         trigFeedT_, trigFeedV_;
};
