//
// Created by Lenn on 2025/12/16.
//

#include "backend.h"
#include "data_backend.h"
#include "serial/serial_backend.h"
#include <qcolor.h>
#include <qnumeric.h>

AppBackend::AppBackend(QObject* parent)
    : QObject(parent)
    , m_serial(new SerialBackend(this))
    , m_data(new DataBackend(this)) {
    m_serial->setFrameCallback([this](const DataFrame& frame) {
        m_data->ingestFrame(frame);
    });

    connect(m_serial, &SerialBackend::connectedChanged, this, [this]() {
        if (m_serial->connected())
            m_data->clear();
        emit connectedChanged();
    });
    connect(this, &AppBackend::sensorRowChanged, m_serial, &SerialBackend::setSensorHeight);
    connect(this, &AppBackend::sensorColChanged, m_serial, &SerialBackend::setSensorWidth);
}

bool AppBackend::connected() const {
    return m_serial && m_serial->connected();
}

void AppBackend::setLanguage(const QString& lang) {
    if (m_language == lang)
        return;
    m_language = lang;
    emit languageChanged();
}

void AppBackend::setLightMode(bool on) {
    if (m_lightMode == on)
        return;
    m_lightMode = on;
    emit lightModeChanged();
}

void AppBackend::setShowGrid(bool on) {
    qInfo() << "setShowGrid:" << on; 
    if (m_showGrid == on)
        return;

    m_showGrid = on;
    emit showGridChanged(on);
}

void AppBackend::setUseHeatmap(bool on) {
    if (m_useHeatmap == on)
        return;
    m_useHeatmap = on;
    emit useHeatmapChanged(on);
}

void AppBackend::setSensorCol(int c) {
    if (m_serial->connected()) {
        return;
    }
    m_sensorCol = c;
    qInfo() << "sensorColChanged: " << c;
    emit sensorColChanged(c);
}

void AppBackend::setSensorRow(int r) {
    if (m_serial->connected()) {
        return;
    }
    m_sensorRow = r;
    qInfo() << "sensorRowChanged: " << r;
    emit sensorRowChanged(r);
}

void AppBackend::setRangeMin(int v) {
    if (m_rangeMin == v)
        return;
    m_rangeMin = v;
    if (m_rangeMin > m_rangeMax) {
        m_rangeMax = m_rangeMin;
        emit rangeMaxChanged(m_rangeMax);
    }
    emit rangeMinChanged(m_rangeMin);
    emit rangeChanged(m_rangeMin, m_rangeMax);
}

void AppBackend::setRangeMax(int v) {
    if (m_rangeMax == v)
        return;
    m_rangeMax = v;
    if (m_rangeMax < m_rangeMin) {
        m_rangeMin = m_rangeMax;
        emit rangeMinChanged(m_rangeMin);
    }
    emit rangeMaxChanged(m_rangeMax);
    emit rangeChanged(m_rangeMin, m_rangeMax);
}

void AppBackend::setColorZero(const QColor& color) {
    if (m_colorZero == color)
        return;
    m_colorZero = color;
    emit colorZeroChanged(m_colorZero);
}

void AppBackend::setColorLow(const QColor& color) {
    if (m_colorLow == color)
        return;
    m_colorLow = color;
    emit colorLowChanged(m_colorLow);
}

void AppBackend::setColorMid(const QColor& color) {
    if (m_colorMid == color)
        return;
    m_colorMid = color;
    emit colorMidChanged(m_colorMid);
}

void AppBackend::setColorHigh(const QColor& color) {
    if (m_colorHigh == color)
        return;
    m_colorHigh = color;
    emit colorHighChanged(m_colorHigh);
}
