/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 * Copyright 2020 Pelican Mapping
 * http://osgearth.org
 *
 * osgEarth is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */
#ifndef OSGEARTH_FEATURE_RASTERIZER
#define OSGEARTH_FEATURE_RASTERIZER 1

#include <osgEarth/StyleSheet>
#include <osgEarth/Session>
#include <osgEarth/Feature>
#include <osgEarth/Filter>
#include <osgEarth/MapboxGLGlyphManager>

namespace osgEarth
{
    namespace Util
    {
        class OSGEARTH_EXPORT FeatureRasterizer
        {
        public:
            FeatureRasterizer(
                unsigned int width, 
                unsigned int height, 
                const GeoExtent& extent, 
                const Color& backgroundColor = Color::Transparent);

            FeatureRasterizer(
                osg::Image* preallocatedImage,
                const GeoExtent& extent);

            MapboxGLGlyphManager* getGlyphManager() const;
            void setGlyphManager(MapboxGLGlyphManager* glyphManager);

            //! A scaling factor that adjusts symbols to the desired pixel size.  Mostly useful for mapbox layers.
            float getPixelScale() const;
            void setPixelScale(float pixelScale);

            void render(
                const FeatureList& features,
                const Style& style,
                const FeatureProfile* profile =nullptr,
                const StyleSheet* sheet =nullptr);

            GeoImage finalize();        

            struct SymbolBoundingBoxes
            {
                bool intersects2d(const osg::BoundingBox& bb)
                {
                    for (auto& b : _bounds)
                    {
                        if (osg::maximum(b.xMin(), bb.xMin()) <= osg::minimum(b.xMax(), bb.xMax()) &&
                            osg::maximum(b.yMin(), bb.yMin()) <= osg::minimum(b.yMax(), bb.yMax()))
                        {
                            return true;
                        }
                    }
                    return false;
                }
                std::vector< osg::BoundingBox > _bounds;
            };

        private:
            GeoExtent _extent;
            osg::ref_ptr< osg::Image > _image;
            osg::ref_ptr< MapboxGLGlyphManager > _glyphManager;
            float _pixelScale = 1.0f;
            
            enum RenderFormat {
                RF_BGRA,
                RF_ABGR
            };
            RenderFormat _implPixelFormat;
            bool _inverted;            

            SymbolBoundingBoxes _symbolBoundingBoxes;


            void render_blend2d(
                const FeatureList& features,
                const Style& style,
                const FeatureProfile* profile,
                const StyleSheet* sheet);

            void render_agglite(
                const FeatureList& features,
                const Style& style,
                const FeatureProfile* profile,
                const StyleSheet* sheet);
        };

        class OSGEARTH_EXPORT FeatureStyleSorter
        {
        public:
            FeatureStyleSorter();

            using Function = std::function<void(
                const Style& style,
                FeatureList& features,
                ProgressCallback* progress
            )>;

            void sort(
                const TileKey& key,
                const Distance& buffer,
                Session* session,
                FeatureFilterChain* filters,
                Function processFeaturesForStyle,
                ProgressCallback* progress) const;

        protected:
            void getFeatures(
                Session* session,
                const Query& query,
                const Distance& buffer,
                const GeoExtent& extent,
                FeatureFilterChain* filters,
                FeatureList& output,
                ProgressCallback* progress) const;

            void sort_usingEmbeddedStyles(
                const TileKey& key,
                const Distance& buffer,
                FeatureFilterChain* filters,
                Session* session,
                Function processFeaturesForStyle,
                ProgressCallback* progress) const;

            void sort_usingSelectors(
                const TileKey& key,
                const Distance& buffer,
                FeatureFilterChain* filters,
                Session* session,
                Function processFeaturesForStyle,
                ProgressCallback* progress) const;

            void sort_usingOneStyle(
                const Style& style,
                const TileKey& key,
                const Distance& buffer,
                FeatureFilterChain* filters,
                Session* session,
                Function processFeaturesForStyle,
                ProgressCallback* progress) const;


        };
    }
}


#endif
