/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
* Copyright 2008-2014 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_DRIVERS_REX_TERRAIN_ENGINE_TILE_NODE_REGISTRY
#define OSGEARTH_DRIVERS_REX_TERRAIN_ENGINE_TILE_NODE_REGISTRY 1

#include "Common"
#include "TileNode"
#include <osgEarth/Revisioning>
#include <osgEarth/ThreadingUtils>
//#include <osgEarth/TerrainEngineNode>
#include <osgEarth/ResourceReleaser>
#include <OpenThreads/Atomic>
#include <osgUtil/RenderBin>
#include <map>

namespace osgEarth { namespace Drivers { namespace RexTerrainEngine
{
    using namespace osgEarth;

    struct RandomAccessTileMap
    {
        struct Entry {
            osg::ref_ptr<TileNode> tile;
            unsigned index;
        };

        typedef std::map<TileKey, Entry> Table;
        Table _table;

        typedef Table::iterator iterator;
        typedef Table::const_iterator const_iterator;

        typedef std::vector<Entry*> Vector;
        Vector _vector;

        iterator begin()             { return _table.begin(); }
        const_iterator begin() const { return _table.begin(); }
        iterator end()               { return _table.end(); }
        const_iterator end() const   { return _table.end(); }

        void insert(const TileKey& key, TileNode* data) {
            Entry& e = _table[key];
            e.tile = data;
            e.index = _vector.size();
            _vector.push_back( &e );
        }

        void erase(const TileKey& key) {
            iterator i = _table.find(key);
            if ( i != _table.end() ) {
                unsigned s = _vector.size()-1;
                _vector[i->second.index] = _vector[s];
                _vector[i->second.index]->index = i->second.index;
                _vector.resize( s );
                _table.erase( i );
            }
        }

        const TileNode* find(const TileKey& key) const {
            const_iterator i = _table.find(key);
            return i != _table.end() ? i->second.tile.get() : 0L;
        }

        TileNode* find(const TileKey& key) {
            const_iterator i = _table.find(key);
            return i != _table.end() ? i->second.tile.get() : 0L;
        }

        unsigned size() const {
            return _vector.size();
        }

        bool empty() const {
            return size() == 0u;
        }

        TileNode* at(unsigned index) {
            return _vector[index]->tile.get();
        }

        const TileNode* at(unsigned index) const {
            return _vector[index]->tile.get();
        }

        void clear() {
            _table.clear();
            _vector.clear();
        }
    };

    /**
     * Holds a reference to each tile created by the driver.
     */
    class TileNodeRegistry : public osg::Referenced
    {
    public:
        typedef RandomAccessTileMap TileNodeMap;

        // Prototype for a locked tileset operation (see run)
        struct Operation {
            virtual void operator()(TileNodeMap& tiles) =0;
        };
        struct ConstOperation {
            virtual void operator()(const TileNodeMap& tiles) const =0;
        };

        // Operation that runs when another node enters the registry.
        struct DeferredOperation {
            virtual void operator()(TileNode* requestingNode, TileNode* expectedNode) const =0;
        };

    public:
        TileNodeRegistry( const std::string& name );

        /* Enabled revisioning on TileNodes, to support incremental update. */
        void setRevisioningEnabled(bool value);

        /**
         * Sets the revision of the map model - the registry will assign this
         * to TileNodes added with add().
         *
         * @param rev        Revision of map
         * @param setToDirty In addition to update the revision, immediately set
         *                   all tiles to dirty as well, effectively forcing an
         *                   update.
         */
        void setMapRevision( const Revision& rev, bool setToDirty =false );

        /** Map revision that the reg will assign to new tiles. */
        const Revision& getMapRevision() const { return _maprev; }

        /**
         * Marks all tiles intersecting the extent as dirty. If incremental
         * update is enabled, they will automatically reload.
         *
         * NOTE: Input extent SRS must match the terrain's SRS exactly.
         *       The method does not check.
         */
        void setDirty(const GeoExtent& extent, unsigned minLevel, unsigned maxLevel);

        /**
         * Sets the current cull traversal frame number so that tiles have
         * access to the information. Atomic.
         */
        void setTraversalFrame(unsigned frame) { _frameNumber.exchange(frame); }

        unsigned getTraversalFrame() const { return _frameNumber; }

        virtual ~TileNodeRegistry() { }

        /** Adds a tile to the registry */
        void add( TileNode* tile );

        /** Adds several tiles to the registry */
        void add( const TileNodeVector& tiles );

        /** Removes a tile */
        void remove( TileNode* tile );

        /** Clears all tiles from the registry */
        //void clear();

        /** Moves a tile from this registry to another registry */
        //void move( TileNode* tile, TileNodeRegistry* destination );

        /** Moves all tiles to another registry. */
        //void moveAll(TileNodeRegistry* destinataion);

        /** Finds a tile in the registry */
        bool get( const TileKey& key, osg::ref_ptr<TileNode>& out_tile );

        /** Finds a tile in the registry and then removes it. */
        bool take( const TileKey& key, osg::ref_ptr<TileNode>& out_tile );

        /** Whether there are tiles in this registry (snapshot in time) */
        bool empty() const;

        /** Runs an operation against the exclusively locked tile set. */
        void run( Operation& op );
        
        /** Runs an operation against the read-locked tile set. */
        void run( const ConstOperation& op ) const;

        /** Number of tiles in the registry. */
        unsigned size() const { return _tiles.size(); }

        /** Tells the registry to listen for the TileNode for the specific key
            to arrive, and upon its arrival, notifies the waiter. After notifying
            the waiter, it removes the listen request. */
        //void listenFor(const TileKey& keyToWaitFor, TileNode* waiter);

        /** Take an arbitrary node from the registry. */
        TileNode* takeAny();

        /** Empty the registry, releasing all tiles. */
        void releaseAll(ResourceReleaser*);

    protected:

        bool                              _revisioningEnabled;
        Revision                          _maprev;
        std::string                       _name;
        TileNodeMap                       _tiles;
        OpenThreads::Atomic               _frameNumber;
        mutable Threading::ReadWriteMutex _tilesMutex;

        //typedef std::vector<TileKey> TileKeyVector;
        typedef fast_set<TileKey> TileKeySet;
        typedef std::map<TileKey, TileKeySet> TileKeyOneToMany;

        TileKeyOneToMany _notifiers;

    private:

        /** adds a tile node, assuming the write-lock has been taken by the caller and
            that node is not NULL */
        void addSafely(TileNode* node);
        void removeSafely(const TileKey& key);

        /** Tells the registry to listen for the TileNode for the specific key
            to arrive, and upon its arrival, notifies the waiter. After notifying
            the waiter, it removes the listen request. (assumes lock held) */
        void startListeningFor(const TileKey& keyToWaitFor, TileNode* waiter);

        /** Removes a listen request set by startListeningFor (assumes lock held) */
        void stopListeningFor(const TileKey& keyToWairFor, TileNode* waiter);
    };

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

#endif // OSGEARTH_DRIVERS_REX_TERRAIN_ENGINE_TILE_NODE_REGISTRY
