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

#include <osgEarth/ImageLayer>
#include <osgEarth/TileRasterizer>
#include <osgEarth/LayerReference>
#include <osgEarth/FeatureSource>
#include <osgEarth/StyleSheet>
#include <osgEarth/Containers>
#include "Export"

namespace osgEarth { namespace Splat
{
    using namespace osgEarth;

    /**
     * Renders a road surface.
     */
    class OSGEARTHSPLAT_EXPORT RoadSurfaceLayer : public osgEarth::ImageLayer
    {
    public: // serialization
        class OSGEARTHSPLAT_EXPORT Options : public ImageLayer::Options {
        public:
            META_LayerOptions(osgEarth, Options, ImageLayer::Options);
            OE_OPTION_LAYER(FeatureSource, featureSource);
            OE_OPTION_VECTOR(ConfigOptions, filters);
            OE_OPTION_LAYER(StyleSheet, styleSheet);
            OE_OPTION(Distance, featureBufferWidth);
            virtual Config getConfig() const;
        private:
            void fromConfig(const Config& conf);
        };

    public:
        META_Layer(osgEarth, RoadSurfaceLayer, Options, osgEarth::ImageLayer, RoadSurface);

        //! Sets the map layer from which to pull feature data
        void setFeatureSource(FeatureSource* layer);
        FeatureSource* getFeatureSource() const;

        //! Buffer around the road vector for querying linear data (should be at least road width/2)
        void setFeatureBufferWidth(const Distance& value);
        const Distance& getFeatureBufferWidth() const;

        //! Style for rendering the road
        void setStyleSheet(StyleSheet* value);
        StyleSheet* getStyleSheet() const;


    public: // ImageLayer

        // Opens the layer and returns a status
        virtual Status openImplementation() override;

        // Closes the layer
        virtual Status closeImplementation() override;

        // Creates an image for a tile key
        virtual GeoImage createImageImplementation(const TileKey& key, ProgressCallback* progress) const override;

        // Called by Map when it adds this layer
        virtual void addedToMap(const class Map*) override;

        // Called by Map when it removes this layer
        virtual void removedFromMap(const class Map*) override;

        osg::Node* getNode() const override;

    protected: // Layer

        // post-ctor initialization
        virtual void init() override;

    protected:

        virtual ~RoadSurfaceLayer() { }

    private:
        osg::ref_ptr<Session> _session;
        mutable Gate<TileKey> _keygate;
        //using FeatureListCache = LRUCache<TileKey, FeatureList>;
        //mutable std::unique_ptr<FeatureListCache> _lru;
        FeatureFilterChain _filterChain;

        void getFeatures(
            FeatureSource* featureSource,
            const TileKey& key,
            FeatureList& output,
            ProgressCallback* progress) const;

        osg::ref_ptr<TileRasterizer> _rasterizer;
    };

} } // namespace osgEarth::Splat

OSGEARTH_SPECIALIZE_CONFIG(osgEarth::Splat::RoadSurfaceLayer::Options);

#endif // OSGEARTH_SPLAT_ROAD_SURFACE_LAYER
