/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
* Copyright 2008-2013 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_ENGINE_MP_TILE_PAGED_LOD
#define OSGEARTH_ENGINE_MP_TILE_PAGED_LOD 1

#include "Common"
#include "TileNodeRegistry"
#include <osg/PagedLOD>
#include <osgEarth/ThreadingUtils>

using namespace osgEarth;

namespace osgEarth_engine_mp
{
    /**
     * TilePagedLOD is an extension to osg::PagedLOD that supports the tile registry.
     */
    class TilePagedLOD : public osg::PagedLOD
    {
    public:
        TilePagedLOD(
            const UID&        engineUID,
            TileNodeRegistry* liveTiles,
            TileNodeRegistry* deadTiles);

        /** The tilenode in this group */
        TileNode* getTileNode();
        void setTileNode(TileNode* tilenode);

    public: // osg::Group

        /** called by the OSG DatabasePager when a paging result is ready. */
        bool addChild( osg::Node* node );

    public: // osg::PagedLOD

        /** override to manage the tile node registries. */
        bool removeExpiredChildren(double expiryTime, unsigned expiryFrame, osg::NodeList& removedChildren);

    protected:
        virtual ~TilePagedLOD();

    private:
        osg::ref_ptr<TileNodeRegistry> _live;
        osg::ref_ptr<TileNodeRegistry> _dead;
        UID                            _engineUID;
        Threading::Mutex               _updateMutex;
    };

} // namespace osgEarth_engine_mp

#endif // OSGEARTH_ENGINE_MP_TILE_PAGED_LOD
