/* osgEarth
 * Copyright 2025 Pelican Mapping
 * MIT License
 */
#pragma once

#include <osgEarth/Common>
#include <osgEarth/Filter>
#include <osgEarth/LineSymbol>
#include <osgEarth/OsgGeometryOperator>
#include <osg/Array>
#include <osg/Geometry>


namespace osgEarth { namespace Util
{
    /**
     * Triangulates a line string into a buffered geometry.
     *
     * The resulting geometry will retain the original line string
     * vertices as a "spine", and will include triangles on either
     * side of the spine that form the buffered polygons. Depending
     * on the Stroke, the geometry may also have triangulated end caps.
     */
    class OSGEARTH_EXPORT PolygonizeLinesOperator : public OsgGeometryOperator
    {
    public:
        /**
         * Construct the operator
         * @param[in ] stroke Line rendering properties
         */
        PolygonizeLinesOperator(const LineSymbol* symbol);

        /**
         * Run the polygonizer.
         *
         * @param[in ] verts    Line string geometry to polygonize. The polygonizer
         *                      will add this array to the resulting geometry.
         * @param[in ] normals  Localized normals associated with the input verts.
         *                      Used to determine the plane in which to polygonize each
         *                      line segment. Optional; can be NULL
         * @param[in ] callback Called for each new point added to the polygonized line
         *                      with the source line index. Optional, call be NULL.
         * @param[in ] twoSided Generate polygons on both sides of the center line.
         *
         * @return Triangulated geometry, including primitive set
         */
        osg::Geometry* operator()(
            osg::Vec3Array* verts, 
            osg::Vec3Array* normals,
            float lineWidth,
            Callback* callback = nullptr,
            bool twoSided = true) const;

        /**
         * Installs an auto-scaling shader on a stateset.
         */
        void installShaders(osg::Node* node) const;

    public:
        //! Whether to generate triangles for both sides of the polygon
        bool _copyToBackFace = false;

    protected:
        osg::ref_ptr<const LineSymbol> _line;
        friend class PolygonizeLinesFilter;
    };



    /**
     * Feature Filter that generates polygonized line geometry.
     */
    class OSGEARTH_EXPORT PolygonizeLinesFilter : public FeaturesToNodeFilter
    {
    public:

        /** Constructs the filter with Style information */
        PolygonizeLinesFilter(const Style& style);


    public: // FeaturesToNodeFilter

        /** Pushes a list of features through the filter. */
        osg::Node* push( FeatureList& input, FilterContext& context );

    protected:

        Style _style;
    };
} }
