/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 * Copyright 2016 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 OSGEARTHSYMBOLOGY_MARKER_SYMBOL_H
#define OSGEARTHSYMBOLOGY_MARKER_SYMBOL_H 1

#include <climits>

#include <osgEarth/Common>
#include <osgEarthSymbology/Symbol>
#include <osgEarthSymbology/Expression>
#include <osg/Vec3f>

namespace osgEarth { namespace Symbology
{
    class InstanceSymbol;

    /**
     * The MarkerSymbol describes replacement of geometry with "markers". A marker
     * it another object like a 3D model (node) or an image.
     */
    class OSGEARTHSYMBOLOGY_EXPORT MarkerSymbol : public Symbol
    {
    public:
        /**
         * Controls the placements of models.
         */
        enum Placement
        {
            /** Places one model instance at the centroid of the feature. */
            PLACEMENT_CENTROID,

            /** Places a model instance at each feature point. */
            PLACEMENT_VERTEX,

            /** Places model instances at regular intervals within/along the feature geometry,
                according to density. */
            PLACEMENT_INTERVAL,

            /** Scatters model instances randomly within/along feature, according to density. */
            PLACEMENT_RANDOM
        };

        // note: these are similar to the values in osgText::Text::AlignmentType
        enum Alignment {
            ALIGN_LEFT_TOP,
            ALIGN_LEFT_CENTER,
            ALIGN_LEFT_BOTTOM,

            ALIGN_CENTER_TOP,
            ALIGN_CENTER_CENTER,
            ALIGN_CENTER_BOTTOM,

            ALIGN_RIGHT_TOP,
            ALIGN_RIGHT_CENTER,
            ALIGN_RIGHT_BOTTOM,
        };

    public:
        META_Object(osgEarthSymbology, MarkerSymbol);

        MarkerSymbol(const MarkerSymbol& rhs,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
        MarkerSymbol( const Config& conf =Config() );


        /** Since MarkerSymbol is deprecated, this conveneince function will convert to an InstanceSymbol */
        class InstanceSymbol* convertToInstanceSymbol() const;

        /** dtor */
        virtual ~MarkerSymbol() { }

        /** URI of the model to use for substitution. */
        optional<StringExpression>& url() { return _url; }
        const optional<StringExpression>& url() const { return _url; }     

        /** Name of the resource library to use with this symbol (optional) */
        optional<StringExpression>& library() { return _library; }
        const optional<StringExpression>& library() const { return _library; }   

        /** How to map feature geometry to model placement. (default is PLACEMENT_CENTROID) */
        optional<Placement>& placement() { return _placement; }
        const optional<Placement>& placement() const { return _placement; }

        /** For PLACEMENT_RANDOM/INTERVAL, the scattering density in instances per sqkm */
        optional<float>& density() { return _density; }
        const optional<float>& density() const { return _density; }

        /** Model instance scale factor */
        optional<NumericExpression>& scale() { return _scale; }
        const optional<NumericExpression>& scale() const { return _scale; }

        /** Orientation in HPR degrees */
        optional<osg::Vec3f>& orientation() { return _orientation; }
        const optional<osg::Vec3f>& orientation() const { return _orientation; }

        /** Seeding value for the randomizer */
        optional<unsigned>& randomSeed() { return _randomSeed; }
        const optional<unsigned>& randomSeed() const { return _randomSeed; }

        /** Hint as to whether the marker is an icon vs. a 3D model */
        optional<bool>& isModel() { return _isModelHint; }
        const optional<bool>& isModel() const { return _isModelHint; }

        /** Alignment of the marker relative to center pixels */
        optional<Alignment>& alignment() { return _alignment; }
        const optional<Alignment>& alignment() const { return _alignment; }
        
    public: // non-serialized properties (for programmatic use only)

        /** Explicit image to use for 2D icon placemet */
        void setImage( osg::Image* image ) { _image = image; }
        osg::Image* getImage( unsigned maxSize =INT_MAX ) const;

        /** Explicit model to use for model placement */
        void setModel( osg::Node* node ) { _node = node; }
        osg::Node* getModel() const { return _node.get(); }

    public:
        virtual Config getConfig() const;
        virtual void mergeConfig( const Config& conf );
        static void parseSLD(const Config& c, class Style& style);

    protected:
        optional<StringExpression>   _url;
        optional<StringExpression>   _library;
        optional<NumericExpression>  _scale;
        optional<Placement>          _placement;
        optional<osg::Vec3f>         _orientation;
        optional<float>              _density;
        optional<unsigned>           _randomSeed;
        optional<bool>               _isModelHint;
        optional<Alignment>         _alignment;

        osg::ref_ptr<osg::Node>      _node;
        mutable osg::ref_ptr<osg::Image> _image;
    };

} } // namespace osgEarth::Symbology

#endif // OSGEARTHSYMBOLOGY_MARKER_SYMBOL_H
