/* -*-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_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 ),
            _expirationThreshold    ( 300 ),
            _progressive            ( false ),
            _normalMaps             ( true ),
            _normalizeEdges         ( false ),
            _morphTerrain           ( true ),
            _morphImagery           ( true ),
            _mergesPerFrame         ( 20 ),
            _expirationRange        ( 0 )
        {
            setDriver( "rex" );
            fromConfig( _conf );

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

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

    public:
        struct LODOptions {
            optional<unsigned> _lod;
            optional<float> _priorityScale;
            optional<float> _priorityOffset;
        };

    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; }

        /** 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 generate normal map textures. Default is true. */
        optional<bool>& normalMaps() { return _normalMaps; }
        const optional<bool>& normalMaps() const { return _normalMaps; }

        /** Whether to average normal vectors on tile boundaries. Doing so reduces the
         *  the appearance of seams when using lighting, but requires extra CPU work. */
        optional<bool>& normalizeEdges() { return _normalizeEdges; }
        const optional<bool>& normalizeEdges() const { return _normalizeEdges; }

        /** 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; }

        /** Options for specific LODs */
        std::vector<LODOptions>& lods() { return _lods; }
        const std::vector<LODOptions>& lods() const { return _lods; }

    public:
        virtual Config getConfig() const {
            Config conf = TerrainOptions::getConfig();
            conf.set( "skirt_ratio", _skirtRatio );
            conf.set( "color", _color );
            conf.set( "expiration_range", _expirationRange );
            conf.set( "expiration_threshold", _expirationThreshold );
            conf.set( "progressive", _progressive );
            conf.set( "normal_maps", _normalMaps );
            conf.set( "normalize_edges", _normalizeEdges);
            conf.set( "morph_terrain", _morphTerrain );
            conf.set( "morph_imagery", _morphImagery );
            conf.set( "merges_per_frame", _mergesPerFrame );

            if (!_lods.empty()) {
                Config lodsConf("lods");
                for (unsigned i = 0; i < _lods.size(); ++i) {
                    Config lodConf("lod");
                    lodConf.set("lod", _lods[i]._lod);
                    lodConf.set("priority_scale", _lods[i]._priorityScale);
                    lodConf.set("priority_offset", _lods[i]._priorityOffset);
                    lodsConf.add(lodConf);
                }
                conf.set(lodsConf);
            }

            return conf;
        }

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

    private:
        void fromConfig( const Config& conf ) {
            conf.get( "skirt_ratio", _skirtRatio );
            conf.get( "color", _color );
            conf.get( "expiration_range", _expirationRange );
            conf.get( "expiration_threshold", _expirationThreshold );
            conf.get( "progressive", _progressive );
            conf.get( "normal_maps", _normalMaps );
            conf.get( "normalize_edges", _normalizeEdges);
            conf.get( "morph_terrain", _morphTerrain );
            conf.get( "morph_imagery", _morphImagery );
            conf.get( "merges_per_frame", _mergesPerFrame );

            const Config* lods = conf.child_ptr("lods");
            if (lods) {
                for (ConfigSet::const_iterator i = lods->children().begin(); i != lods->children().end(); ++i) {
                    const Config& lodConf = *i;
                    if (!lodConf.empty()) {
                        _lods.push_back(LODOptions());
                        LODOptions& lod = _lods.back();
                        lodConf.get("lod", lod._lod);
                        lodConf.get("priority_scale", lod._priorityScale);
                        lodConf.get("priority_offset", lod._priorityOffset);
                    }
                }
            }
        }

        optional<float>    _skirtRatio;
        optional<Color>    _color;
        optional<float>    _expirationRange;
        optional<unsigned> _expirationThreshold;
        optional<bool>     _progressive;
        optional<bool>     _normalMaps;
        optional<bool>     _normalizeEdges;
        optional<bool>     _morphTerrain;
        optional<bool>     _morphImagery;
        optional<int>      _mergesPerFrame;
        std::vector<LODOptions> _lods;
    };

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

#endif // OSGEARTH_DRIVERS_REX_TERRAIN_ENGINE_OPTIONS
