#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])
  , _vehicleList            (new QmlObjectListModel(this))
  , _vehiclePolygons        (new QmlObjectListModel(this))
{
    this->setObjectName("Operating Area");
    _polyline.bindPolygon(this->polygon());
}

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

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();
    _vehiclePolygons->clear();
    for(int i = 0; i < polyCount; i++){
        WimaVehicleMeasurementPolygon* subPoly = new WimaVehicleMeasurementPolygon(listQGCPoly->takeAt(i), this);
        _vehiclePolygons->append(subPoly);
    }
}

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

    if(count > 0){
        do{
            _vehicleList->removeAt(0);
            count--;
        }while(count > 0);

        emit vehicleListChanged();
    }

}


void WimaGlobalMeasurementPolygon::addVehiclePolygon()
{
    _vehiclePolygons->append(new WimaVehicleMeasurementPolygon(this));

    emit vehiclePolygonsChanged();
}

void WimaGlobalMeasurementPolygon::removeVehiclePolygon(int polygonIndex)
{
    if(polygonIndex >= 0 && polygonIndex < _vehiclePolygons->count()){
        _vehiclePolygons->removeAt(polygonIndex);

        emit vehiclePolygonsChanged();
    }else {
        qWarning("Index out of bounds!");
    }
}

void WimaGlobalMeasurementPolygon::removeVehiclePolygon(WimaVehicleMeasurementPolygon *wimaPolygon)
{
    if(wimaPolygon != nullptr){
        QObject* removedPolygon = _vehiclePolygons->removeOne(wimaPolygon);
        if(removedPolygon){
            emit vehiclePolygonsChanged();
        }else {
            qWarning("Polygon not inside polygon list.");
        }
    }else {
        qWarning("Not a valid Polygon.");
    }
}



