/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
* Copyright 2008-2010 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 <osgTerrain/Terrain>
#include <osg/CoordinateSystemNode>
#include <osg/ClusterCullingCallback>
#include <vector>

using namespace osgEarth;
using namespace osgEarth::Drivers;

//class osgEarth::Map;
//class osgEarth::MapFrame;
class CustomTerrain;
class CustomTile;

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

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

    /**
    * Creates a single terrain tile corresponding to the provided TileKey.
    */
    osg::Node* createTile(
        const MapFrame& mapf,
        CustomTerrain* terrain,
        const TileKey& key,
        bool populateLayers,
        bool wrapInPagedLOD,
        bool fallback,
        bool &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;

    /**
    * Returns true if all the data needed to contruct a tile from the specified tile key is
    * in a cache.
    */
    bool isCached( const MapFrame& mapf, const TileKey& key ) const;

    /**
     * Returns true is data for all the key's child tiles is cached.
     */
    bool areChildrenCached( const Map* map, const TileKey& parentKey ) const;

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



    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,
        int numCols =8,
        int numRows =8 );


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

    bool createLodBlendedImage(
        UID layerUID, 
        const TileKey& key, 
        const osg::Image* tileImage,
        CustomTerrain* terrain,
        osg::ref_ptr<osg::Image>& output);

protected:

    //virtual ~MapEngine();


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

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

    void addPlaceholderImageLayers(
        CustomTile* tile,
        CustomTile* ancestorTile,
        const ImageLayerVector& imageLayers,
        GeoLocator* defaultLocator,
        const TileKey& key);

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

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

    void init();

protected:

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

#endif // OSGEARTH_ENGINE_OSGTERRAIN_TILE_FACTORY_H
