// 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 is used to access sensors data avaiable within the device (i.e. a cell phone)
#include "sensorscellphone.h"
#include <QDebug>

SensorsCellPhone::SensorsCellPhone(QObject *parent) : QObject(parent)
{
    // Creating the total pressure sensor object
    pressureSensor = new QPressureSensor();
    pressureSensor->setAlwaysOn(true);
    // Creating the temperature sensor object
    temperatureSensor = new QAmbientTemperatureSensor();
    temperatureSensor->setAlwaysOn(true);

    // Creating the humidity sensor
    #if QT_VERSION >= QT_VERSION_CHECK(5,9,0)
        humiditySensor = new QHumiditySensor();
        humiditySensor->setAlwaysOn(true);
    #endif

    connect(this->pressureSensor, SIGNAL(activeChanged()), this, SIGNAL(barometerActiveChanged()));
    connect(this->temperatureSensor, SIGNAL(activeChanged()), this, SIGNAL(thermometerActiveChanged()));
    #if QT_VERSION >= QT_VERSION_CHECK(5,9,0)
        connect(this->humiditySensor, SIGNAL(activeChanged()), this, SIGNAL(humiditySensorActiveChanged()));
    #endif
}

// Methods to get the last recorded values from sensors
double SensorsCellPhone::getTotalPressure()
{
    if (getTotalPressureSensorIsPresent())
        return pressureSensor->reading()->pressure();
    else
        return 0;
}
double SensorsCellPhone::getTemperature()
{
    if (getTemperatureSensorIsPresent())
        return temperatureSensor->reading()->temperature();
    else
        return 0;
}
double SensorsCellPhone::getHumidity()
{
    // Humidity sensor is unavailable for Qt lower than version 5.? -> not sure when it will be enabled
    if (getHumiditySensorIsPresent())
        return humiditySensor->reading()->relativeHumidity();
    else
        return 0;
}

// Methods to enable the sensors to record data
void SensorsCellPhone::setEnableTotalPressureSensor(bool enable)
{
    // Enable only if the total pressure sensor is present
    if (getTotalPressureSensorIsPresent())
    {
        if (enable == true) {
            // Only do this if the pressure sensor was in an inactive state
            if (pressureSensor->isActive() == false)
            {
                pressureSensor->start();
                pressureSensor->setActive(true);
            }
        }
        else {
            // Only do this if the sensor was previously active
            if (pressureSensor->isActive())
            {
                pressureSensor->stop();
                pressureSensor->setActive(false);
            }
        }
    }
}
bool SensorsCellPhone::getTotalPressureSensorIsEnabled() { return pressureSensor->isActive(); }  // Modify the include the SensorTag

void SensorsCellPhone::setEnableTemperatureSensor(bool enable)
{
    // Enable only if the temperature sensor is present
    if (getTemperatureSensorIsPresent())
    {
        if (enable == true) {
            // Only do this if the sensor was in an inactive state
            if (temperatureSensor->isActive() == false)
            {
                temperatureSensor->setActive(true);
                temperatureSensor->start();
            }
        }
        else {
            // Only do this if the sensor was previously active
            if (temperatureSensor->isActive())
            {
                temperatureSensor->stop();
                temperatureSensor->setActive(false);
            }
        }
    }
}
bool SensorsCellPhone::getTemperatureSensorIsEnabled() { return temperatureSensor->isActive(); } // Modify the include the SensorTag

void SensorsCellPhone::setEnableHumiditySensor(bool enable)
{
    // Modify the include the SensorTag

    // The humidity sensor segment only makes sense for Qt above 5.9 (not available prior to Qt 5.9) and even then it is not trully supported
    // Maybe should modify this code later to extract the humidity into a separate class or something for better looks
    if (getHumiditySensorIsPresent())
    {
        if (enable == true)
        {
            // Only do this if the sensor was in an inactive state
            if (humiditySensor->isActive() == false)
            {
                humiditySensor->setActive(true);
                humiditySensor->start();
            }
        }
        else
        {
            // Only do this if the sensor was previously active
            if (humiditySensor->isActive())
            {
                humiditySensor->stop();
                humiditySensor->setActive(false);
            }
        }
    }
}
bool SensorsCellPhone::getHumiditySensorIsEnabled()
{
    if (getHumiditySensorIsPresent())
        return humiditySensor->isActive();
    else
        return false;    
}

// Methods to establish if one of the below sensors is present
bool SensorsCellPhone::getTotalPressureSensorIsPresent()
{
    if (QSensor::sensorsForType("QPressureSensor").count() > 0) { return true; }
                                                           else { return false; }
}
bool SensorsCellPhone::getTemperatureSensorIsPresent()
{
    if (QSensor::sensorsForType("QAmbientTemperatureSensor").count() > 0) { return true; }
                                                                     else { return false; }
}
bool SensorsCellPhone::getHumiditySensorIsPresent()
{
    if (QSensor::sensorsForType("QHumiditySensor").count() > 0) { return true; }
                                                           else { return false; }
}
