/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 * Copyright 2016 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_SIMPLE_MODEL_OPTIONS
#define OSGEARTH_DRIVER_SIMPLE_MODEL_OPTIONS 1

#include <osgEarth/Common>
#include <osgEarth/ModelSource>
#include <osgEarth/URI>
#include <osgEarth/ShaderUtils>

namespace osgEarth { namespace Drivers
{
    using namespace osgEarth;

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

        optional<float>& lodScale() { return _lod_scale; }
        const optional<float>& lodScale() const { return _lod_scale; }

        optional<osg::Vec3>& location() { return _location; }
        const optional<osg::Vec3>& location() const { return _location; }

        optional<osg::Vec3>& orientation() { return _orientation;}
        const optional<osg::Vec3>& orientation() const { return _orientation;}

        optional<ShaderPolicy>& shaderPolicy() { return _shaderPolicy; }
        const optional<ShaderPolicy>& shaderPolicy() const { return _shaderPolicy; }
        
        optional<float>& loadingPriorityScale() { return _loadingPriorityScale; }
        const optional<float>& loadingPriorityScale() const { return _loadingPriorityScale; }

        optional<float>& loadingPriorityOffset() { return _loadingPriorityOffset; }
        const optional<float>& loadingPriorityOffset() const { return _loadingPriorityOffset; }

        optional<bool>& paged() { return _paged; }
        const optional<bool>& paged() const { return _paged; }
        
        /**
         If specified, use this node instead try to load from url
        */
        osg::ref_ptr<osg::Node>& node() { return _node; }
        const osg::ref_ptr<osg::Node>& node() const { return _node; }



    public:
        SimpleModelOptions( const ConfigOptions& options=ConfigOptions() )
            : ModelSourceOptions( options ),
              _lod_scale(1.0f),
              _shaderPolicy( SHADERPOLICY_GENERATE ),
              _loadingPriorityScale(1.0f),
              _loadingPriorityOffset(0.0f),
              _paged(false)
        {
            setDriver( "simple" );
            fromConfig( _conf );
        }

        virtual ~SimpleModelOptions() { }

    public:
        Config getConfig() const {
            Config conf = ModelSourceOptions::getConfig();
            conf.set( "url", _url );
            conf.set( "lod_scale", _lod_scale );
            conf.set( "location", _location );
            conf.set( "orientation", _orientation);
            conf.set( "loading_priority_scale", _loadingPriorityScale );
            conf.set( "loading_priority_offset", _loadingPriorityOffset );
            conf.set( "paged", _paged);

            conf.addIfSet( "shader_policy", "disable",  _shaderPolicy, SHADERPOLICY_DISABLE );
            conf.addIfSet( "shader_policy", "inherit",  _shaderPolicy, SHADERPOLICY_INHERIT );
            conf.addIfSet( "shader_policy", "generate", _shaderPolicy, SHADERPOLICY_GENERATE );

            conf.updateNonSerializable( "SimpleModelOptions::Node", _node.get() );
            return conf;
        }

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

    private:
        void fromConfig( const Config& conf ) {
            conf.getIfSet( "url", _url );
            conf.getIfSet( "lod_scale", _lod_scale );
            conf.getIfSet( "location", _location);
            conf.getIfSet( "orientation", _orientation);
            conf.getIfSet( "loading_priority_scale", _loadingPriorityScale );
            conf.getIfSet( "loading_priority_offset", _loadingPriorityOffset );
            conf.getIfSet( "paged", _paged );

            conf.getIfSet( "shader_policy", "disable",  _shaderPolicy, SHADERPOLICY_DISABLE );
            conf.getIfSet( "shader_policy", "inherit",  _shaderPolicy, SHADERPOLICY_INHERIT );
            conf.getIfSet( "shader_policy", "generate", _shaderPolicy, SHADERPOLICY_GENERATE );

            _node = conf.getNonSerializable<osg::Node>( "SimpleModelOptions::Node" );
        }

        optional<URI> _url;
        optional<float> _lod_scale;
        optional<osg::Vec3> _location;
        optional<osg::Vec3> _orientation;
        optional<ShaderPolicy> _shaderPolicy;
        optional<float> _loadingPriorityScale;
        optional<float> _loadingPriorityOffset;
        optional<bool> _paged;
        osg::ref_ptr<osg::Node> _node;
    };

} } // namespace osgEarth::Drivers

#endif // OSGEARTH_DRIVER_SIMPLE_MODEL_OPTIONS

