/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
* Copyright 2015 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.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* 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_ANNOTATION_ANNOTATION_UTILS_H
#define OSGEARTH_ANNOTATION_ANNOTATION_UTILS_H 1

#include <osgEarthAnnotation/Common>
#include <osgEarthSymbology/TextSymbol>
#include <osgEarthSymbology/Style>
#include <osg/AutoTransform>
#include <osg/CullStack>
#include <osg/Drawable>
#include <osg/Geometry>
#include <osgText/TextBase>
#include <osgUtil/CullVisitor>

namespace osgEarth { namespace Annotation
{
    using namespace osgEarth;
    using namespace osgEarth::Symbology;

    /**
     * Internal tools used by the annotation library.
     */
    struct OSGEARTHANNO_EXPORT AnnotationUtils
    {
        static const std::string& UNIFORM_HIGHLIGHT();

        static const std::string& UNIFORM_IS_TEXT();

        static const std::string& UNIFORM_FADE();

        static const std::string& PROGRAM_NAME();

        /**
         * Convert a symbology encoding enum to an osgText enum
         */
        static osgText::String::Encoding convertTextSymbolEncoding(const TextSymbol::Encoding encoding);

        /**
         * Creates a drawable representing a symbolized text label in
         * pixel space.
         */
        static osg::Drawable* createTextDrawable(
            const std::string& text,
            const TextSymbol*  symbol,
            const osg::Vec3&   positionOffset );

        /**
         * Creates the basic geometry to draw an image texture-mapped to
         * a quad in pixel space.
         */
        static osg::Geometry* createImageGeometry(
            osg::Image*       image,
            const osg::Vec2s& pixelOffsets,
            unsigned          textureUnit,
            double            heading,
            double            scale );

        /**
         * Creates a fading uniform that the decluttering engine can use
         * to adjust the alpha of annotation drawables.
         */
        static osg::Uniform* createFadeUniform();

        /**
         * Creates a boolean uniform used to indicate a hightlighted state.
         */
        static osg::Uniform* createHighlightUniform();

        /**
         * Builds a graph on top of the specified that that implements a 2-pass
         * rendering scheme for self-occluding or self-intersecting geometies that
         * would not otherwise properly blend. The scheme renders the back faces
         * first, followed by the front faces, ensuring proper blending.
         */
        static osg::Node* installTwoPassAlpha( osg::Node* );

        /**
         * Checks whether using a style will require transparency blending.
         */
        static bool styleRequiresAlphaBlending( const Style& style );

        /**
         * Internal - A customized AutoTransform used by the OrthoNode to
         * support intersections that are compatible with the decluttering engine
         */
        struct OrthoNodeAutoTransform : public osg::AutoTransform
        {
            /** override */
            virtual void accept(osg::NodeVisitor& nv);

            bool okToIntersect() const { return !_firstTimeToInitEyePoint; }
        };

        // some geometry creation utilities
        static osg::Drawable* create2DQuad( const osg::BoundingBox& box, float padding, const osg::Vec4& color );

        static osg::Drawable* create2DOutline( const osg::BoundingBox& box, float padding, const osg::Vec4& color );

        static osg::Node* createFullScreenQuad( const osg::Vec4& color );

        /**
         * Builds a sphere geometry.
         * @param r        Radius
         * @param color    Color
         * @param maxAngle Maximum angle between verts (controls tessellation)
         */
        static osg::Node* createSphere( float r, const osg::Vec4& color, float maxAngle =15.0f );

        /**
         * Builds a hemisphere geometry.
         * @param r        Radius
         * @param color    Color
         * @param maxAngle Maximum angle between verts (controls tessellation)
         */
        static osg::Node* createHemisphere( float r, const osg::Vec4& color, float maxAngle =15.0f );

        /**
         * Builds the geometry for an ellipsoid.
         */
        static osg::Geometry* createEllipsoidGeometry( 
            float xr, float yr, float zr, const osg::Vec4& color, float maxAngle =10.0f,
            float minLat =-90.0, float maxLat=90.0, float minLon=-180.0, float maxLon=180.0);
        
        static osg::Node* createEllipsoid( 
            float xr, float yr, float zr, const osg::Vec4& color, float maxAngle =10.0f,
            float minLat =-90.0, float maxLat=90.0, float minLon=-180.0, float maxLon=180.0);

        struct AltitudePolicy
        {
            AltitudePolicy() : draping(false), sceneClamping(false), gpuClamping(false) { }
            bool draping;
            bool sceneClamping;
            bool gpuClamping;
        };
        static void getAltitudePolicy( const Style& style, AltitudePolicy& b );


    private:
        AnnotationUtils() { }
    };

    
    /**
     * A bounding sphere calculator that forces the center point
     * to (0,0,0) and adjusts the radius accordingly. This forces the bounding
     * center to the "control point" of the annotation - useful for culling.
     */
    class ControlPointCallback : public osg::Node::ComputeBoundingSphereCallback
    {
    public:
        osg::BoundingSphere computeBound(const osg::Node& node) const
        {
            osg::BoundingSphere nodebs = node.computeBound();
            return osg::BoundingSphere(osg::Vec3(0,0,0), nodebs.center().length() + nodebs.radius());
        }
    };

} } // namespace osgEarth::Annotation

#endif //OSGEARTH_ANNOTATION_ANNOTATION_UTILS_H
