/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 * Copyright 2016 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_COMPOSITE_TILE_SOURCE_H
#define OSGEARTH_COMPOSITE_TILE_SOURCE_H 1

#include <osgEarth/Common>
#include <osgEarth/TileSource>
#include <osgEarth/ImageLayer>
#include <osgEarth/ElevationLayer>

namespace osgEarth
{
    /**
     * Use this class to configure a CompositeTileSource.
     */
    class OSGEARTH_EXPORT CompositeTileSourceOptions : public TileSourceOptions
    {
    public:
        CompositeTileSourceOptions( const TileSourceOptions& options =TileSourceOptions() );

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

        /** Add a component described by an image layer configuration. */
        void add( const ImageLayerOptions& options );

        /**
         * Adds a component described by an elevation layer configuration
         */
        void add( const ElevationLayerOptions& options );

    public:
        virtual Config getConfig() const;

    protected:
        virtual void mergeConfig( const Config& conf );

    private:
        void fromConfig( const Config& conf );

        struct Component {
            optional<ImageLayerOptions> _imageLayerOptions;
            optional<ElevationLayerOptions> _elevationLayerOptions;
            osg::ref_ptr<TerrainLayer>    _layer;
        };

        typedef std::vector<Component> ComponentVector;
        ComponentVector _components;

        friend class CompositeTileSource;        
    };

    //--------------------------------------------------------------------

    /**
     * A "virtual" TileSource that contains one or more other TileSources and 
     * composites them into a single TileSource for a layer to use.
     */
    class OSGEARTH_EXPORT CompositeTileSource : public TileSource
    {
    public:
        /** Constructs a new composite tile source */
        CompositeTileSource( const TileSourceOptions& options =TileSourceOptions() );

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

        /**
         * Adds a pre-existing ImageLayer to the composite layer.
         * Note: You can only add tile sources BEFORE the tile source is initialized,
         * i.e., before you add it to the map.
         * Returns true upon success; false if the composite tile source is already
         * initialized.
         */
        bool add( ImageLayer* layer );


        /**
         * Adds a pre-existing ElevationLayer to the composite layer.
         * Note: You can only add tile sources BEFORE the tile source is initialized,
         * i.e., before you add it to the map.
         * Returns true upon success; false if the composite tile source is already
         * initialized.
         */
        bool add( ElevationLayer* layer );


    public: // TileSource overrides
        
        /** Creates a new image for the given key */
        virtual osg::Image* createImage( 
            const TileKey&        key,
            ProgressCallback*     progress =0 );

        /** Creates a heightfield for the given key */
        virtual osg::HeightField* createHeightField(
            const TileKey&        key,
            ProgressCallback*     progress );

        /** Whether one of the underlying tile source's is dynamic */
        virtual bool isDynamic() const { return _dynamic; }
        
        /** Initializes the tile source */
        Status initialize( const osgDB::Options* dbOptions );

    protected:
        CompositeTileSourceOptions         _options;
        bool                               _initialized;
        bool                               _dynamic;
        osg::ref_ptr<const osgDB::Options> _dbOptions;              

        ElevationLayerVector _elevationLayers;    
        ImageLayerVector _imageLayers;
    };
}

#endif // OSGEARTH_COMPOSITE_TILE_SOURCE_H
