/* -*-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_MODEL_LAYER_H
#define OSGEARTH_MODEL_LAYER_H 1

#include <osgEarth/Common>
#include <osgEarth/Layer>
#include <osgEarth/Config>
#include <osgEarth/ModelSource>
#include <osg/Node>
#include <vector>

namespace osgEarth
{
    class Map;

    /**
     * Configuration options for a ModelLayer.
     */
    class OSGEARTH_EXPORT ModelLayerOptions : public ConfigOptions
    {
    public:        
        ModelLayerOptions( const ConfigOptions& options =ConfigOptions() );

        ModelLayerOptions( const std::string& name, const ModelSourceOptions& driverOptions =ModelSourceOptions() );

        /**
         * The readable name of the layer.
         */
        optional<std::string>& name() { return _name; }
        const optional<std::string>& name() const { return _name; }

        /**
         * Options for the underlying model source driver.
         */
        optional<ModelSourceOptions>& driver() { return _driver; }
        const optional<ModelSourceOptions>& driver() const { return _driver; }

        /**
         * Whether to enable OpenGL lighting on the model node.
         */
        optional<bool>& lightingEnabled() { return _lighting; }
        const optional<bool>& lightingEnabled() const { return _lighting; }

        /**
         * Whether this layer will be drawn.
         */
        optional<bool>& enabled() { return _enabled; }
        const optional<bool>& enabled() const { return _enabled; }

        /**
         * Whether to drape the model geometry over the terrain as a projected overlay.
         * Defaults to false
         */
        optional<bool>& overlay() { return _overlay; }
        const optional<bool>& overlay() const { return _overlay; }

    public:
        virtual Config getConfig() const;
        virtual void mergeConfig( const Config& conf );

    private:
        void fromConfig( const Config& conf );
        void setDefaults();

        optional<std::string> _name;
        optional<bool> _overlay;
        optional<ModelSourceOptions> _driver;
        optional<bool> _enabled;
        optional<bool> _lighting;
    };

    /**
    * Callback for receiving notification of property changes on a ModelLayer.
    */
    struct ModelLayerCallback : public osg::Referenced
    {
        virtual void onOverlayChanged( class ModelLayer* layer ) { }
    };

    typedef void (ModelLayerCallback::*ModelLayerCallbackMethodPtr)(ModelLayer* layer);

    typedef std::list< osg::ref_ptr<ModelLayerCallback> > ModelLayerCallbackList;


    class OSGEARTH_EXPORT ModelLayer : public Layer
    {
    public:
        /**
         * Constructs a new model layer.
         */
        ModelLayer( const ModelLayerOptions& options );

        /**
         * Constructs a new model layer with a user-provided driver options.
         */
        ModelLayer( const std::string& name, const ModelSourceOptions& options );
        
        /**
         * Constructs a new model layer with a user-provided model source.
         */
        ModelLayer(const ModelLayerOptions& options, ModelSource* source );

        /**
         * Constructs a new model layer with a user provided name and an existing node
         */
        ModelLayer(const std::string& name, osg::Node* node);

    public:
        /** 
         * Gets the name of this model layer
         */
        const std::string& getName() const { return _options.name().value(); }

        /**
         * Gets the initialization options for this layer.
         */
        const ModelLayerOptions& getModelLayerOptions() const { return _options; }

        /**
         * Gets the reference URI (for resolving relative paths)
         */
        const std::string& getReferenceURI() const { return _referenceURI; }

        /**
         * Access the underlying model source.
         */
        ModelSource* getModelSource() const { return _modelSource.get(); }

    public:

        osg::Node* getOrCreateNode( ProgressCallback* progress =0L );

    public: // properties

        /** Whether this layer is rendered. */
        bool getEnabled() const;
        void setEnabled(bool enabled);

        /** Whether this layer is drawn as normal geometry or as a draped overlay. */
        bool getOverlay() const;
        void setOverlay( bool overlay );

        // whether to apply lighting to the model layer's root node
        void setLightingEnabled( bool value );
        const optional<bool>& lightingEnabled() const { return _enabled; }

    public:

        /** Adds a property notification callback to this layer */
        void addCallback( ModelLayerCallback* cb );

        /** Removes a property notification callback from this layer */
        void removeCallback( ModelLayerCallback* cb );

    public:

        // internal function
        void initialize( const std::string& referenceURI, const Map* map );

    private:
        std::string _referenceURI;
        osg::ref_ptr<ModelSource> _modelSource;
        const ModelLayerOptions _options;

        osg::ref_ptr< osg::Node > _node;

        optional<bool> _enabled;
        optional<bool> _lighting;
        optional<bool>  _overlay;

        Revision _modelSourceRev;

        ModelLayerCallbackList _callbacks;
        virtual void fireCallback( ModelLayerCallbackMethodPtr method );

    };

    typedef std::vector< osg::ref_ptr<ModelLayer> > ModelLayerVector;
}

#endif // OSGEARTH_MODEL_LAYER_H
