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

#include <osgEarth/Common>
#include <osgEarth/TileSource>
#include <osgEarth/HeightFieldUtils>

namespace osgEarth { namespace Drivers
{
    using namespace osgEarth;

    class GDALOptions : public TileSourceOptions // NO EXPORT; header only
    {
    public: // properties

        optional<std::string>& url() { return _url; }
        const optional<std::string>& url() const { return _url; }

        optional<std::string>& extensions() { return _extensions; }
        const optional<std::string>& extensions() const { return _extensions; }
        
        optional<ElevationInterpolation>& interpolation() { return _interpolation; }
        const optional<ElevationInterpolation>& interpolation() const { return _interpolation; }

        optional<unsigned int>& maxDataLevel() { return _maxDataLevel;}
        const optional<unsigned int>& maxDataLevel() const { return _maxDataLevel;}

    public: // ctors

        GDALOptions( const TileSourceOptions& options =TileSourceOptions() ) :
            TileSourceOptions( options ),
            _interpolation( INTERP_AVERAGE )
        {
            setDriver( "gdal" );
            fromConfig( _conf );
        }

    protected:

        Config getConfig() const
        {
            Config conf = TileSourceOptions::getConfig();
            conf.updateIfSet( "url", _url );
            conf.updateIfSet( "extensions", _extensions );

            if ( _interpolation.isSet() ) {
                if ( _interpolation.value() == osgEarth::INTERP_NEAREST ) conf.update( "interpolation", "nearest" );
                else if ( _interpolation.value() == osgEarth::INTERP_AVERAGE ) conf.update( "interpolation", "average" );
                else if ( _interpolation.value() == osgEarth::INTERP_BILINEAR ) conf.update( "interpolation", "bilinear" );
            }

            conf.updateIfSet( "max_data_level", _maxDataLevel);
            return conf;
        }

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

        void fromConfig( const Config& conf ) {
            conf.getIfSet( "url", _url );
            conf.getIfSet( "extensions", _extensions );
            std::string in = conf.value( "interpolation" );
            if ( in == "nearest" ) _interpolation = osgEarth::INTERP_NEAREST;
            else if ( in == "average" ) _interpolation = osgEarth::INTERP_AVERAGE;
            else if ( in == "bilinear" ) _interpolation = osgEarth::INTERP_BILINEAR;
            conf.getIfSet( "max_data_level", _maxDataLevel);
        }

        optional<std::string> _url;
        optional<std::string> _extensions;
        optional<ElevationInterpolation> _interpolation;
        optional<unsigned int> _maxDataLevel;
    };

} } // namespace osgEarth::Drivers

#endif // OSGEARTH_DRIVER_GDAL_DRIVEROPTIONS

