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

#include <osgEarth/AnnotationNode>
#include <osgEarth/GeoTransform>
#include <osgEarth/CullingUtils>
#include <osg/PositionAttitudeTransform>

namespace osgEarth
{
    /**
     * Base class for an annotation node that is anchored to a GeoPoint on the map.
     * Use getGeoTransform() to control the map position, and getPositionAttitudeTransform()
     * to control the offsets in the local tangent plane at that location.
     */
    class OSGEARTH_EXPORT GeoPositionNode : public AnnotationNode
    {
    public:
        META_AnnotationNode(osgEarth, GeoPositionNode );

        /**
         * Constructs an positional node
         */
        GeoPositionNode();

        /**
         * The anchor position of this node.
         * If the annotation also has a style that contains an AltitudeSymbol,
         * any clamping properties in the symbol will take precedence over
         * the altitude mode in the GeoPoint you pass into this call.
         * @param pos New geoposition
         */
        virtual void setPosition(const GeoPoint& pos);
        const GeoPoint& getPosition() const { return _geoxform->getPosition(); }

        /** Local XYZ offset */
        virtual void setLocalOffset(const osg::Vec3d& pos) { _paxform->setPosition(pos); dirty(); }
        const osg::Vec3d& getLocalOffset() const           { return _paxform->getPosition(); }

        /** Local rotation */
        virtual void setLocalRotation(const osg::Quat& rot) { _paxform->setAttitude(rot); dirty(); }
        const osg::Quat& getLocalRotation() const           { return _paxform->getAttitude(); }

        /** Scale */
        virtual void setScale(const osg::Vec3f& scale) { _paxform->setScale(scale); dirty(); }
        const osg::Vec3d& getScale() const             { return _paxform->getScale(); }

        /**
         * Access to the GeoTransform that positions this node.
         */
        GeoTransform* getGeoTransform() const { return _geoxform; }

        /**
         * Access to the PositionAttitudeTransform for local offsets and rotation
         */
        osg::PositionAttitudeTransform* getPositionAttitudeTransform() const { return _paxform; }

        /**
         * Enables or disables ray based occlusion culling
         */
        bool getOcclusionCulling() const;
        void setOcclusionCulling( bool value );

        /**
         * Gets or sets the maximum altitude that the occlusion culling takes place.
         */
        double getOcclusionCullingMaxAltitude() const;
        void setOcclusionCullingMaxAltitude( double occlusionCullingMaxAltitude );

        /** Serialize. */
        Config getConfig() const;


    public: // AnnotationNode

        virtual void applyStyle(const Style& style);

    public: // MapNodeObserver

        virtual void setMapNode( MapNode* mapNode );

    protected:
        /** virtual dtor */
        virtual ~GeoPositionNode() { }

        /** called when someone calls one of the set functions */
        virtual void dirty() { }

        virtual void setConfig(const Config&);

        //virtual void init(const osgDB::Options*);

    private:
        GeoTransform*                   _geoxform;
        osg::PositionAttitudeTransform* _paxform;
        bool                            _occlusionCullingRequested;
        optional< double >              _occlusionCullingMaxAltitude;
        bool                            _horizonCullingRequested;

        osg::ref_ptr< HorizonCullCallback >      _horizonCuller;
        osg::ref_ptr< OcclusionCullingCallback > _occlusionCuller;

        void construct();

    protected:
        GeoPositionNode(const Config& conf, const osgDB::Options*);

        // required by META_Node, but this object is not cloneable
        GeoPositionNode(const GeoPositionNode& rhs, const osg::CopyOp& op =osg::CopyOp::DEEP_COPY_ALL) 
         : _geoxform(rhs._geoxform)
         , _paxform(rhs._paxform)
         , _occlusionCullingRequested(rhs._occlusionCullingRequested)
         , _horizonCullingRequested(rhs._horizonCullingRequested)
        { }
    };

}

#endif // OSGEARTH_ANNO_LOCALIZED_NODE_H
