/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 * Copyright 2015 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_MP_TERRAIN_ENGINE_OPTIONS
#define OSGEARTH_DRIVERS_MP_TERRAIN_ENGINE_OPTIONS 1

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

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

    /**
     * Options for configuring the MP Engine driver.
     */
    class MPTerrainEngineOptions : public TerrainOptions // NO EXPORT (header-only)
    {
    public:
        MPTerrainEngineOptions( const ConfigOptions& options =ConfigOptions() ) : TerrainOptions( options ),
            _skirtRatio        ( 0.05 ),
            _quickRelease      ( true ),
            _normalizeEdges    ( false ),
            _rangeMode         ( osg::LOD::DISTANCE_FROM_EYE_POINT ),
            _tilePixelSize     ( 256 ),
            _color             ( Color::White ),
            _incrementalUpdate ( false ),
            _smoothing         ( false )
         {
            setDriver( "mp" );
            fromConfig( _conf );
        }

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

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

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

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

        /** Mode to use when calculating LOD switching distances */
        optional<osg::LOD::RangeMode>& rangeMode() { return _rangeMode;}
        const optional<osg::LOD::RangeMode>& rangeMode() const { return _rangeMode;}

        /** The size of the tile, in pixels, when using rangeMode = PIXEL_SIZE_ON_SCREEN */
        optional<float>& tilePixelSize() { return _tilePixelSize; }
        const optional<float>& tilePixelSize() const { return _tilePixelSize; }

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

        /**
         * Whether to create smooth transitions between elevation datasets at differing LODs
         * by inheriting low-resolution data from parent tiles and filling in the new data.
         * When smoothing is on, elevation may not be "true" in all locations, since the engine
         * will interpolate values from low LODs. Defaults to FALSE.
         */
        optional<bool>& elevationSmoothing() { return _smoothing; }
        const optional<bool>& elevationSmoothing() const { return _smoothing; }

    public:

        /** @deprecated */
        optional<bool>& incrementalUpdate() { return _incrementalUpdate; }
        const optional<bool>& incrementalUpdate() const { return _incrementalUpdate; }

    protected:
        virtual Config getConfig() const {
            Config conf = TerrainOptions::getConfig();
            conf.updateIfSet( "skirt_ratio", _skirtRatio );
            conf.updateIfSet( "quick_release_gl_objects", _quickRelease );
            conf.updateIfSet( "normalize_edges", _normalizeEdges);
            conf.updateIfSet( "tile_pixel_size", _tilePixelSize );
            conf.updateIfSet( "range_mode", "PIXEL_SIZE_ON_SCREEN", _rangeMode, osg::LOD::PIXEL_SIZE_ON_SCREEN );
            conf.updateIfSet( "range_mode", "DISTANCE_FROM_EYE_POINT", _rangeMode, osg::LOD::DISTANCE_FROM_EYE_POINT);
            conf.updateIfSet( "color", _color );
            conf.updateIfSet( "incremental_update", _incrementalUpdate );
            conf.updateIfSet( "elevation_smoothing", _smoothing );

            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( "quick_release_gl_objects", _quickRelease );
            conf.getIfSet( "normalize_edges", _normalizeEdges );
            conf.getIfSet( "tile_pixel_size", _tilePixelSize );

            conf.getIfSet( "range_mode", "PIXEL_SIZE_ON_SCREEN", _rangeMode, osg::LOD::PIXEL_SIZE_ON_SCREEN );
            conf.getIfSet( "range_mode", "DISTANCE_FROM_EYE_POINT", _rangeMode, osg::LOD::DISTANCE_FROM_EYE_POINT);
            conf.getIfSet( "color", _color );
            conf.getIfSet( "incremental_update", _incrementalUpdate );
            conf.getIfSet( "elevation_smoothing", _smoothing );
       }

        optional<float>               _skirtRatio;
        optional<bool>                _quickRelease;
        optional<float>               _lodFallOff;
        optional<bool>                _normalizeEdges;
        optional<osg::LOD::RangeMode> _rangeMode;
        optional<float>               _tilePixelSize;
        optional<Color>               _color;
        optional<bool>                _incrementalUpdate;
        optional<bool>                _smoothing;
    };

} } } // namespace osgEarth::Drivers::MPTerrainEngine

#endif // OSGEARTH_DRIVERS_MP_TERRAIN_ENGINE_OPTIONS
