// bytran -|- spectral calculations for portable devices using the HITRAN database
//
// Developed by Dzianis Pliutau (Denis Plutov) and Kostya Roslyakov
// www.bytran.org | bytran@bytran.org
// Copyright (c) 2016 Dzianis Pliutau
// Distributed under the MIT license
//
// The bytran logo is distributed under the Creative Commons 3.0 Attribution License: https://creativecommons.org/licenses/by/3.0/us/
//
// The MIT License (MIT)
// Copyright (c) 2016 Dzianis Pliutau
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



// Object to transfer over data from the sensors C++ class to/from the QML interface
#ifndef SENSORSQML_H
#define SENSORSQML_H

#include <QObject>
#include <QLocale>
#include "bytrancore.h"

class SensorsQml : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString totalPressure READ totalPressure NOTIFY totalPressureChanged)
    Q_PROPERTY(QString temperature READ temperature NOTIFY temperatureChanged)
    Q_PROPERTY(QString humidity READ humidity NOTIFY humidityChanged)    
    Q_PROPERTY(int samplingPeriod READ samplingPeriod WRITE setSamplingPeriod NOTIFY samplingPeriodChanged)
    Q_PROPERTY(int numSamplesToAverage READ numSamplesToAverage WRITE setNumSamplesToAverage NOTIFY numSamplesToAverageChanged)
    Q_PROPERTY(int averagingTime READ averagingTime NOTIFY averagingTimeChanged)
    Q_PROPERTY(bool totalPressureSensorEnabled READ totalPressureSensorEnabled WRITE setTotalPressureSensorEnabled NOTIFY totalPressureSensorEnabledChanged)
    Q_PROPERTY(bool temperatureSensorEnabled READ temperatureSensorEnabled WRITE setTemperatureSensorEnabled NOTIFY temperatureSensorEnabledChanged)
    Q_PROPERTY(bool humiditySensorEnabled READ humiditySensorEnabled WRITE setHumiditySensorEnabled NOTIFY humiditySensorEnabledChanged)
    Q_PROPERTY(bool totalPressureSensorIsPresent READ totalPressureSensorIsPresent NOTIFY totalPressureSensorIsPresentChanged)
    Q_PROPERTY(bool temperatureSensorIsPresent READ temperatureSensorIsPresent NOTIFY temperatureSensorIsPresentChanged)
    Q_PROPERTY(bool humidityBuiltInSensorIsPresent READ humidityBuiltInSensorIsPresent NOTIFY humidityBuiltInSensorIsPresentChanged)
    Q_PROPERTY(QString humiditySensorLabelText READ humiditySensorLabelText NOTIFY humiditySensorLabelTextChanged)
    Q_PROPERTY(QString temperatureSensorLabelText READ temperatureSensorLabelText NOTIFY temperatureSensorLabelTextChanged)
    Q_PROPERTY(QString totalPressSensorLabelText READ totalPressSensorLabelText NOTIFY totalPressSensorLabelTextChanged)
    Q_PROPERTY(bool useSensorTag READ useSensorTag WRITE setUseSensorTag NOTIFY useSensorTagChanged)
    Q_PROPERTY(bool sensorTagConnected READ sensorTagConnected NOTIFY sensorTagConnectedChanged)
    Q_PROPERTY(int tempMode READ tempMode WRITE setTempMode NOTIFY tempModeChanged)
    Q_PROPERTY(QString connectButtonText READ connectButtonText NOTIFY connectButtonTextChanged)
    Q_PROPERTY(int selectedDeviceIndex READ selectedDeviceIndex WRITE setSelectedDeviceIndex NOTIFY selectedDeviceIndexChanged)

