#include "attributetrackingmodel.h"

#include <QDebug>
#include <QUrl>
#include <QBuffer>
#include "qgssymbol.h"
#include "qgsrenderer.h"
#include "qgscategorizedsymbolrenderer.h"


AttributeTrackingModel::AttributeTrackingModel()
    : QQuickImageProvider( Image ) { }

QList<QObject *> AttributeTrackingModel::getLayerFields(QgsVectorLayer *layer)
{
    //qDebug() << "getLayerFields()";
    QList<QObject *> fieldItems;

    if(!layer){ //i should not allow calling this method with no layer context
        //qDebug() << "no layer";
        return fieldItems;
    }

    QgsFields fields = layer->fields();
    
    for(int i = 0 ; i < fields.count(); i++){
        //qDebug() << "Name: " << fields[i].alias();
        //qDebug() << "Value: " << fields[i].name();
        fieldItems.append(new FieldItem(fields[i].name(), fields[i].alias()));
    }
    
    //FieldItem test = *dynamic_cast<FieldItem *>(fieldItems[3]);

    //qDebug() << test.alias();
    //qDebug() << test.name();


    return fieldItems;
}

void AttributeTrackingModel::startTracker(QgsVectorLayer *layer, QString inspectedAttribute, QModelIndex layerTreeIndex)
{
    if(!mTracker)
        mTracker = new AttributeTracker();

    mTracker->attribute = inspectedAttribute;
    mTracker->layer = layer;
    //mTracker->layerTreeIndex = layerTreeIndex;

    emit trackerStarted(mTracker->layer, mTracker->attribute);
}

void AttributeTrackingModel::stopTracker()
{
    delete(mTracker);
    mTracker = nullptr;

    emit trackerStopped();
}

void AttributeTrackingModel::processIdentifyResults(QgsFeatureList results)
{
    if(results.count() < 1){
        emit locationRecieved("NO FEATURE FOUND", "");
        return;
    }

    QgsFeature result = results.first();

    QString attributeValue = result.attribute(mTracker->attribute).toString();

//    QgsRenderContext context = QgsRenderContext::fromMapSettings(mMapSettings->mapSettings());
//    QgsFeatureRenderer * renderer =  mTracker->layer->renderer();
//    QgsCategorizedSymbolRenderer* symbolRenderer = QgsCategorizedSymbolRenderer::convertFromRenderer(renderer);

//    renderer->startRender(context, mTracker->layer->fields());
//    QgsSymbol* symbol = renderer->originalSymbolForFeature(result, context);
//    renderer->stopRender(context);



    test(mTracker->layer->id());

    //QImage image = symbol->asImage(QSize(40,40));

    //QString image = mLayerTreeModel->data(mTracker->layerTreeIndex, FlatLayerTreeModel::LegendImage).toString();

    emit locationRecieved(attributeValue, QString::number(result.id()));
}

void AttributeTrackingModel::recieveLocation(QgsPoint location)
{
    processIdentifyResults(identifyLayer(mTracker->layer, location));
}

bool AttributeTrackingModel::tracking()
{
    return mTracker != nullptr;
}

QgsQuickMapSettings *AttributeTrackingModel::mapSettings() const
{
    return mMapSettings;
}

void AttributeTrackingModel::setMapSettings(QgsQuickMapSettings *newMapSettings)
{
    test("setMapSettings");
    mMapSettings = newMapSettings;
}

QImage AttributeTrackingModel::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
    if(id == "")
        return QImage();

    QgsRenderContext context = QgsRenderContext::fromMapSettings(mMapSettings->mapSettings());
    QgsFeatureRenderer * renderer =  mTracker->layer->renderer();
    QgsCategorizedSymbolRenderer* symbolRenderer = QgsCategorizedSymbolRenderer::convertFromRenderer(renderer);

    renderer->startRender(context, mTracker->layer->fields());
    QgsSymbol* symbol = renderer->originalSymbolForFeature(mTracker->layer->getFeature(id.toLongLong()), context);
    renderer->stopRender(context);

    QSize sizeValue = *size;

    qDebug() << "size.width = " << size->width();
    qDebug() << "size.height = " << size->height();

    qDebug() << "requestedSize.width = " << requestedSize.width();
    qDebug() << "requestedSize.height = " << requestedSize.height();

    return symbol->asImage(QSize(requestedSize.width() / 5, requestedSize.height() / 5));
}

void AttributeTrackingModel::test(QString msg)
{    
    qDebug() << msg;
}







