/* -*-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_DRIVER_OGR_FEATURE_SOURCE_OPTIONS
#define OSGEARTH_DRIVER_OGR_FEATURE_SOURCE_OPTIONS 1

#include <osgEarth/Common>
#include <osgEarthFeatures/FeatureSource>

namespace osgEarth { namespace Drivers
{
    using namespace osgEarth;
    using namespace osgEarth::Features;

    class OGRFeatureOptions : public FeatureSourceOptions // NO EXPORT; header only
    {
    public:
        optional<URI>& url() { return _url; }
        const optional<URI>& url() const { return _url; }

        optional<std::string>& connection() { return _connection; }
        const optional<std::string>& connection() const { return _connection; }

        optional<std::string>& ogrDriver() { return _ogrDriver; }
        const optional<std::string>& ogrDriver() const { return _ogrDriver; }

        optional<bool>& buildSpatialIndex() { return _buildSpatialIndex; }
        const optional<bool>& buildSpatialIndex() const { return _buildSpatialIndex; }

        optional<bool>& forceRebuildSpatialIndex() { return _forceRebuildSpatialIndex; }
        const optional<bool>& forceRebuildSpatialIndex() const { return _forceRebuildSpatialIndex; }

        optional<Config>& geometryConfig() { return _geometryConf; }
        const optional<Config>& geometryConfig() const { return _geometryConf; }

        optional<std::string>& geometryUrl() { return _geometryUrl; }
        const optional<std::string>& geometryUrl() const { return _geometryUrl; }

        optional<std::string>& layer() { return _layer; }
        const optional<std::string>& layer() const { return _layer; }

        // does not serialize
        osg::ref_ptr<Symbology::Geometry>& geometry() { return _geometry; }
        const osg::ref_ptr<Symbology::Geometry>& geometry() const { return _geometry; }

    public:
        OGRFeatureOptions( const ConfigOptions& opt =ConfigOptions() ) : FeatureSourceOptions( opt ) {
            setDriver( "ogr" );
            fromConfig( _conf );
        }

        virtual ~OGRFeatureOptions() { }

    public:
        Config getConfig() const {
            Config conf = FeatureSourceOptions::getConfig();
            conf.updateIfSet( "url", _url );
            conf.updateIfSet( "connection", _connection );
            conf.updateIfSet( "ogr_driver", _ogrDriver );
            conf.updateIfSet( "build_spatial_index", _buildSpatialIndex );
            conf.updateIfSet( "force_rebuild_spatial_index", _forceRebuildSpatialIndex );
            conf.updateIfSet( "geometry", _geometryConf );    
            conf.updateIfSet( "geometry_url", _geometryUrl );
            conf.updateIfSet( "layer", _layer );
            conf.updateNonSerializable( "OGRFeatureOptions::geometry", _geometry.get() );
            return conf;
        }

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

    private:
        void fromConfig( const Config& conf ) {
            conf.getIfSet( "url", _url );
            conf.getIfSet( "connection", _connection );
            conf.getIfSet( "ogr_driver", _ogrDriver );
            conf.getIfSet( "build_spatial_index", _buildSpatialIndex );
            conf.getIfSet( "force_rebuild_spatial_index", _forceRebuildSpatialIndex );
            conf.getIfSet( "geometry", _geometryConf );
            conf.getIfSet( "geometry_url", _geometryUrl );
            conf.getIfSet( "layer", _layer);
            _geometry = conf.getNonSerializable<Symbology::Geometry>( "OGRFeatureOptions::geometry" );
        }

        optional<URI>                     _url;
        optional<std::string>             _connection;
        optional<std::string>             _ogrDriver;
        optional<bool>                    _buildSpatialIndex;
        optional<bool>                    _forceRebuildSpatialIndex;
        optional<Config>                  _geometryConf;
        optional<Config>                  _geometryProfileConf;
        optional<std::string>             _geometryUrl;
        optional<std::string>             _layer;
        osg::ref_ptr<Symbology::Geometry> _geometry;
    };

} } // namespace osgEarth::Drivers

#endif // OSGEARTH_DRIVER_OGR_FEATURE_SOURCE_OPTIONS