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

#include <osgEarth/Common>
#include <osgEarth/AlphaEffect>
#include <osgEarth/Layer>
#include <osgEarth/CachePolicy>
#include <osgEarth/Config>
#include <osgEarth/ModelSource>
#include <osgEarth/MaskSource>
#include <osgEarth/ShaderUtils>
#include <osgEarth/Containers>
#include <osg/Node>
#include <osg/Array>
#include <vector>

namespace osgEarth
{
    class Map;

    /**
     * Configuration options for a ModelLayer.
     */
    class OSGEARTH_EXPORT ModelLayerOptions : public ConfigOptions
    {
    public:        
        /** Construct or deserialize new model layer options. */
        ModelLayerOptions(
            const ConfigOptions& options =ConfigOptions());

        /** Construct or deserialize new model layer options. */
        ModelLayerOptions(
            const std::string&        name, 
            const ModelSourceOptions& driverOptions =ModelSourceOptions() );

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

        /**
         * 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 is active
         */
        optional<bool>& enabled() { return _enabled; }
        const optional<bool>& enabled() const { return _enabled; }

        /**
         * Whether this layer is visible
         */
        optional<bool>& visible() { return _visible; }
        const optional<bool>& visible() const { return _visible; }

        /**
         * The opacity of this layer
         */
        optional<float>& opacity() { return _opacity; }
        const optional<float>& opacity() const { return _opacity; }

        /**
         * Masking options for cutting a hole in the terrain to accommodate this model.
         * Note; the mask will NOT honor any visibility or opacity settings on the
         * model layer.
         */
        optional<MaskSourceOptions>& maskOptions() { return _maskOptions; }
        const optional<MaskSourceOptions>& maskOptions() const { return _maskOptions; }

        /**
         * Minimum terrain LOD at which to apply the mask (if there if one)
         */
        optional<unsigned>& maskMinLevel() { return _maskMinLevel; }
        const optional<unsigned>& maskMinLevel() const { return _maskMinLevel; }

        /**
         * Whether this layer should be treated as part of the terrain
         * for the purposes of elevation queries, clamping, etc.
         */
        optional<bool>& terrainPatch() { return _terrainPatch; }
        const optional<bool>& terrainPatch() const { return _terrainPatch; }

        /**
         * Caching policy for the layer
         */
        optional<CachePolicy>& cachePolicy() { return _cachePolicy; }
        const optional<CachePolicy>& cachePolicy() const { return _cachePolicy; }


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

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

        optional<std::string>        _name;
        optional<ModelSourceOptions> _driver;
        optional<bool>               _enabled;
        optional<bool>               _visible;
        optional<float>              _opacity;
        optional<bool>               _lighting;
        optional<MaskSourceOptions>  _maskOptions;
        optional<unsigned>           _maskMinLevel;
        optional<bool>               _terrainPatch;
        optional<CachePolicy>        _cachePolicy;
    };

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

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

        /** dtor */
        virtual ~ModelLayer();

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

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

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

        /**
         * The underlying mask source, if one exists.
         */
        MaskSource* getMaskSource() const { return _maskSource.get(); }

        /**
         * The minimum terrain LOD at which to apply the mask.
         */
        unsigned getMaskMinLevel() const { return _initOptions.maskMinLevel().get(); }
        
        /**
         * The boundary geometry for the mask.
         */
        osg::Vec3dArray* getOrCreateMaskBoundary(
            float                   heightScale,
            const SpatialReference* srs,
            ProgressCallback*       progress);

        /**
         * Whether this model layer is a terrain patch for the purposes of 
         * intersection testing. (convenience function)
         */
        bool isTerrainPatch() const { return _initOptions.terrainPatch().get(); }


    public:

        /**
         * Perform one-time initialize of the model layer.
         */
        void initialize( const osgDB::Options* options );

        /**
         * Creates the scene graph representing this model layer for the given Map,
         * or returns one that already exists.
         */
        osg::Node* getOrCreateSceneGraph(const Map* map, ProgressCallback* progress);

        /**
         * Gets a scene graph what was previously created with getOrCreateSceneGraph.
         */
        osg::Node* getSceneGraph(const UID& mapUID) const;
        
        /**
         * This layer's caching policy
         */
        void setCachePolicy(const CachePolicy& cachePolicy);
        const CachePolicy& getCachePolicy() const;

    public: // deprecated
        
        /** @deprecated */
        osg::Node* getOrCreateSceneGraph( 
            const Map*            map, 
            const osgDB::Options* dbOptions,
            ProgressCallback*     progress ) { return getOrCreateSceneGraph(map, progress); }

    public: // properties

        /** Whether this layer is rendered. */
        bool getVisible() const;
        void setVisible(bool value);

        /** Whether this layer is used at all. */
        bool getEnabled() const;

        /** whether to apply lighting to the model layer's root node */
        void setLightingEnabled( bool value );
        bool isLightingEnabled() const;

        /**
         * Sets the opacity of this image layer.
         * @param opacity Opacity [0..1] -> [transparent..opaque]
         */
        void setOpacity( float opacity );
        float getOpacity() const;

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

    private:
        osg::ref_ptr<ModelSource>     _modelSource;
        osg::ref_ptr<MaskSource>      _maskSource;
        const ModelLayerOptions       _initOptions;
        ModelLayerOptions             _runtimeOptions;
        Revision                      _modelSourceRev;
        ModelLayerCallbackList        _callbacks;
        osg::ref_ptr<AlphaEffect>     _alphaEffect;
        osg::ref_ptr<osg::Vec3dArray> _maskBoundary;
        osg::ref_ptr<osgDB::Options>  _dbOptions;

        typedef fast_map<UID, osg::observer_ptr<osg::Node> > Graphs;
        Graphs _graphs;

        mutable Threading::Mutex _mutex; // general-purpose mutex.

        virtual void fireCallback( ModelLayerCallbackMethodPtr method );

        void copyOptions();

        void setLightingEnabledNoLock(bool value);
        
        void initializeCachePolicy(const osgDB::Options* options);
    };

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

#endif // OSGEARTH_MODEL_LAYER_H
