#include "WimaGOperationArea.h"


const char* WimaGOperationArea::settingsGroup =                   "OperatingArea";
const char* WimaGOperationArea::bottomLayerAltitudeName =         "BottomLayerAltitude";
const char* WimaGOperationArea::numberOfLayersName =              "NumberOfLayers";
const char* WimaGOperationArea::layerDistanceName =               "LayerDistance";
const char* WimaGOperationArea::borderPolygonOffsetName =         "BorderPolygonOffset";

WimaGOperationArea::WimaGOperationArea(QObject *parent)
  : WimaGOperationArea (nullptr, parent)
{

}

WimaGOperationArea::WimaGOperationArea(WimaArea *other, QObject *parent)
  : WimaArea(other, parent)
  , _metaDataMap            (FactMetaData::createMapFromJsonFile(QStringLiteral(":/json/WimaGOperationArea.SettingsGroup.json"), this /* QObject parent */))
  , _bottomLayerAltitude    (settingsGroup, _metaDataMap[bottomLayerAltitudeName])
  , _numberOfLayers         (settingsGroup, _metaDataMap[numberOfLayersName])
  , _layerDistance          (settingsGroup, _metaDataMap[layerDistanceName])
  , _borderPolygonOffset    (settingsGroup, _metaDataMap[borderPolygonOffsetName])
  , _polyline               (new WimaTrackerPolyline(this))
  , _borderPolygon          (new QGCMapPolygon(this))
{
    this->setObjectName("Operation Area");
    _polyline.bindPolygon(this);
    connect(this, &WimaGOperationArea::pathChanged, this, &WimaGOperationArea::recalcBorderPolygon);
    connect(&_borderPolygonOffset, &SettingsFact::rawValueChanged, this, &WimaGOperationArea::recalcBorderPolygon);
    //qWarning("Here I am!");
    //connect(&_polyline, &WimaTrackerPolyline::pathChanged, this, &WimaGOperationArea::polylineChanged );
}

void WimaGOperationArea::addVehicle(WimaVehicle *vehicle)
{
    if(vehicle != nullptr){
        _wimaVehicle = vehicle;
        emit vehicleChanged();
    }
}

void WimaGOperationArea::removeVehicle(int vehicleIndex)
{
    if(vehicleIndex >= 0){
        _wimaVehicle = nullptr;
        emit vehicleChanged();
    }
}

void WimaGOperationArea::setVehicleCorridor(WimaVCorridor *corridor)
{
    if(corridor != nullptr){
        if (corridor != _vehicleCorridor){
            _vehicleCorridor = corridor;
            emit vehicleCorridorChanged(_vehicleCorridor);
        }
        else {
            qWarning("WimaGOperationArea::setVehicleCorridor(): new corridor equals old _vehicleCorridor!");
        }
    }else{
        qWarning("WimaGOperationArea::setVehicleCorridor(): corridor == nullptr!");
    }
}

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::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 WimaGOperationArea::removeAllVehicles()
{
    int count = _vehicleList->count();

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

        emit vehicleListChanged();
    }

}


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

    emit vehiclePolygonsChanged();
}

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

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

void WimaGOperationArea::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.");
    }
}*/



