#include "WimaFlyArea.h"

WimaFlyArea::WimaFlyArea(QObject *parent) : QObject(parent)
  , _polygons             (nullptr)
{

}

WimaFlyArea::WimaFlyArea(const WimaFlyArea &other, QObject *parent): QObject(parent)
{
    *this = other;
}

const WimaFlyArea& WimaFlyArea::operator=(const WimaFlyArea& other)
{
    this->_polygons = other._polygons;
    return *this;
}

void WimaFlyArea::_init()
{
    _polygons = new QmlObjectListModel(this);
}



void WimaFlyArea::appendFlyAreaPolygon()
{
    QGCMapPolygon *newPolygon  = new QGCMapPolygon();

    _polygons->append(newPolygon);
    int index = _polygons->count()-1;
    selectCurrentPolygon(index);

    emit polygonsChanged();
}

void WimaFlyArea::removeFlyAreaPolygon(int index)
{
    if(index >= 0 && index < _polygons->count()) {
        _polygons->removeAt(index);

        emit polygonsChanged();
    }
}

void WimaFlyArea::selectCurrentPolygon(int index)
{
    if(index >= 0 && index < _polygons->count()) {

        disselectAllPolygons();

        _currentPolygonIndex = index;
        _currentPolygonItem  = qobject_cast<QGCMapPolygon*>(_polygons->get(index));
        _currentPolygonItem->setInteractive(true);

        if(index != _currentPolygonIndex){
            emit currentPolygonItemChanged(_currentPolygonItem);
            emit currentPolygonIndexChanged(index);
        }

    }
}

void WimaFlyArea::disselectAllPolygons()
{
    for (int i = 0; i < _polygons->count(); i++) {
        QGCMapPolygon* polygon = qobject_cast<QGCMapPolygon*>(_polygons->get(i));
        polygon->setInteractive(false);
    }
}

/*void WimaFlyArea::setBottomLayerAltitude(double alt)
{
    if(_bottomLayerAltitude != alt){
        _bottomLayerAltitude = alt;
        emit bottomLayerAltitudeChanged(_bottomLayerAltitude);
    }

}*/

