/* -*-c++-*- */
/* osgEarth - Geospatial SDK for OpenSceneGraph
 * Copyright 2008-2014 Pelican Mapping
 * http://osgearth.org
 *
 * osgEarth is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */
#ifndef OSGEARTH_FEATURES_MVT
#define OSGEARTH_FEATURES_MVT 1


#include <osgEarth/Common>

#ifdef OSGEARTH_HAVE_MVT

#include <osgEarth/FeatureSource>

namespace osgEarth { namespace MVT 
{
    //! Reads features from an MVT stream for the specified tile.
    extern OSGEARTH_EXPORT bool readTile(
        std::istream&  in,
        const TileKey& key,
        FeatureList&   features);

    // Internal serialization options
    class OSGEARTH_EXPORT MVTFeatureSourceOptions : public FeatureSource::Options
    {
    public:
        META_LayerOptions(osgEarth, MVTFeatureSourceOptions, FeatureSource::Options);
        OE_OPTION(URI, url);
        OE_OPTION(int, minLevel);
        OE_OPTION(int, maxLevel);
        virtual Config getConfig() const;
    private:
        void fromConfig(const Config& conf);
    };

} } // osgEarth::MVT


namespace osgEarth
{
    /**
     * Feature Layer that accesses features according to the Mapnik Vector Tiles specification.
     */
    class OSGEARTH_EXPORT MVTFeatureSource : public FeatureSource
    {   
    public: // serialization
        typedef MVT::MVTFeatureSourceOptions Options;

    public:
        META_Layer(osgEarth, MVTFeatureSource, Options, FeatureSource, MVTFeatures);

        //! Location of the resource
        void setURL(const URI& value);
        const URI& getURL() const;

        //! Minimum level of detail to access
        void setMinLevel(const int& value);
        const int& getMinLevel() const;

        //! Maximum level of detail to access
        void setMaxLevel(const int& value);
        const int& getMaxLevel() const;

        typedef void(*FeatureTileCallback)(const TileKey& key, const FeatureList& features, void* context);
        /**
        * Iterates over the tiles in the mbtiles dataset
        */
        void iterateTiles(int zoomLevel, int limit, int offset, const GeoExtent& extent, FeatureTileCallback callback, void* context);

    public: // Layer

        Status openImplementation() override;
        Status closeImplementation() override;

    protected:

        void init() override;

    public: // FeatureSource

        FeatureCursor* createFeatureCursorImplementation(const Query& query, ProgressCallback* progress) const override;
        
        const FeatureSchema& getSchema() const override { return _schema; }

    protected:

        virtual ~MVTFeatureSource();

    private:
        FeatureSchema _schema;
        osg::ref_ptr<osgDB::BaseCompressor> _compressor;
        void* _database;
        unsigned _minLevel;
        unsigned _maxLevel;

        const FeatureProfile* createFeatureProfile();
        void computeLevels();
        bool getMetaData(const std::string& key, std::string& value);
        void closeDatabase();
    };
}

OSGEARTH_SPECIALIZE_CONFIG(osgEarth::MVTFeatureSource::Options);

#else
#pragma message("osgEarth was not built with MVT support")
#endif

#endif // OSGEARTH_FEATURES_MVT