#pragma once

#include <string_view>
#include <utility>
#include <vector>

#include <QByteArray>
#include <QString>

class SensorDriver {
    class Sensor {
      public:
        ~Sensor();

        std::pair<int, int> readRange();
        std::vector<int> readValues();

        operator bool() const { return !m_errorOccurred; }

      private:
        friend SensorDriver;

        Sensor(const QString& serialPortName);

        static constexpr auto VERSION_CMD = std::string_view{"version"};
        static constexpr auto RANGE_CMD = std::string_view{"range"};
        static constexpr auto READ_CMD = std::string_view{"read csv"};

        const QString m_serialPortName;
        bool m_errorOccurred = false;

        bool isValidSensor();

        bool getSensorCommandResponse(const QString& serialPortName, QByteArray command, QByteArray& response);

        std::string parseVersionResponse(const QByteArray& response) const;
        std::pair<int, int> parseRangeResponse(const QByteArray& response) const;
        std::vector<int> parseReadResponse(const QByteArray& response) const;
    };

  public:
    SensorDriver();
    ~SensorDriver();

    std::vector<Sensor> enumerateSensors() const;

  private:
};
