#include "WimaGlobalMeasurementPolygon.h"


const char* WimaGlobalMeasurementPolygon::settingsGroup =               "OperatingArea";
const char* WimaGlobalMeasurementPolygon::bottomLayerAltitudeName =         "BottomLayerAltitude";
const char* WimaGlobalMeasurementPolygon::numberOfLayersName =              "NumberOfLayers";
const char* WimaGlobalMeasurementPolygon::layerDistanceName =               "LayerDistance";

WimaGlobalMeasurementPolygon::WimaGlobalMeasurementPolygon(QObject *parent)
  : WimaPolygon (parent)
  , _metaDataMap            (FactMetaData::createMapFromJsonFile(QStringLiteral(":/json/WimaGlobalMeasurementPolygon.SettingsGroup.json"), this /* QObject parent */))
  , _bottomLayerAltitude    (settingsGroup, _metaDataMap[bottomLayerAltitudeName])
  , _numberOfLayers         (settingsGroup, _metaDataMap[numberOfLayersName])
  , _layerDistance          (settingsGroup, _metaDataMap[layerDistanceName])
{
    this->setObjectName("Operating Area");
}

WimaGlobalMeasurementPolygon::WimaGlobalMeasurementPolygon(QGCMapPolygon *other, QObject *parent):
    WimaPolygon (other, parent)
{

}

void WimaGlobalMeasurementPolygon::addVehicle(Vehicle *vehicle)
{
    if(vehicle != nullptr){
        _vehicleList->append(vehicle);
        emit vehicleListChanged();
    }
}

void WimaGlobalMeasurementPolygon::removeVehicle(int vehicleIndex)
{
    if(vehicleIndex >= 0 && vehicleIndex < _vehicleList->count()){
        _vehicleList->removeAt(vehicleIndex);
        emit vehicleListChanged();
    }
}

void WimaGlobalMeasurementPolygon::recalculatesubPolygons()
{
    int vehicleCount = _vehicleList->count();
    QScopedPointer<QList<QGCMapPolygon*>> listQGCPoly(this->splitPolygonArea(vehicleCount));

    int polyCount = listQGCPoly->size();
    _subPolygons->clear();
    for(int i = 0; i < polyCount; i++){
        WimaVehicleMeasurementPolygon* subPoly = new WimaVehicleMeasurementPolygon(listQGCPoly->takeAt(i), this);
        _subPolygons->append(subPoly);
    }
}

void WimaGlobalMeasurementPolygon::removeAllVehicles()
{
    int counter = _vehicleList->count()-1;

    while(counter >= 0){
        _vehicleList->removeAt(0);
        counter--;
    }

    emit vehicleListChanged();
}


