/* osgEarth
* Copyright 2025 Pelican Mapping
* MIT License
*/
#ifndef OSGEARTH_ANNO_LOCAL_GEOMETRY_NODE_H
#define OSGEARTH_ANNO_LOCAL_GEOMETRY_NODE_H 1

#include <osgEarth/GeoPositionNode>
#include <osgEarth/MapNode>
#include <osgEarth/GeometryClamper>
#include <osgEarth/Geometry>
#include <osgEarth/Style>

namespace osgEarth
{
    /**
     * Simple node that renders geometry into a scene graph node. The geometry
     * is in a local tangent plane that may be positioned somewhere on the 
     * Map's surface.
     *
     * As a GeoPositionNode, the LGN's main location is controlled by the 
     * setPosition method. Terrain following is controlled by using a GeoPoint
     * with an altitude value relative to the terrain.
     */
    class OSGEARTH_EXPORT LocalGeometryNode : public GeoPositionNode
    {
    public:
        //! Construct a new empty LocalGeometryNode
        LocalGeometryNode();
        
        //! Construct a new empty LocalGeometryNode and assign it a geoemtry and style
        LocalGeometryNode( 
            Geometry*    geom, 
            const Style& style );

        /**
         * Gets the Style used to create thenode.
         */
        const Style& getStyle() const { return _style; }

        /**
         * Sets a new style for the node
         */
        void setStyle(const Style& style);

        /**
         * Gets the geometry used to create this node (if applicable)
         */
        const Geometry* getGeometry() const { return _geom.get(); }

        /**
         * Sets new geometry for this node.
         */
        void setGeometry( Geometry* geom );


    public: // GeoPositionNode

        virtual void setPosition(const GeoPoint&);

    public: // AnnotationNode

        virtual void setMapNode(MapNode*);

    public:

        /**
         * Constructs an LGN from a serialized Config.
         */
        LocalGeometryNode(
            const Config&         conf,
            const osgDB::Options* dbOptions);

        virtual Config getConfig() const;

    public: // osg::Node

        virtual void traverse(osg::NodeVisitor&);

    protected:

        virtual ~LocalGeometryNode() { }

        Style                        _style;
        osg::ref_ptr<osg::Node>      _node;
        osg::ref_ptr<Geometry>       _geom;
        bool                         _clampInUpdateTraversal;
        bool                         _perVertexClampingEnabled;
        
        typedef TerrainCallbackAdapter<LocalGeometryNode> ClampCallback;
        osg::ref_ptr<ClampCallback> _clampCallback;
        GeometryClamper::LocalData _clamperData;

        void compileGeometry();
        void togglePerVertexClamping();
        void reclamp();

    public:
        void onTileUpdate(
            const TileKey&          key, 
            osg::Node*              graph, 
            TerrainCallbackContext& context);

        virtual void clamp(
            osg::Node*     graph,
            const Terrain* terrain);

    private:

        void construct();

        GeoPoint _lastPosition;
    };

}

#endif // OSGEARTH_ANNO_LOCAL_GEOMETRY_NODE_H
