/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit 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_TERRAIN_ENGINE_OPTIONS
#define OSGEARTH_DRIVERS_REX_TERRAIN_ENGINE_OPTIONS 1

#include <osgEarth/Common>
#include <osgEarth/TerrainOptions>
#include <osgEarthSymbology/Color>
#include <osg/LOD>

namespace osgEarth { namespace Drivers { namespace RexTerrainEngine
{
    using namespace osgEarth;
    using namespace osgEarth::Symbology;

    /**
     * Options for configuring the Rex Engine driver.
     */
    class RexTerrainEngineOptions : public TerrainOptions // NO EXPORT (header-only)
    {
    public:
        RexTerrainEngineOptions( const ConfigOptions& options =ConfigOptions() ) : TerrainOptions( options ),
            _skirtRatio             ( 0.0f ),
            _color                  ( Color::White ),
            //_quickRelease           ( false ),
            _expirationThreshold    ( 300 ),
            _progressive            ( false ),
            _highResolutionFirst    ( true ),
            _normalMaps             ( true ),
            _morphTerrain           ( true ),
            _morphImagery           ( true ),
            _mergesPerFrame         ( 20 ),
            _expirationRange        ( 0 )
        {
            setDriver( "rex" );
            fromConfig( _conf );

            // override.
            enableMercatorFastPath().init( false );
        }

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

    public:
        /** Ratio of terrain tile skirt height to tile radius */
        optional<float>& heightFieldSkirtRatio() { return _skirtRatio; }
        const optional<float>& heightFieldSkirtRatio() const { return _skirtRatio; }

        /** The color of the globe surface where no images are applied */
        optional<Color>& color() { return _color; }
        const optional<Color>& color() const { return _color; }

#if 0
        /** Whether to run a post-render process that releases GL objects as quickly as
          * possible, freeing up memory faster */
        optional<bool>& quickReleaseGLObjects() { return _quickRelease; }
        const optional<bool>& quickReleaseGLObjects() const { return _quickRelease; }
#endif

        /** Minimum distance at which tiles will page out of memory once loaded */
        optional<float>& expirationRange() { return _expirationRange; }
        const optional<float>& expirationRange() const { return _expirationRange; }

        /** Number of terrain tiles to keep in memory before expiring usused tiles. */
        optional<unsigned>& expirationThreshold() { return _expirationThreshold; }
        const optional<unsigned>& expirationThreshold() const { return _expirationThreshold; }

        /** Whether to finish loading a tile's data before subdividing */
        optional<bool>& progressive() { return _progressive; }
        const optional<bool>& progressive() const { return _progressive; }

        /** Whether to load higher-resolution data first. */
        optional<bool>& highResolutionFirst() { return _highResolutionFirst; }
        const optional<bool>& highResolutionFirst() const { return _highResolutionFirst; } 

        /** Whether to generate normal map textures. Default is true. */
        optional<bool>& normalMaps() { return _normalMaps; }
        const optional<bool>& normalMaps() const { return _normalMaps; }

        /** Whether to morph terrain data between terrain tile LODs. */
        optional<bool>& morphTerrain() { return _morphTerrain; }
        const optional<bool>& morphTerrain() const { return _morphTerrain; }

        /** Whether to morph imagery between terrain tile LODs. */
        optional<bool>& morphImagery() { return _morphImagery; }
        const optional<bool>& morphImagery() const { return _morphImagery; }

        /** Maximum number of tile data merges permitted per frame. 0 = infinity. */
        optional<int>& mergesPerFrame() { return _mergesPerFrame; }
        const optional<int>& mergesPerFrame() const { return _mergesPerFrame; }


    protected:
        virtual Config getConfig() const {
            Config conf = TerrainOptions::getConfig();
            conf.updateIfSet( "skirt_ratio", _skirtRatio );
            conf.updateIfSet( "color", _color );
            conf.updateIfSet( "quick_release_gl_objects", _quickRelease );
            conf.updateIfSet( "expiration_range", _expirationRange );
            conf.updateIfSet( "expiration_threshold", _expirationThreshold );
            conf.updateIfSet( "progressive", _progressive );
            conf.updateIfSet( "high_resolution_first", _highResolutionFirst );
            conf.updateIfSet( "normal_maps", _normalMaps );
            conf.updateIfSet( "morph_terrain", _morphTerrain );
            conf.updateIfSet( "morph_imagery", _morphImagery );
            conf.updateIfSet( "merges_per_frame", _mergesPerFrame );

            return conf;
        }

        virtual void mergeConfig( const Config& conf ) {
            TerrainOptions::mergeConfig( conf );
            fromConfig( conf );
        }

    private:
        void fromConfig( const Config& conf ) {
            conf.getIfSet( "skirt_ratio", _skirtRatio );
            conf.getIfSet( "color", _color );
            conf.getIfSet( "quick_release_gl_objects", _quickRelease );
            conf.getIfSet( "expiration_range", _expirationRange );
            conf.getIfSet( "expiration_threshold", _expirationThreshold );
            conf.getIfSet( "progressive", _progressive );
            conf.getIfSet( "high_resolution_first", _highResolutionFirst );
            conf.getIfSet( "normal_maps", _normalMaps );
            conf.getIfSet( "morph_terrain", _morphTerrain );
            conf.getIfSet( "morph_imagery", _morphImagery );
            conf.getIfSet( "merges_per_frame", _mergesPerFrame );
        }

        optional<float>    _skirtRatio;
        optional<Color>    _color;
        optional<bool>     _quickRelease;
        optional<float>    _expirationRange;
        optional<unsigned> _expirationThreshold;
        optional<bool>     _progressive;
        optional<bool>     _highResolutionFirst;
        optional<bool>     _normalMaps;
        optional<bool>     _morphTerrain;
        optional<bool>     _morphImagery;
        optional<int>      _mergesPerFrame;
    };

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

#endif // OSGEARTH_DRIVERS_REX_TERRAIN_ENGINE_OPTIONS
