#pragma once

#include "QmlObjectListModel.h"

#include <QVector>
#include <QString>

template <class PolygonType, template <class, class...> class ContainerType = QVector>
class GenericPolygonArray
{
public:
    GenericPolygonArray() {}
    GenericPolygonArray(const GenericPolygonArray &other) :
        _polygons(other._polygons), _dirty(true)
    {}
    ~GenericPolygonArray(){
        _objs.clearAndDeleteContents();
    }

    class QmlObjectListModel *QmlObjectListModel(){
        if (_dirty)
            _updateObjects();
        _dirty = false;
        return &_objs;
    }

    const ContainerType<PolygonType> &polygons() const {
        return _polygons;
    }

    ContainerType<PolygonType> &polygons(){
        _dirty = true;
        return _polygons;
    }

    GenericPolygonArray &operator =(const GenericPolygonArray &other){
        this->_polygons = other._polygons;
        this->_dirty = true;
        return *this;
    }



private:
    void _updateObjects(void){
        _objs.clearAndDeleteContents();
        for (long i=0; i<_polygons.size(); ++i){
            _objs.append(new PolygonType(_polygons[i]));
        }
    }

    ContainerType<PolygonType> _polygons;
    class QmlObjectListModel   _objs;
    bool                       _dirty;

};


