/* osgEarth - Geospatial SDK for OpenSceneGraph
* Copyright 2018 Pelican Mapping
* MIT License
*/
#ifndef OSGEARTH_DRIVERS_MP_TERRAIN_ENGINE_TILE_GROUP
#define OSGEARTH_DRIVERS_MP_TERRAIN_ENGINE_TILE_GROUP 1

#include "Common"
#include "TileNode"
#include "TileNodeRegistry"
#include <osg/Group>
#include <osgEarth/Threading>
#include <osgEarth/ResourceReleaser>

namespace osgEarth { namespace Drivers { namespace MPTerrainEngine
{
    using namespace osgEarth;

    /**
     * TileGroup is a group node that contains four sibling TileNodes
     * in the quadtree structure. It has the ability to update itself
     * by checking the map revision and scheduling an incremental
     * update of its TileNode set.
     */
    class TileGroup : public osg::Group
    {
    public:
        TileGroup(const TileKey&    key, 
                  const UID&        engineUID,
                  TileNodeRegistry* live,
                  ResourceReleaser* releaser);

        const UID& getEngineUID() const { return _engineUID; }

        const TileKey& getKey() const { return _key; }

    public: // osg::Node
        virtual void traverse(osg::NodeVisitor& nv);

    public: // internal
        void applyUpdate(osg::Node* node);

    protected:
        virtual ~TileGroup() { }

        TileNode* getTileNode(unsigned q);

        osg::ref_ptr<osg::Node>        _updateAgent;
        mutable Threading::Mutex       _updateMutex;
        UID                            _engineUID;
        TileKey                        _key;
        osg::ref_ptr<TileNodeRegistry> _live;
        osg::ref_ptr<ResourceReleaser> _releaser;
    };

} } } // namespace osgEarth::Drivers::MPTerrainEngine

#endif // OSGEARTH_DRIVERS_MP_TERRAIN_ENGINE_TILE_NODE
