/**
 * @file HistoryBar.cpp
 */

#include "HistoryBar.h"
#include "Hub.h"
#include "PlotGrid.h"
#include "Theme.h"

#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QDateTime>

namespace shq {

static double nowSec() {
    return QDateTime::currentMSecsSinceEpoch() / 1000.0;
}

HistoryBar::HistoryBar(Hub* hub, PlotGrid* grid, QWidget* parent)
    : QWidget(parent), hub_(hub), grid_(grid) {
    auto* lay = new QHBoxLayout(this);
    lay->setContentsMargins(6, 2, 6, 2);
    lay->setSpacing(4);

    auto* title = new QLabel("History", this);
    title->setStyleSheet("color:#fab387; font-weight:bold;");
    lay->addWidget(title);

    auto* liveBtn = new QPushButton("Live", this);
    connect(liveBtn, &QPushButton::clicked, this, [this]() {
        grid_->goLive();
        Q_EMIT liveRequested();
    });
    lay->addWidget(liveBtn);

    rangeLbl_ = new QLabel("No range", this);
    rangeLbl_->setStyleSheet("color:#a6adc8;");
    rangeLbl_->setMinimumWidth(170);
    lay->addWidget(rangeLbl_);

    auto* left = new QPushButton("\u25c0", this);   /* pan left */
    left->setMaximumWidth(32);
    connect(left, &QPushButton::clicked, this, [this]() { grid_->panAll(-0.75); });
    lay->addWidget(left);
    auto* right = new QPushButton("\u25b6", this);  /* pan right */
    right->setMaximumWidth(32);
    connect(right, &QPushButton::clicked, this, [this]() { grid_->panAll(0.75); });
    lay->addWidget(right);

    /* Jump presets. */
    static const double kJumpSec[]   = {10, 30, 60, 300, 600, 1800, 3600};
    static const char*  kJumpLabel[] = {"10s","30s","1m","5m","10m","30m","1h"};
    for (int j = 0; j < 7; j++) {
        double sec = kJumpSec[j];
        auto* b = new QPushButton(kJumpLabel[j], this);
        b->setMaximumWidth(40);
        connect(b, &QPushButton::clicked, this, [this, sec]() {
            grid_->jumpAllAgo(sec);
        });
        lay->addWidget(b);
    }

    auto* allBtn = new QPushButton("All", this);
    allBtn->setMaximumWidth(40);
    connect(allBtn, &QPushButton::clicked, this, &HistoryBar::showAll);
    lay->addWidget(allBtn);

    lay->addStretch(1);
}

void HistoryBar::showAll() {
    const auto& hi = hub_->historyInfo();
    double t0 = 1e300, t1 = -1e300;
    for (const auto& hs : hi.signals) {
        if (hs.t0 < t0) { t0 = hs.t0; }
        if (hs.t1 > t1) { t1 = hs.t1; }
    }
    if (t1 > t0) { grid_->setAllStoredX(t0, t1); }
}

void HistoryBar::updateReadout() {
    double t0, t1;
    if (!grid_->currentRange(t0, t1)) {
        rangeLbl_->setText("Live");
        return;
    }
    double span = t1 - t0;
    double ago  = nowSec() - t1;
    QString s;
    if (ago < 1.0) {
        s = QString("%1 s span | now").arg(span, 0, 'g', 3);
    } else if (ago < 60.0) {
        s = QString("%1 s span | %2 s ago").arg(span, 0, 'g', 3).arg(ago, 0, 'f', 0);
    } else if (ago < 3600.0) {
        s = QString("%1 s span | %2 min ago").arg(span, 0, 'g', 3).arg(ago / 60.0, 0, 'f', 1);
    } else {
        s = QString("%1 s span | %2 h ago").arg(span, 0, 'g', 3).arg(ago / 3600.0, 0, 'f', 1);
    }
    rangeLbl_->setText(s);
}

} /* namespace shq */
