/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 * Copyright 2008-2016 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_BUILDINGS_LAYER_H
#define OSGEARTH_BUILDINGS_LAYER_H

#include "Common"
#include "BuildingLayerOptions"
#include "BuildingCatalog"
#include <osgEarth/TiledModelLayer>
#include <osgEarth/SimplePager>
#include <osgEarth/FeatureSource>
#include <osgEarth/Session>

namespace osgEarth { namespace Buildings 
{
    using namespace osgEarth;

    class BuildingPager;

    class OSGEARTHBUILDINGS_EXPORT BuildingLayer : public TiledModelLayer
    {
    public:
        META_Layer(osgEarthBuildings, BuildingLayer, BuildingLayerOptions, TiledModelLayer, Buildings);

        //! Sets the source of buildings data - call before opening layer
        void setFeatureSource(FeatureSource*);
        FeatureSource* getFeatureSource() const { return options().featureSource().getLayer(); }

        BuildingPager* pager();

    public: // Layer

        // The Node representing this layer.
        osg::Node* getNode() const override;

        //! Extent of the feature layer, if available (INVALID if not)
        const GeoExtent& getExtent() const override;

        //! Report debugging stats
        Stats reportStats() const override;

        //! Minimum available LOD
        unsigned getMinLevel() const override;

        //! Maximum available LOD
        unsigned getMaxLevel() const override;

        //! Profile of underlying data source
        const Profile* getProfile() const override;
        

    protected: // Layer

        // opens the layer and returns the status
        Status openImplementation() override;
        
        // called by the map when this layer is added
        void addedToMap(const class Map*) override;

        // called by the map when this layer is removed
        void removedFromMap(const class Map*) override;

        // post-ctor initialization
        void init() override;

        //! from TiledModelLayer
        osg::ref_ptr<osg::Node> createTileImplementation(const TileKey& key, ProgressCallback* progress) const override;

    private:

        osg::ref_ptr<osg::Group> _root;
        osg::ref_ptr<BuildingCatalog> _catalog;
        osg::ref_ptr<Session> _session;

        void createSceneGraph();
        void destroySceneGraph();
        
    protected:

        //! Destructor
        virtual ~BuildingLayer();
    };
} }

#endif // OSGEARTH_BUILDINGS_EXTENSION_H
