/* -*-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_TILE_FACTORY_H
#define OSGEARTH_ENGINE_OSGTERRAIN_TILE_FACTORY_H 1

#include "OSGTerrainOptions"
#include "TransparentLayer"

#include <osgEarth/Map>
#include <osgEarth/TileSource>
#include <osgEarth/ImageLayer>
#include <osgEarth/ElevationLayer>
#include <osgEarth/Locators>

#include <osgDB/ReaderWriter>
#include <osg/CoordinateSystemNode>
#include <osg/ClusterCullingCallback>
#include <vector>

namespace osgEarth_engine_osgterrain
{
    using namespace osgEarth;
    using namespace osgEarth::Drivers;

    class TerrainNode;
    class StreamingTerrainNode;
    class Tile;
    class StreamingTile;

    /**
    * TileFactory is the main workhorse - it generates osg Nodes for TileKeys.
    */
    class OSGTileFactory : public osg::Referenced
    {
    public:
        OSGTileFactory(
            unsigned                 engineId,
            const MapFrame&          cull_thread_mapf,
            const OSGTerrainOptions& props =OSGTerrainOptions() );

        /** dtor */
        virtual ~OSGTileFactory() { }

    public:
        /**
        * Creates a node graph containing four tiles that correspond to the four
        * subkeys of the provided TileKey.
        */
        osg::Node* createSubTiles(
            const MapFrame&  mapf,
            TerrainNode*     terrain,
            const TileKey&   key,
            bool             populateLayers );

        /**
        * Creates a single terrain tile corresponding to the provided TileKey.
        */
        osg::Node* createTile(
            const MapFrame&  mapf,
            TerrainNode*     terrain,
            const TileKey&   key,
            bool             populateLayers,
            bool             wrapInPagedLOD,
            bool             fallback,
            bool&            out_validData);


        CustomColorLayerRef* createImageLayer(
            const MapInfo&    mapInfo,
            ImageLayer*       layer,
            const TileKey&    key,
            ProgressCallback* progress);

        osgTerrain::HeightFieldLayer* createHeightFieldLayer(
            const MapFrame& mapf,
            const TileKey&  key,
            bool            exactOnly );

        /**
        * Gets the properties that customize how this engine renders tile data.
        */
        const OSGTerrainOptions& getTerrainOptions() const;

        /**
        * Gets a pagedLOD child URI given a tile key.
        */
        std::string createURI( unsigned int id, const TileKey& key );

        /**
         * Wraps a tile in a paged LOD and setup up all its parameters
         */
        osg::Node* prepareTile( 
            Tile*            tile, 
            TerrainNode*         terrain,
            const MapInfo&   mapInfo,
            bool             wrapInPagedLOD );

        bool createValidGeoImage(
            ImageLayer*       layer,
            const TileKey&    key,
            GeoImage&         out_image,
            TileKey&          out_actualTileKey,
            ProgressCallback* progress  = 0);

        osg::Matrixd getTransformFromExtents(double minX, double minY, double maxX, double maxY) const;

        // checks whether more data exists below the specified key's level of detail
        bool hasMoreLevels( Map* map, const TileKey& key );

        static osg::HeightField* createEmptyHeightField(
            const TileKey& key,
            unsigned       numCols =8,
            unsigned       numRows =8 );

        osgTerrain::HeightFieldLayer* createPlaceholderHeightfieldLayer(
            osg::HeightField* ancestorHF,
            const TileKey&    ancestorKey,
            const TileKey&    key,
            GeoLocator*       locator );

    protected:

        osg::Node* createPlaceholderTile(
            const MapFrame&   mapf,
            StreamingTerrainNode* terrain,
            const TileKey&    key );

        osg::Node* createPopulatedTile(
            const MapFrame&  mapf,
            TerrainNode*         terrain,
            const TileKey&   key,
            bool             wrapInPagedLOD,
            bool             fallback,
            bool&            out_validData);

        void addPlaceholderImageLayers( Tile* tile, Tile* ancestorTile );

        void addPlaceholderHeightfieldLayer(
            StreamingTile* tile,
            StreamingTile* ancestorTile,
            GeoLocator*    defaultLocator,
            const TileKey& key,
            const TileKey& ancestorKey );

        osg::ClusterCullingCallback* createClusterCullingCallback(
            Tile*                tile,
            osg::EllipsoidModel* et );

        void init();

    protected:

        unsigned          _engineId;
        const MapFrame&   _cull_thread_mapf;
        OSGTerrainOptions _terrainOptions;
    };

} // namespace osgEarth_engine_osgterrain

#endif // OSGEARTH_ENGINE_OSGTERRAIN_TILE_FACTORY_H
