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

#include "Common"
#include "TileModel"
#include "TileModelCompiler"
#include <osgEarth/Locators>
#include <osg/Group>
#include <vector>

namespace osgEarth_engine_quadtree
{
    using namespace osgEarth;

    //------------------------------------------------------------------------

    /**
     * Node that represents a single quadtree terrain tile corresponding to 
     * unique TileKey.
     */
    class TileNode : public osg::Group
    {
    public:
        /**
         * Constructs a new tile node
         */
        TileNode( const TileKey& key, GeoLocator* keyLocator );

        /**
         * The tilekey associated with this tile
         */
        const TileKey& getKey() const { return _key; }

        /**
         * The data model used to create this node's geometry.
         */
        void setTileModel( TileModel* model );
        TileModel* getTileModel() { return _model.get(); }

        /**
         * Compiles this node's tile model into geometry.
         * @param[in ] compiler     Compiler that will compiler the model
         * @param[in ] releaseModel Whether to deallocate the tile model after compilation
         * @return True upon success
         */
        bool compile( TileModelCompiler* compiler, bool releaseModel =true );

        /**
         * The locator for geometry in this tile
         */
        GeoLocator* getLocator() const { return _locator.get(); }

        /**
         * The public state set associated with the compiled tile model.
         */
        osg::StateSet* getPublicStateSet() const { return _publicStateSet; }


    public: // OVERRIDES

        virtual void traverse( class osg::NodeVisitor& nv );

        //virtual osg::BoundingSphere computeBound() const;

    protected:

        virtual ~TileNode();

        bool                      _cullTraversed;
        TileKey                   _key;
        osg::ref_ptr<GeoLocator>  _locator;
        osg::ref_ptr<TileModel>   _model;
        osg::StateSet*            _publicStateSet;
    };


    typedef std::vector< osg::ref_ptr<TileNode> > TileNodeVector;


    /**
     * Marker class. 
     */
    class TileNodeGroup : public osg::Group
    {
    public:
        TileNodeGroup() : osg::Group() { }
    };

} // namespace osgEarth_engine_quadtree

#endif // OSGEARTH_ENGINE_QUADTREE_TILE_NODE
