#ifndef CANCONTROLLER_H
#define CANCONTROLLER_H

#include <QObject>
#include <QVariant>

class QQmlEngine;
class QJSEngine;

class CanMessage;

class CanController : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString inputVoltage MEMBER inputVoltage_ NOTIFY inputVoltageChanged)
    Q_PROPERTY(QString radiatorTemperature MEMBER radiatorTemperature_ NOTIFY radiatorTemperatureChanged)
    Q_PROPERTY(QString outputVoltage MEMBER outputVoltage_ NOTIFY outputVoltageChanged)
    Q_PROPERTY(QString emergencyCounter MEMBER emergencyCounter_ NOTIFY emergencyCounterChanged)
    Q_PROPERTY(QString outputCurrent MEMBER outputCurrent_ NOTIFY outputCurrentChanged)

    Q_PROPERTY(QString maximumCurrent MEMBER maximumCurrent_ NOTIFY maximumCurrentChanged)
    Q_PROPERTY(QString emergencyDelay MEMBER emergencyDelay_ NOTIFY emergencyDelayChanged)
    Q_PROPERTY(QString breakingDelay MEMBER breakingDelay_ NOTIFY breakingDelayChanged)
    Q_PROPERTY(QString retriesAfterEmergencyBreak MEMBER retriesAfterEmergencyBreak_ NOTIFY retriesAfterEmergencyBreakChanged)
    Q_PROPERTY(QString breakingCurrent MEMBER breakingCurrent_ NOTIFY breakingCurrentChanged)

    Q_PROPERTY(QVariantList statuses MEMBER statuses_ NOTIFY statusesChanged)
    Q_PROPERTY(bool isVkuClosed MEMBER isVkuClosed_ NOTIFY isVkuClosedChanged)

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

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

public slots:
    void connectCan();
    void disconnectCan();

    void vkuClosure();
    void emergencyReset();

private slots:
    void sendParameters();

signals:
    void inputVoltageChanged();
    void radiatorTemperatureChanged();
    void outputVoltageChanged();
    void emergencyCounterChanged();
    void outputCurrentChanged();

    void maximumCurrentChanged();
    void emergencyDelayChanged();
    void breakingDelayChanged();
    void retriesAfterEmergencyBreakChanged();
    void breakingCurrentChanged();

    void statusesChanged();
    void isVkuClosedChanged();

private slots:
    void handlePackage(int index);
    void handleCurrentState(const CanMessage* message);
    void handleStatus(const CanMessage* message);

private:
    void setupDrivers();
    void setupDefaultParameters();

private:
    QString inputVoltage_;
    QString radiatorTemperature_;
    QString outputVoltage_;
    QString emergencyCounter_;
    QString outputCurrent_;

    QString maximumCurrent_;
    QString emergencyDelay_;
    QString breakingDelay_;
    QString retriesAfterEmergencyBreak_;
    QString breakingCurrent_;

    QVariantList statuses_;
    bool isVkuClosed_ = false;
};

#endif // CANCONTROLLER_H
