/* -*-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>

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);

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

            GeoImage finalize();

        private:
            GeoExtent _extent;
            osg::ref_ptr< osg::Image > _image;
            enum RenderFormat {
                RF_BGRA,
                RF_ABGR
            };
            RenderFormat _implPixelFormat;
            bool _inverted;

            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;
        };
    }
}


#endif
