#include "Experiment.h"
#include "InkCanvas.h"
#include "epfb.h"

#include <QTimer>
#include <QRect>

Experiment::Experiment(QObject *parent) : QObject(parent)
{
    // Sweep screenMode Pen(0) and Mono(1) across contentType 0..5, flags 0.
    for (int sm : {0, 1})
        for (int ct = 0; ct < 6; ++ct)
            m_combos.append({sm, ct, 0});
}

void Experiment::start()
{
    auto *t = new QTimer(this);
    connect(t, &QTimer::timeout, this, &Experiment::tick);
    t->start(3000);
    QTimer::singleShot(600, this, &Experiment::tick);
}

void Experiment::tick()
{
    if (!m_canvas || m_combos.isEmpty())
        return;
    m_idx = (m_idx + 1) % m_combos.size();
    const Combo c = m_combos[m_idx];

    m_canvas->drawTestStroke();
    m_status = QStringLiteral("[%1/%2]  screen=%3  content=%4  flags=%5")
                   .arg(m_idx + 1).arg(m_combos.size()).arg(c.sm).arg(c.ct).arg(c.flags);
    emit statusChanged();
    qInfo("EXPERIMENT %s", qPrintable(m_status));

    // After the scene composites the test pattern, force a refresh of just the
    // test region with these parameters.
    QTimer::singleShot(350, this, [this, c]() {
        const QRect r = m_canvas->testRectQ();
        void *fb = epfb_instance();
        if (fb)
            epfb_swapBuffers(fb, {r.left(), r.top(), r.right(), r.bottom()}, c.ct, c.sm, c.flags);
    });
}
