/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 * Copyright 2008-2013 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_QUADTREE_OPTIONS
#define OSGEARTH_ENGINE_QUADTREE_OPTIONS 1

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

namespace osgEarth { namespace Drivers
{
    using namespace osgEarth;

    class QuadTreeTerrainEngineOptions : public TerrainOptions // NO EXPORT (header-only)
    {
    public:
        QuadTreeTerrainEngineOptions( const ConfigOptions& options =ConfigOptions() ) : TerrainOptions( options ),
            _skirtRatio  ( 0.05 ),
            _quickRelease( true ),
            _lodFallOff  ( 0.0 ),
            _normalizeEdges( false ),
            _rangeMode( osg::LOD::DISTANCE_FROM_EYE_POINT ),
            _tilePixelSize( 256 )
        {
            setDriver( "quadtree" );
            fromConfig( _conf );
        }

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

    public:
        optional<float>& heightFieldSkirtRatio() { return _skirtRatio; }
        const optional<float>& heightFieldSkirtRatio() const { return _skirtRatio; }

        optional<bool>& quickReleaseGLObjects() { return _quickRelease; }
        const optional<bool>& quickReleaseGLObjects() const { return _quickRelease; }

        optional<float>& lodFallOff() { return _lodFallOff; }
        const optional<float>& lodFallOff() const { return _lodFallOff; }

        optional<bool>& normalizeEdges() { return _normalizeEdges; }
        const optional<bool>& normalizeEdges() const { return _normalizeEdges; }

        optional<osg::LOD::RangeMode>& rangeMode() { return _rangeMode;}
        const optional<osg::LOD::RangeMode>& rangeMode() const { return _rangeMode;}

        optional<float>& tilePixelSize() { return _tilePixelSize; }
        const optional<float>& tilePixelSize() const { return _tilePixelSize; }

    protected:
        virtual Config getConfig() const {
            Config conf = TerrainOptions::getConfig();
            conf.updateIfSet( "skirt_ratio", _skirtRatio );
            conf.updateIfSet( "quick_release_gl_objects", _quickRelease );
            conf.updateIfSet( "lod_fall_off", _lodFallOff );
            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);

            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( "lod_fall_off", _lodFallOff );
            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);
        }

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

} } // namespace osgEarth::Drivers

#endif // OSGEARTH_ENGINE_QUADTREE_OPTIONS
