/* -*-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_DRIVERS_REX_TILE_GROUP_FACTORY
#define OSGEARTH_DRIVERS_REX_TILE_GROUP_FACTORY 1

#include "Common"
#include "GeometryPool"
#include "Loader"
#include "Unloader"
#include "TileNode"
#include "TileNodeRegistry"
#include "RexTerrainEngineOptions"
#include "RenderBindings"
#include "TileDrawable"

#include <osgEarth/TerrainTileModel>
#include <osgEarth/Progress>
#include <osgEarth/TerrainEngineNode>
#include <osgEarth/TileRasterizer>

#include <osgUtil/CullVisitor>

using namespace osgEarth;

#define ENGINE_CONTEXT_TAG "osgEarth::Rex::EngineContext"

namespace osgEarth {
    class TerrainEngine;
}

namespace osgEarth { namespace Drivers { namespace RexTerrainEngine
{
    class SelectionInfo;

    class EngineContext : public osg::Referenced
    {
    public:
        EngineContext(
            const Map*                          map,
            TerrainEngineNode*                  engine,
            GeometryPool*                       geometryPool,
            Loader*                             loader,
            Unloader*                           unloader,
            TileRasterizer*                     rasterizer,
            TileNodeRegistry*                   liveTiles,
            const RenderBindings&               renderBindings,
            const RexTerrainEngineOptions&      options,
            const SelectionInfo&                selectionInfo);
        
        Loader* getLoader() const { return _loader; }

        Unloader* getUnloader() const { return _unloader; }

        const RenderBindings& getRenderBindings() const { return _renderBindings; }

        GeometryPool* getGeometryPool() const { return _geometryPool; }

        osg::ref_ptr<const Map> getMap() const;

        // only call this variant when it's safe to do so (update, cull).
        TerrainEngineNode* getEngine() const { return _terrainEngine.get(); }

        TileNodeRegistry* liveTiles() const { return _liveTiles; }

        const SelectionInfo& getSelectionInfo() const { return _selectionInfo; }

        const RexTerrainEngineOptions& getOptions() const { return _options; }

        ProgressCallback* progress() const { return _progress.get(); }

        void startCull(osgUtil::CullVisitor* cv);

        void endCull(osgUtil::CullVisitor* cv);

        double getElapsedCullTime() const;

        bool maxLiveTilesExceeded() const;

        double getExpirationRange2() const { return _expirationRange2; }

        ModifyBoundingBoxCallback* getModifyBBoxCallback() const { return _bboxCB; }

        bool getUseTextureBorder() const { return false; }

        TileRasterizer* getTileRasterizer() const { return _tileRasterizer; }

    protected:

        virtual ~EngineContext() { }

    public:
        
        osg::observer_ptr<TerrainEngineNode>  _terrainEngine;
        osg::observer_ptr<const Map>          _map;
        int                                   _mainThreadId;
        TileNodeRegistry*                     _liveTiles;
        const RexTerrainEngineOptions&        _options;
        const RenderBindings&                 _renderBindings;
        GeometryPool*                         _geometryPool;
        Loader*                               _loader;
        Unloader*                             _unloader;
        TileRasterizer*                       _tileRasterizer;
        const SelectionInfo&                  _selectionInfo;
        osg::Timer_t                          _tick;
        int                                   _tilesLastCull;
        osg::ref_ptr<ProgressCallback>        _progress;    
        double                                _expirationRange2;
        osg::ref_ptr<ModifyBoundingBoxCallback> _bboxCB;
    };

} } } // namespace osgEarth::Drivers::RexTerrainEngine

#endif // OSGEARTH_DRIVERS_REX_TILE_GROUP_FACTORY
