/* -*-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_MP_OPTIONS
#define OSGEARTH_ENGINE_MP_OPTIONS 1

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

namespace osgEarth { namespace Drivers
{
    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 ),
            _lodFallOff    ( 0.0 ),
            _normalizeEdges( true ),
            _rangeMode     ( osg::LOD::DISTANCE_FROM_EYE_POINT ),
            _tilePixelSize ( 256 ),
            _premultAlpha  ( true ),
            _color         ( Color::White )
        {
            setDriver( "mp" );
            fromConfig( _conf );
        }

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

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

        optional<bool>& premultipliedAlpha() { return _premultAlpha; }
        const optional<bool>& premultipliedAlpha() const { return _premultAlpha; }

        optional<Color>& color() { return _color; }
        const optional<Color>& color() const { return _color; }

    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);
            conf.updateIfSet( "premultiplied_alpha", _premultAlpha );
            conf.updateIfSet( "color", _color );

            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);
            conf.getIfSet( "premultiplied_alpha", _premultAlpha );
            conf.getIfSet( "color", _color );
        }

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

} } // namespace osgEarth::Drivers

#endif // OSGEARTH_ENGINE_MP_OPTIONS
