#ifndef ATTRIBUTETRACKINGMODEL_H
#define ATTRIBUTETRACKINGMODEL_H

#include <QAbstractItemModel>
#include <QUrl>
#include <QBuffer>
#include <QQuickImageProvider>
#include "qgsvectorlayer.h"
#include "qgsquickmapsettings.h"
#include "layertreemodel.h"

class FieldItem : public QObject{

    Q_OBJECT
    Q_PROPERTY(QString name READ name )
    Q_PROPERTY(QString alias READ alias )

public:
    FieldItem(QString name, QString alias)

    {
        this->mName = name;
        this->mAlias = alias;
    }

    FieldItem(const FieldItem &field)
    {
        this->mName = field.name();
        this->mAlias = field.alias();
    }

    FieldItem& operator=(FieldItem other)
    {
        this->mName = other.name();
        this->mAlias = other.alias();

        return *this;
    }

    QString name() const { return mName; }
    QString alias() const { return mAlias; }

private:
    QString mName;
    QString mAlias;

};

class AttributeTracker{

public:
    QgsVectorLayer* layer;
    QString attribute;
//    QModelIndex layerTreeIndex;
};

class AttributeTrackingModel : public QObject, public QQuickImageProvider
{
    Q_OBJECT

public:
    AttributeTrackingModel(/*FlatLayerTreeModel* layerTreeModel*/);

    Q_INVOKABLE QList<QObject *> getLayerFields(QgsVectorLayer *layer);
    Q_INVOKABLE bool tracking();

    Q_INVOKABLE QgsQuickMapSettings *mapSettings() const;
    Q_INVOKABLE void setMapSettings(QgsQuickMapSettings *newMapSettings);

    QUrl imageToUrl(const QImage& image);

    QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize);

public slots:
    void startTracker(QgsVectorLayer *layer, QString inspectedAttribute, QModelIndex layerTreeIndex);
    void stopTracker();
    void processIdentifyResults(QgsFeatureList results);
    void recieveLocation(QgsPoint location);
    void test(QString msg);

signals:
    void trackerStarted(QgsVectorLayer *layer, QString attribute);
    void trackerStopped();
    void locationRecieved(QString attributeValue, QString featureId);

private:    
    QgsFeatureList identifyLayer(QgsVectorLayer *layer, QgsPoint point)
    {
        int searchRadius = 10;

        QgsFeatureList featureList;

        QgsRectangle r;
        r.setXMinimum( point.x() - searchRadius );
        r.setXMaximum( point.x() + searchRadius );
        r.setYMinimum( point.y() - searchRadius );
        r.setYMaximum( point.y() + searchRadius );

        QgsFeatureRequest req;
        req.setFilterRect( r );
        req.setLimit( 100 );
        req.setFlags( QgsFeatureRequest::ExactIntersect );

        QgsFeatureIterator fit = layer->getFeatures( req );
        QgsFeature f;
        while ( fit.nextFeature( f ) )
          featureList << QgsFeature( f );

        return featureList;
    }

    QUrl imageToUrl(QImage image) const
    {
        QByteArray byteArray;
        QBuffer buffer(&byteArray);
        buffer.open(QIODevice::WriteOnly);
        image.save(&buffer, "png");
        QString base64 = QString::fromUtf8(byteArray.toBase64());
        return QString("data:image/png;base64,") + base64;
    }

    AttributeTracker* mTracker = nullptr;
    QgsQuickMapSettings* mMapSettings = nullptr;
//    FlatLayerTreeModel* mLayerTreeModel = nullptr;
};

#endif // ATTRIBUTETRACKINGMODEL_H
