#include "WimaAreaData.h"

WimaAreaData::WimaAreaData(QObject *parent)
    : QObject(parent)
{
    _maxAltitude = 0;
}

/*!
 * \fn double WimaAreaData::maxAltitude()
 *
 * Returns the maximum altitude at which vehicles are allowed to fly.
 */
double WimaAreaData::maxAltitude() const
{
    return _maxAltitude;
}

/*!
 * \fn double WimaAreaData::maxAltitude()
 *
 * Returns the path (vertex list defining the \l {Simple Polygon}).
 */
QVariantList WimaAreaData::path() const
{
    return _path;
}

QGeoCoordinate WimaAreaData::center() const
{
    return _center;
}

QList<QGeoCoordinate> WimaAreaData::coordinateList() const
{
    QList<QGeoCoordinate> coordinateList;

    for ( auto coorinate : _path)
        coordinateList.append(coorinate.value<QGeoCoordinate>());

    return coordinateList;
}

bool WimaAreaData::containsCoordinate(const QGeoCoordinate &coordinate) const
{
    using namespace PlanimetryCalculus;
    using namespace PolygonCalculus;
    using namespace GeoUtilities;

    if (_path.size() > 2) {
        QPolygonF polygon = toQPolygonF(toCartesian2D(this->coordinateList(), coordinate));
        return PlanimetryCalculus::contains(polygon, QPointF(0,0));
    } else
        return false;
}


/*!
 * \fn void WimaAreaData::setMaxAltitude(double maxAltitude)
 *
 * Sets the maximum altitude member to \a maxAltitude and emits the \c maxAltitudeChanged() signal if
 * \a maxAltitude differs from the members value.
 */
void WimaAreaData::setMaxAltitude(double maxAltitude)
{
    if ( !qFuzzyCompare(_maxAltitude, maxAltitude) ) {
        _maxAltitude = maxAltitude;

        emit maxAltitudeChanged(_maxAltitude);
    }
}

void WimaAreaData::setPath(const QVariantList &coordinateList)
{
    _path.clear();
    _path.append(coordinateList);
}

void WimaAreaData::setCenter(const QGeoCoordinate &center)
{
    if (_center != center) {
        _center = center;

        emit centerChanged();
    }
}

/*!
 * \fn void WimaAreaData::assign(const WimaAreaData &other)
 *
 * Assigns \a other to the invoking object
 */
void WimaAreaData::assign(const WimaAreaData &other)
{
    setMaxAltitude(other.maxAltitude());
    setPath(other.path());
    setCenter(other.center());
}

void WimaAreaData::assign(const WimaArea &other)
{
    setMaxAltitude(other.maxAltitude());
    setPath(other.path());    
    setCenter(other.center());
}


/*!
 * \fn void WimaAreaData::setPath(const QList<QGeoCoordinate> &coordinateList)
 *
 * Sets the path member to \a coordinateList by copying all entries of \a coordinateList.
 * Emits the \c pathChanged() signal.
 */
void WimaAreaData::setPath(const QList<QGeoCoordinate> &coordinateList)
{
    _path.clear();

    // copy all coordinates to _path
    for(int i = 0; i < coordinateList.size(); i++) {
        _path.append(QVariant::fromValue(coordinateList.value(i)));
    }

    emit pathChanged(_path);
}

/*!
 * \class WimaArea::WimaAreaData
 * \brief Class to store and exchange data of a \c WimaArea Object.
 * Class to store and exchange data of a \c WimaArea Object. In contrast to \c WimaArea this class
 * does not uses the QGC Fact System. It is designed to exchange data between the \c WimaPlaner and
 * the \c WimaController class. And it is the base class for any derived data objects
 *
 * \sa WimaArea
 */
