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

#ifndef TACTILEIPC3D_BACKEND_H
#define TACTILEIPC3D_BACKEND_H
#include <QObject>
#include <QString>
#include <QColor>
#include <qcolor.h>
#include <qtmetamacros.h>

#include "data_backend.h"
#include "serial/serial_backend.h"

class AppBackend : public QObject {
    Q_OBJECT
    Q_PROPERTY(bool lightMode READ lightMode WRITE setLightMode NOTIFY lightModeChanged)
    Q_PROPERTY(QString language READ language WRITE setLanguage NOTIFY languageChanged)
    Q_PROPERTY(bool connected READ connected NOTIFY connectedChanged)
    Q_PROPERTY(bool showGrid READ showGrid WRITE setShowGrid NOTIFY showGridChanged);
    Q_PROPERTY(bool useHeatmap READ useHeatmap WRITE setUseHeatmap NOTIFY useHeatmapChanged);
    Q_PROPERTY(SerialBackend* serial READ serial CONSTANT)
    Q_PROPERTY(DataBackend* data READ data CONSTANT)
    Q_PROPERTY(int sensorCol READ sensorCol WRITE setSensorCol NOTIFY sensorColChanged);
    Q_PROPERTY(int sensorRow READ sensorRow WRITE setSensorRow NOTIFY sensorRowChanged);
    Q_PROPERTY(int rangeMin READ rangeMin WRITE setRangeMin NOTIFY rangeMinChanged);
    Q_PROPERTY(int rangeMax READ rangeMax WRITE setRangeMax NOTIFY rangeMaxChanged);
    Q_PROPERTY(QColor colorZero READ colorZero WRITE setColorZero NOTIFY colorZeroChanged);
    Q_PROPERTY(QColor colorLow READ colorLow WRITE setColorLow NOTIFY colorLowChanged);
    Q_PROPERTY(QColor colorMid READ colorMid WRITE setColorMid NOTIFY colorMidChanged);
    Q_PROPERTY(QColor colorHigh READ colorHigh WRITE setColorHigh NOTIFY colorHighChanged);

public:
    explicit AppBackend(QObject* parent=nullptr);

    bool lightMode() const { return m_lightMode; }
    QString language() const { return m_language; }
    bool connected() const;

    SerialBackend* serial() const { return m_serial; }
    DataBackend* data() const { return m_data; }

    void setLanguage(const QString& lang);
    void setLightMode(bool on);

    bool showGrid() const { return m_showGrid; }
    void setShowGrid(bool on);
    bool useHeatmap() const { return m_useHeatmap; }
    void setUseHeatmap(bool on);
    int sensorCol() const { qInfo() << "col:" << m_sensorCol; return m_sensorCol; }
    int sensorRow() const { qInfo() << "row:" << m_sensorRow; return m_sensorRow; }
    void setSensorRow(int r);
    void setSensorCol(int c);
    int rangeMin() const { return m_rangeMin; }
    int rangeMax() const { return m_rangeMax; }
    void setRangeMin(int v);
    void setRangeMax(int v);
    QColor colorZero() const { return m_colorZero; }
    QColor colorLow() const { return m_colorLow; }
    QColor colorMid() const { return m_colorMid; }
    QColor colorHigh() const { return m_colorHigh; }
    void setColorZero(const QColor& color);
    void setColorLow(const QColor& color);
    void setColorMid(const QColor& color);
    void setColorHigh(const QColor& color);

signals:
    void lightModeChanged();
    void languageChanged();
    void connectedChanged();
    void showGridChanged(bool on);
    void useHeatmapChanged(bool on);
    void sensorColChanged(int c);
    void sensorRowChanged(int r);
    void rangeMinChanged(int v);
    void rangeMaxChanged(int v);
    void rangeChanged(int minV, int maxV);
    void colorZeroChanged(const QColor& color);
    void colorLowChanged(const QColor& color);
    void colorMidChanged(const QColor& color);
    void colorHighChanged(const QColor& color);
private:
    SerialBackend* m_serial = nullptr;
    DataBackend* m_data = nullptr;
    bool m_lightMode = true;
    QString m_language = QStringLiteral("zh_CN");

    bool m_showGrid = true;
    bool m_useHeatmap = false;
    int m_sensorRow = 12;
    int m_sensorCol = 7;
    int m_rangeMin = 0;
    int m_rangeMax = 1000;
    QColor m_colorZero = QColor::fromRgbF(0.10, 0.75, 1.00);
    QColor m_colorLow = QColor::fromRgbF(0.10, 0.75, 1.00);
    QColor m_colorMid = QColor::fromRgbF(0.10, 0.95, 0.35);
    QColor m_colorHigh = QColor::fromRgbF(1.00, 0.22, 0.10);
};

#endif //TACTILEIPC3D_BACKEND_H
