/* osgEarth
* Copyright 2025 Pelican Mapping
* MIT License
*/

#ifndef OSGEARTH_ANNOTATION_LABEL_NODE_H
#define OSGEARTH_ANNOTATION_LABEL_NODE_H 1

#include <osgEarth/GeoPositionNode>
#include <osgEarth/Style>
#include <osgEarth/MapNode>
#include <osgEarth/ScreenSpaceLayout>
#include <osg/Geode>

namespace osgEarth
{
    /**
     * Text labeling node.
     */
    class OSGEARTH_EXPORT LabelNode : public GeoPositionNode
    {
    public:
        META_AnnotationNode(osgEarth, LabelNode );

        //! Construct a new empty label node.
        LabelNode();

        //! Construct new node with text content and default styling.
        LabelNode(const std::string& text,
                  const Style&       style =Style());

        LabelNode(const GeoPoint&    position,
                  const std::string& text  ="",
                  const Style&       style =Style());

        //! Deserialize a label node
        LabelNode(
            const Config& conf,
            const osgDB::Options* dbOptions );

        //! Sets the text content.
        void setText( const std::string& text );
        const std::string& text() const { return _text; }
        const std::string& getText() const override { return text(); }

        //! Gets a reference to the text style.
        const Style& getStyle() const { return _style; }

        //! Sets a new text style
        void setStyle( const Style& style );

    public: // GeoPositionNode override

        void setPriority(float value) override;

        void setDynamic(bool value) override;

        Config getConfig() const override;

        void dirty() override;

    protected: // AnnotationNode override
        
        virtual bool supportsRenderBinDetails() const { return false; }

    protected:

        virtual ~LabelNode() { }

        std::string              _text;
        Style                    _style;
        osg::ref_ptr<osg::Geode> _geode;
        osg::ref_ptr<ScreenSpaceLayoutData> _dataLayout;

        /** rotation of the label **/
        float                    _labelRotationRad;
        bool                     _followFixedCourse;
        GeoPoint                 _geoPointLoc;
        GeoPoint                 _geoPointProj;

        /** Copy constructor */
        LabelNode(const LabelNode& rhs, const osg::CopyOp& op = osg::CopyOp::DEEP_COPY_ALL) 
         : _labelRotationRad(rhs._labelRotationRad)
         , _followFixedCourse(rhs._followFixedCourse)
        { }

        void updateLayoutData();

    private:
        static osg::observer_ptr<osg::StateSet> s_geodeStateSet;
        void construct();
        void compile();

    };

}

#endif // OSGEARTH_ANNOTATION_LABEL_NODE_H
