#ifndef DATACONTROLLER_H
#define DATACONTROLLER_H

#include <QObject>

class QQmlEngine;
class QJSEngine;
class QSerialPort;

class DataController : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString firstStatus MEMBER firstStatus_ NOTIFY firstStatusChanged)
    Q_PROPERTY(QString secondStatus MEMBER secondStatus_ NOTIFY secondStatusChanged)

    Q_PROPERTY(QString psnInputVoltage MEMBER psnInputVoltage_ NOTIFY psnInputVoltageChanged)
    Q_PROPERTY(QString mainVoltage MEMBER mainVoltage_ NOTIFY mainVoltageChanged)

    Q_PROPERTY(int ainMode MEMBER ainMode_ NOTIFY ainModeChanged)
    Q_PROPERTY(int ainReservedMask MEMBER ainReservedMask_ NOTIFY ainReservedMaskChanged)

    Q_PROPERTY(QString ain1Voltage MEMBER ain1Voltage_ NOTIFY ain1VoltageChanged)
    Q_PROPERTY(QString ain1Current MEMBER ain1Current_ NOTIFY ain1CurrentChanged)

    Q_PROPERTY(QString ain2Voltage MEMBER ain2Voltage_ NOTIFY ain2VoltageChanged)
    Q_PROPERTY(QString ain2Current MEMBER ain2Current_ NOTIFY ain2CurrentChanged)

    Q_PROPERTY(QString ain3Voltage MEMBER ain3Voltage_ NOTIFY ain3VoltageChanged)
    Q_PROPERTY(QString ain3Current MEMBER ain3Current_ NOTIFY ain3CurrentChanged)

    Q_PROPERTY(QString ain4Voltage MEMBER ain4Voltage_ NOTIFY ain4VoltageChanged)
    Q_PROPERTY(QString ain4Current MEMBER ain4Current_ NOTIFY ain4CurrentChanged)

    Q_PROPERTY(QString ain5Voltage MEMBER ain5Voltage_ NOTIFY ain5VoltageChanged)
    Q_PROPERTY(QString ain5Current MEMBER ain5Current_ NOTIFY ain5CurrentChanged)

    Q_PROPERTY(QString ain6Voltage MEMBER ain6Voltage_ NOTIFY ain6VoltageChanged)
    Q_PROPERTY(QString ain6Current MEMBER ain6Current_ NOTIFY ain6CurrentChanged)

public:
    explicit DataController(QObject* parent = nullptr);
    ~DataController();

    static QObject* qmlInstance(QQmlEngine* engine, QJSEngine* scriptEngine);

signals:
    void firstStatusChanged();
    void secondStatusChanged();

    void psnInputVoltageChanged();
    void mainVoltageChanged();

    void ainModeChanged();
    void ainReservedMaskChanged();

    void ain1VoltageChanged();
    void ain1CurrentChanged();
    void ain2VoltageChanged();
    void ain2CurrentChanged();
    void ain3VoltageChanged();
    void ain3CurrentChanged();

    void ain4VoltageChanged();
    void ain4CurrentChanged();
    void ain5VoltageChanged();
    void ain5CurrentChanged();
    void ain6VoltageChanged();
    void ain6CurrentChanged();

private slots:
    void serialReadyRead();
    void serialError();

private:
    QVariantMap readSettings();
    void openSerialPort();
    void parseSerialData();
    void mockSerialData();

    QString firstStatus_ = " — ";
    QString secondStatus_ = " — ";

    QString psnInputVoltage_;
    QString mainVoltage_;

    int ainMode_ = 0;
    int ainReservedMask_ = 0;

    QString ain1Voltage_;
    QString ain1Current_;
    QString ain2Voltage_;
    QString ain2Current_;
    QString ain3Voltage_;
    QString ain3Current_;

    QString ain4Voltage_;
    QString ain4Current_;
    QString ain5Voltage_;
    QString ain5Current_;
    QString ain6Voltage_;
    QString ain6Current_;

    QByteArray serialData_;
    QScopedPointer<QSerialPort> serialPort_;
};

#endif // DATACONTROLLER_H
