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

#include <osgEarth/Common>
#include <osgEarth/ImageLayer>
#include <osgEarth/LayerReference>
#include <osgEarth/URI>
#include <osgEarth/TerrainResources>
#include <osgEarth/Color>

namespace osgEarth
{
    /**
     * A Rex map layer that renders a simple ocean surface.
     * This layer requires that the map include bathymetric data (ocean floor).
     */
    class OSGEARTH_EXPORT SimpleOceanLayer : public VisibleLayer
    {
    public: // serialization
        class OSGEARTH_EXPORT Options : public VisibleLayer::Options {
        public:
            META_LayerOptions(osgEarth, Options, VisibleLayer::Options);
            OE_OPTION(Color, color);
            OE_OPTION(float, maxAltitude);
            OE_OPTION_LAYER(ImageLayer, maskLayer);
            OE_OPTION(bool, useBathymetry);
            OE_OPTION(URI, textureURI);
            OE_OPTION(unsigned, textureLOD);
            OE_OPTION(float, seaLevel);
            OE_OPTION_REFPTR(osg::Image, surfaceImage);
            virtual Config getConfig() const;
        private:
            void fromConfig(const Config& conf);
        };

    public:
        META_Layer(osgEarth, SimpleOceanLayer, Options, VisibleLayer, ocean);

    public:

        //! Ocean surface color (including transparency in the alpha channel)
        void setColor(const Color& color);
        const Color& getColor() const;

        //! Maximum altitude at which the ocean layer is visible
        void setMaxAltitude(const float& altitude_m);
        const float& getMaxAltitude() const;

        //! Sets a masking layer, or pass NULL to clear the masking layer
        void setMaskLayer(ImageLayer* layer);
        ImageLayer* getMaskLayer() const;

        //! Sea level (meters above the ellipsoid)
        void setSeaLevel(const float& seaLevel);
        const float& getSeaLevel() const;

        //! Whether to sample the terrain bathymetry and only draw the ocean
        //! where the elevation is negative (default = true)
        void setUseBathymetry(const bool& value);
        const bool& getUseBathymetry() const;

        //! Surface texture to apply. Call this before adding to the map or 
        //! opening the layer.
        void setSurfaceImage(osg::Image* value);

        //! LOD at which to apply surface texture 1:1 (default = 13)
        void setSurfaceTextureLOD(const unsigned& value);
        const unsigned& getSurfaceTextureLOD() const;

    public: // Layer

        /** callback that ensures proper culling */
        void modifyTileBoundingBox(const TileKey& key, osg::BoundingBox& box) const override;

    protected: // Layer

        virtual void init() override;

        virtual Status openImplementation() override;

        virtual void addedToMap(const class Map*) override;

        virtual void removedFromMap(const class Map*) override;

        virtual void prepareForRendering(TerrainEngine* engine) override;

        virtual Config getConfig() const override;

    protected:

        virtual ~SimpleOceanLayer() { }

    private:

        TextureImageUnitReservation _texReservation;

        void updateMaskLayer();
    };
    
}

#endif // OSGEARTH_UTIL_SIMPLE_OCEAN_LAYER
