#pragma once

#include <QObject>
#include "WimaArea.h"
#include "QGCMapPolyline.h"


class WimaTrackerPolyline : public QGCMapPolyline
{
    Q_OBJECT
public:
    WimaTrackerPolyline(QObject *parent = nullptr);
    WimaTrackerPolyline(QGCMapPolyline *other, QObject *parent = nullptr);

    Q_PROPERTY(int                      startVertexIndex    READ startVertexIndex   WRITE setStartVertexIndex   NOTIFY startVertexIndexChanged)
    Q_PROPERTY(int                      endVertexIndex      READ endVertexIndex     WRITE setEndVertexIndex     NOTIFY endVertexIndexChanged)

    // Property accessors
    int                     startVertexIndex    (void) const { return _startVertexIndex;}
    int                     endVertexIndex      (void) const { return _endVertexIndex;}


    //Property setters
    Q_INVOKABLE  void       bindPolygon             (WimaArea* polygon);
    Q_INVOKABLE  void       unbindPolygon           ();
    Q_INVOKABLE  void       swapEndPoints           ();
    Q_INVOKABLE  void       setStartVertexIndex     (int PolygonVertexIndex);
    Q_INVOKABLE  void       setEndVertexIndex       (int PolygonVertexIndex);

    // Methodes
    // calls updateEndVertex() if VertexIndex == _endVertexIndex, calls updateStartVertex() if VertexIndex == _startVertexIndex, or
    // calls both if VertexIndex != _endVertexIndex && VertexIndex != _startVertexIndex
    Q_INVOKABLE  void       snapVertex            (int VertexIndex);
    // snaps the end vertex to the closest coordinate of _boundArea when called
                 void       snapEndVertex         ();
    // snaps the start vertex to the closest coordinate of _boundArea when called
                 void       snapStartVertex       ();





signals:
    void startVertexIndexChanged    (void);
    void endVertexIndexChanged      (void);

public slots:
    void recalcPolyline(void);

private:
    WimaArea*                   _boundArea;
    // this->vertexCoordinate(this->count()-1) and _boundArea->vertexCoordinate(_startVertexIndex) are linked
    int                         _startVertexIndex;
    // this->vertexCoordinate(0) and _boundArea->vertexCoordinate(_endVertexIndex) are linked
    int                         _endVertexIndex;

};