#include "WimaGOperationArea.h"


const char* WimaGOperationArea::settingsGroup =                   "OperationArea";
const char* WimaGOperationArea::bottomLayerAltitudeName =         "BottomLayerAltitude";
const char* WimaGOperationArea::numberOfLayersName =              "NumberOfLayers";
const char* WimaGOperationArea::layerDistanceName =               "LayerDistance";
const char* WimaGOperationArea::borderPolygonOffsetName =         "BorderPolygonOffset";
const char* WimaGOperationArea::wimaGOperationAreaName =          "Operation Area";


WimaGOperationArea::WimaGOperationArea(QObject *parent)
  : WimaArea (parent)
{
    init();
}

WimaGOperationArea::WimaGOperationArea(const WimaGOperationArea &other, QObject *parent)
  : WimaArea(other, parent)
{
    init();
}

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

bool WimaGOperationArea::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;
        }

        if ( json.contains(borderPolygonOffsetName) && json[borderPolygonOffsetName].isDouble() ) {
            _borderPolygonOffset.setRawValue(json[borderPolygonOffsetName].toDouble());
        } else {
            errorString.append(tr("Could not load Border Polygon Offset!\n"));
            retVal = false;
        }

        return retVal;
    } else {
        return false;
    }
}

void WimaGOperationArea::update(const WimaGOperationArea &area)
{
    this->WimaArea::update(area);

    this->setBottomLayerAltitude(area.bottomLayerAltitude());
    this->setNumberOfLayers(area.numberOfLayers());
    this->setLayerDistance(area.layerDistance());
    this->setBorderPolygonOffset(area.borderPolygonOffset());

    recalcBorderPolygon();
}

void WimaGOperationArea::setBottomLayerAltitude(double altitude)
{

}

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

void print(const WimaGOperationArea &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()));
    outputStr.append(QString("Border Polygon Offset: %1\n").arg(area._borderPolygonOffset.rawValue().toDouble()));

    outputStr.append(QString("Border Polygon Coordinates\n").arg(area._borderPolygonOffset.rawValue().toDouble()));
    for (int i = 0; i < area._borderPolygon.count(); i++) {
        QGeoCoordinate coordinate = area._borderPolygon.vertexCoordinate(i);
        outputStr.append(QString("%1\n").arg(coordinate.toString(QGeoCoordinate::Degrees)));
    }
}

void WimaGOperationArea::recalcBorderPolygon()
{
    //qWarning("WimaGOperationArea::recalcBorderPolygon() %f", _borderPolygonOffset.rawValue().toDouble());
    QGCMapPolygon polyCopy = this->toQGCPolygon(*this);
    polyCopy.offset(_borderPolygonOffset.rawValue().toDouble());
    _borderPolygon.setPath(polyCopy.path());
    polyCopy.deleteLater();

    emit borderPolygonChanged();
}

void WimaGOperationArea::init()
{

    _metaDataMap            = FactMetaData::createMapFromJsonFile(QStringLiteral(":/json/WimaGOperationArea.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 */);
    _borderPolygonOffset    = SettingsFact(settingsGroup, _metaDataMap[borderPolygonOffsetName], this /* QObject parent */);
    _borderPolygon          = new QGCMapPolygon(this);

    this->setObjectName(wimaGOperationAreaName);
    connect(this, &WimaGOperationArea::pathChanged, this, &WimaGOperationArea::recalcBorderPolygon);
    connect(&_borderPolygonOffset, &SettingsFact::rawValueChanged, this, &WimaGOperationArea::recalcBorderPolygon);
}