public:

    Q_INVOKABLE void discoverDevices();
    Q_INVOKABLE void connectSensorTagDevice();

    explicit SensorsQml(QObject *parent = 0, BytranCore *inCore = 0);

    QString totalPressure() const;
    QString temperature() const;
    QString humidity() const;

    int samplingPeriod() const;
    void setSamplingPeriod(int const samplingPeriod);
    int numSamplesToAverage() const;
    void setNumSamplesToAverage(int const numSamplesToAverage);
    int averagingTime() const;
    void setAveragingTime(int const averagingTime);

    bool totalPressureSensorEnabled() const;
    void setTotalPressureSensorEnabled(const bool totalPressureSensorEnabled);
    bool temperatureSensorEnabled() const;
    void setTemperatureSensorEnabled(const bool temperatureSensorEnabled);
    bool humiditySensorEnabled() const;
    void setHumiditySensorEnabled(const bool humiditySensorEnabled);

    bool totalPressureSensorIsPresent() const;
    bool temperatureSensorIsPresent() const;
    bool humidityBuiltInSensorIsPresent() const;

    QString humiditySensorLabelText() const;
    QString temperatureSensorLabelText() const;
    QString totalPressSensorLabelText() const;

    // Methods set the variable indicating if the sesnor tag is to be used (on/off)
    bool useSensorTag() const;
    void setUseSensorTag(const bool &useSensorTag);

    // Method notifies if the sensor tag is connected
    bool sensorTagConnected() const;

    // Temperature sensor mode (HDC1000 value, BPM280 value, or their average value)
    int tempMode() const;
    void setTempMode(const int &tempMode);

    // Methods set and get the selected device index in the Bluetooth low energy discovered devices list.
    int selectedDeviceIndex() const;
    void setSelectedDeviceIndex(const int &selectedDeviceIndex);

    // Text of the bluetooth low energy connect button
    QString connectButtonText() const;

signals:

    // Signals emited then the sensor read in values are updated
    void totalPressureChanged();
    void temperatureChanged();
    void humidityChanged();

    // Signals emited shen the sampling period or averaging time is changed
    void samplingPeriodChanged();
    void numSamplesToAverageChanged();
    void averagingTimeChanged();

    // Signal to provide information about the enabled state of the sensors (if they are recording and should be used instead of manual entry)
    void totalPressureSensorEnabledChanged();
    void temperatureSensorEnabledChanged();
    void humiditySensorEnabledChanged();

    // SensorTag related signals
    void useSensorTagChanged();
    void sensorTagConnectedChanged();
    void tempModeChanged();

    // Signals emited when the sensor presence variables are modified
    void totalPressureSensorIsPresentChanged();
    void temperatureSensorIsPresentChanged();
    void humidityBuiltInSensorIsPresentChanged();

    // Signals to notify when the auxiliary label sensor text is changed
    void humiditySensorLabelTextChanged();
    void temperatureSensorLabelTextChanged();
    void totalPressSensorLabelTextChanged();

    // Additional signals
    void selectedDeviceIndexChanged();
    void connectButtonTextChanged();

public slots:

private slots:

    // Private slot to update the sensor values
    void updateTotalPressureValue();
    void updateTemperatureValue();
    void updateHumidityValue();

    // Private slot to update the sensor active states
    void updateBarometerActiveState();
    void updateThermometerActiveState();
    void updateHumiditySensorActiveState();

private:

    QString m_totalPressure;  // Total pressure value in current units
    QString m_temperature;    // Temperature value in current units
    QString m_humidity;       // Humidity value

    int m_samplingPeriod;        // Sampling period, milliseconds
    int m_numSamplesToAverage;   // Number of samples to average
    int m_averagingTime;         // Time in milliseconds to average retrieved data over

    bool m_totalPressureSensorEnabled;  // Variable indicating if the total pressure sensor is enabled
    bool m_temperatureSensorEnabled;    // Variable indicating if the temperature sensor is enabled
    bool m_humiditySensorEnabled;       // Variable indicating if the humidity sensor is enabled

    bool m_totalPressureSensorIsPresent;   // Variable indicates if the total pressure sensor is present
    bool m_temperatureSensorIsPresent;     // Variable indicates if the temperature sensor is present
    bool m_humidityBuiltInSensorIsPresent;        // Variable indicates if the humidity sensor is present

    // Text for the auxiliary labels when the sensors are not available or not enabled
    QString m_humiditySensorLabelText;
    QString m_temperatureSensorLabelText;
    QString m_totalPressSensorLabelText;

    // Variable indicates if the SensorTag is to be used
    bool m_useSensorTag;

    // Variable indicates if the SensorTag is connected
    bool m_sensorTagConnected;

    // Sensor to use to get the ambient temperature value (or the average of the available temperature values from sensors)
    int m_tempMode;  // Temperature mode: 1 - HDC1000 sensor value, 2 - BMP280 sensor value, all other options give the average of the HDC1000 and the BMP280 values

    // Selected service item index
    int m_selectedDeviceIndex;

    // Connect/Disconnect button text
    QString m_connectButtonText;

    // Core object with the calculation logic etc.
    BytranCore *core;

    // Locale specific string to number and inverse conversions
    QLocale loc;

};

#endif // SENSORSQML_H
