/* -*-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_OSGTERRAIN_STREAMING_TERRAIN
#define OSGEARTH_ENGINE_OSGTERRAIN_STREAMING_TERRAIN 1

#include "TerrainNode"
#include "StreamingTile"
#include <osgEarth/TaskService>

namespace osgEarth_engine_osgterrain
{
    using namespace osgEarth;

    /**
     * Terrain implementation that supports the SEQUENTIAL and PREEMPTIVE loading policies.
     */
    class StreamingTerrainNode : public TerrainNode
    {
    public:
        StreamingTerrainNode(
            const MapFrame& update_mapf,
            const MapFrame& cull_mapf,
            OSGTileFactory* factory,
            bool            quickReleaseGLObjects );

        virtual const char* libraryName() const { return "osgEarth"; }
        virtual const char* className() const { return "StreamingTerrainNode"; }
        
        //override
        virtual Tile* createTile(const TileKey& key, GeoLocator* locator) const;

    public:

        TaskService* getImageryTaskService(int layerId);
        TaskService* getElevationTaskService();
        TaskService* getTileGenerationTaskService();

        /**
         * Updates the catalog of task service threads - this gets called by the OSGTerrainEngine
         * in response to a change in the Map's data model. The map frame is that of the terrain
         * engine.
         */
        void updateTaskServiceThreads( const MapFrame& mapf );

        const LoadingPolicy& getLoadingPolicy() const { return _loadingPolicy; }

    protected:

	    virtual ~StreamingTerrainNode();

        //override
        virtual unsigned getNumActiveTasks() const;

        //override
        virtual void updateTraversal( osg::NodeVisitor& nv );

    private:

        TaskService* createTaskService( const std::string& name, int id, int numThreads );
        TaskService* getTaskService( int id );

        void refreshFamily( const MapInfo& info, const TileKey& key, StreamingTile::Relative* family, bool tileTableLocked );

        typedef std::map< int, osg::ref_ptr< TaskService > > TaskServiceMap;

        TaskServiceMap     _taskServices;
        OpenThreads::Mutex _taskServiceMutex;
        int                _numLoadingThreads;
        LoadingPolicy      _loadingPolicy;
        UID                _elevationTaskServiceUID;
    };

} // namespace osgEarth_engine_osgterrain

#endif // OSGEARTH_ENGINE_OSGTERRAIN_STREAMING_TERRAIN
