#ifndef DATACONTROLLER_H
#define DATACONTROLLER_H

#include <QObject>

class QQmlEngine;
class QJSEngine;
class QSerialPort;

class DataController : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString ipbsFirstVoltage MEMBER ipbsFirstVoltage_ NOTIFY ipbsFirstVoltageChanged)
    Q_PROPERTY(QString ipbsFirstCurrent MEMBER ipbsFirstCurrent_ NOTIFY ipbsFirstCurrentChanged)

    Q_PROPERTY(QString ipbsSecondVoltage MEMBER ipbsSecondVoltage_ NOTIFY ipbsSecondVoltageChanged)
    Q_PROPERTY(QString ipbsSecondCurrent MEMBER ipbsSecondCurrent_ NOTIFY ipbsSecondCurrentChanged)

    Q_PROPERTY(QString ipbsThirdVoltage MEMBER ipbsThirdVoltage_ NOTIFY ipbsThirdVoltageChanged)
    Q_PROPERTY(QString ipbsThirdCurrent MEMBER ipbsThirdCurrent_ NOTIFY ipbsThirdCurrentChanged)

    Q_PROPERTY(QString mainVoltage MEMBER mainVoltage_ NOTIFY mainVoltageChanged)
    Q_PROPERTY(int ipbsMode MEMBER ipbsMode_ NOTIFY ipbsModeChanged)

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

    Q_PROPERTY(QString ainFirstVoltage MEMBER ainFirstVoltage_ NOTIFY ainFirstVoltageChanged)
    Q_PROPERTY(QString ainFirstCurrent MEMBER ainFirstCurrent_ NOTIFY ainFirstCurrentChanged)

    Q_PROPERTY(QString ainSecondVoltage MEMBER ainSecondVoltage_ NOTIFY ainSecondVoltageChanged)
    Q_PROPERTY(QString ainSecondCurrent MEMBER ainSecondCurrent_ NOTIFY ainSecondCurrentChanged)

    Q_PROPERTY(QString ainThirdVoltage MEMBER ainThirdVoltage_ NOTIFY ainThirdVoltageChanged)
    Q_PROPERTY(QString ainThirdCurrent MEMBER ainThirdCurrent_ NOTIFY ainThirdCurrentChanged)

    Q_PROPERTY(QString psnInputVoltage MEMBER psnInputVoltage_ NOTIFY psnInputVoltageChanged)

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

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

signals:
    void ipbsFirstVoltageChanged();
    void ipbsFirstCurrentChanged();
    void ipbsSecondVoltageChanged();
    void ipbsSecondCurrentChanged();
    void ipbsThirdVoltageChanged();
    void ipbsThirdCurrentChanged();
    void mainVoltageChanged();
    void ipbsModeChanged();
    void firstStatusChanged();
    void secondStatusChanged();
    void ainFirstVoltageChanged();
    void ainFirstCurrentChanged();
    void ainSecondVoltageChanged();
    void ainSecondCurrentChanged();
    void ainThirdVoltageChanged();
    void ainThirdCurrentChanged();
    void psnInputVoltageChanged();

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

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

    QString ipbsFirstVoltage_;
    QString ipbsFirstCurrent_;
    QString ipbsSecondVoltage_;
    QString ipbsSecondCurrent_;
    QString ipbsThirdVoltage_;
    QString ipbsThirdCurrent_;
    QString mainVoltage_;
    int ipbsMode_ = 0;
    QString firstStatus_ = " — ";
    QString secondStatus_ = " — ";
    QString ainFirstVoltage_;
    QString ainFirstCurrent_;
    QString ainSecondVoltage_;
    QString ainSecondCurrent_;
    QString ainThirdVoltage_;
    QString ainThirdCurrent_;
    QString psnInputVoltage_;

    QByteArray serialData_;
    QScopedPointer<QSerialPort> serialPort_;
};

#endif // DATACONTROLLER_H
