#include "WimaGlobalMeasurementPolygon.h"



WimaGlobalMeasurementPolygon::WimaGlobalMeasurementPolygon(QObject *parent):
    WimaPolygon (parent)
{
    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();
}

void WimaGlobalMeasurementPolygon::setBottomLayerAltitude(double altitude)
{
    if(altitude > 0 && altitude != _bottomLayerAltitude){
        _bottomLayerAltitude = altitude;
        emit bottomLayerAltitudeChanged();
    }
}

void WimaGlobalMeasurementPolygon::setNumberOfLayers(int numberOfLayers)
{
    if(numberOfLayers > 0 && _numberOfLayers != numberOfLayers){
        _numberOfLayers = numberOfLayers;
        emit numberOfLayersChanged();
    }
}

void WimaGlobalMeasurementPolygon::setLayerDistance(double distance)
{
    if(distance > 0 && distance != _layerDistance){
        _layerDistance = distance;
        emit layerDistanceChanged();
    }
}
