#include "WimaMeasurementArea.h"


const char* WimaMeasurementArea::settingsGroup =                   "MeasurementArea";
const char* WimaMeasurementArea::bottomLayerAltitudeName =         "BottomLayerAltitude";
const char* WimaMeasurementArea::numberOfLayersName =              "NumberOfLayers";
const char* WimaMeasurementArea::layerDistanceName =               "LayerDistance";
const char* WimaMeasurementArea::WimaMeasurementAreaName =         "Measurement Area";


WimaMeasurementArea::WimaMeasurementArea(QObject *parent)
  :     WimaArea (parent)
  ,     _metaDataMap            (FactMetaData::createMapFromJsonFile(QStringLiteral(":/json/WimaMeasurementArea.SettingsGroup.json"), this /* QObject parent */))
  ,     _bottomLayerAltitude    (SettingsFact(settingsGroup, _metaDataMap[bottomLayerAltitudeName], this /* QObject parent */))
  ,     _numberOfLayers         (SettingsFact(settingsGroup, _metaDataMap[numberOfLayersName], this /* QObject parent */))
  ,     _layerDistance          (SettingsFact(settingsGroup, _metaDataMap[layerDistanceName], this /* QObject parent */))
{
    init();
}

WimaMeasurementArea::WimaMeasurementArea(const WimaMeasurementArea &other, QObject *parent)
    :     WimaArea (other, parent)
    ,     _metaDataMap            (FactMetaData::createMapFromJsonFile(QStringLiteral(":/json/WimaMeasurementArea.SettingsGroup.json"), this /* QObject parent */))
    ,     _bottomLayerAltitude    (SettingsFact(settingsGroup, _metaDataMap[bottomLayerAltitudeName], this /* QObject parent */))
    ,     _numberOfLayers         (SettingsFact(settingsGroup, _metaDataMap[numberOfLayersName], this /* QObject parent */))
    ,     _layerDistance          (SettingsFact(settingsGroup, _metaDataMap[layerDistanceName], this /* QObject parent */))
{
    init();
}

/*!
 * \overload operator=()
 *
 * Calls the inherited operator WimaArea::operator=().
 */
WimaMeasurementArea &WimaMeasurementArea::operator=(const WimaMeasurementArea &other)
{
    WimaArea::operator=(other);

    return *this;
}

void WimaMeasurementArea::saveToJson(QJsonObject &json)
{
    this->WimaArea::saveToJson(json);
    json[bottomLayerAltitudeName]   = _bottomLayerAltitude.rawValue().toDouble();
    json[numberOfLayersName]        = _numberOfLayers.rawValue().toInt();
    json[layerDistanceName]         = _layerDistance.rawValue().toDouble();
    json[areaTypeName]              = WimaMeasurementAreaName;
}

bool WimaMeasurementArea::loadFromJson(const QJsonObject &json, QString& errorString)
{
    if (this->WimaArea::loadFromJson(json, errorString)) {
        bool retVal = true;

        if ( json.contains(bottomLayerAltitudeName) && json[bottomLayerAltitudeName].isDouble() ) {
            _bottomLayerAltitude.setRawValue(json[bottomLayerAltitudeName].toDouble());
        } else {
            errorString.append(tr("Could not load Bottom Layer Altitude!\n"));
            retVal = false;
        }

        if ( json.contains(numberOfLayersName) && json[numberOfLayersName].isDouble() ) {
            _numberOfLayers.setRawValue(json[numberOfLayersName].toInt());
        } else {
            errorString.append(tr("Could not load Number of Layers!\n"));
            retVal = false;
        }

        if ( json.contains(layerDistanceName) && json[layerDistanceName].isDouble() ) {
            _layerDistance.setRawValue(json[layerDistanceName].toDouble());
        } else {
            errorString.append(tr("Could not load Layer Distance!\n"));
            retVal = false;
        }

        return retVal;
    } else {
        return false;
    }
}

void WimaMeasurementArea::setBottomLayerAltitude(double altitude)
{
    if ( !qFuzzyCompare(_bottomLayerAltitude.rawValue().toDouble(), altitude) ) {
        _bottomLayerAltitude.setRawValue(altitude);

        emit bottomLayerAltitudeChanged();
    }
}

void WimaMeasurementArea::setNumberOfLayers(double numLayers)
{
    if ( !qFuzzyCompare(_numberOfLayers.rawValue().toDouble(), numLayers) ) {
        _numberOfLayers.setRawValue(numLayers);

        emit numberOfLayersChanged();
    }
}

void WimaMeasurementArea::setLayerDistance(double layerDistance)
{
    if ( !qFuzzyCompare(_layerDistance.rawValue().toDouble(), layerDistance) ) {
        _layerDistance.setRawValue(layerDistance);

        emit layerDistanceChanged();
    }
}

void print(const WimaMeasurementArea &area)
{
    QString message;
    print(area, message);
    qWarning() << message;
}

void print(const WimaMeasurementArea &area, QString outputStr)
{
    print(static_cast<const WimaArea&>(area), outputStr);
    outputStr.append(QString("Bottom Layer Altitude: %1\n").arg(area._bottomLayerAltitude.rawValue().toDouble()));
    outputStr.append(QString("Number of Layers: %1\n").arg(area._numberOfLayers.rawValue().toInt()));
    outputStr.append(QString("Layer Distance: %1\n").arg(area._layerDistance.rawValue().toDouble()));
}

void WimaMeasurementArea::init()
{
    this->setObjectName(WimaMeasurementAreaName);
}

/*!
 * \class WimaMeasurementArea
 * \brief Class defining the area inside which the actual drone measurements are performed.
 *
 * \sa WimaArea, WimaController, WimaPlaner
 */


