/* -*-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 OSGEARTHFEATURES_FEATURE_H
#define OSGEARTHFEATURES_FEATURE_H 1

#include <osgEarthFeatures/Common>

#include <osgEarthSymbology/Geometry>
#include <osgEarthSymbology/Style>

#include <osgEarth/GeoCommon>
#include <osgEarth/SpatialReference>
#include <osg/Array>
#include <osg/Shape>
#include <map>
#include <list>

namespace osgEarth { namespace Features
{
    using namespace osgEarth;
    using namespace osgEarth::Symbology;

    class FilterContext;
    class Session;

    /**
     * Metadata and schema information for feature data.
     */
    class OSGEARTHFEATURES_EXPORT FeatureProfile : public osg::Referenced
    {
    public:        
        FeatureProfile( const GeoExtent& extent );

        virtual ~FeatureProfile() { }

        /** Gets the spatial extents of the features in this profile. */
        const GeoExtent& getExtent() const { return _extent; }

        /** Gets the spatial reference system of feature shapes in this class. */
        const SpatialReference* getSRS() const { return _extent.getSRS(); }

        /** Whether the feature data is pre-tiled */
        bool getTiled() const;
        void setTiled(bool tiled);

        int getFirstLevel() const;
        void setFirstLevel(int firstLevel );

        int getMaxLevel() const;
        void setMaxLevel(int maxLevel);

        const osgEarth::Profile* getProfile() const;
        void setProfile( const osgEarth::Profile* profile );

        /** Interpolation method for geodetic data */
        optional<GeoInterpolation>& geoInterp() { return _geoInterp; }
        const optional<GeoInterpolation>& geoInterp() const { return _geoInterp; }

    protected:
        osg::ref_ptr< const osgEarth::Profile > _profile;
        GeoExtent _extent;
        bool _tiled;
        int _firstLevel;
        int _maxLevel;
        optional<GeoInterpolation> _geoInterp;
    };

    struct AttributeValueUnion
    {
        std::string stringValue;
        double      doubleValue;
        int         intValue;
        bool        boolValue;

        //Whether the value is set.  A value of false means the value is effectively NULL
        bool        set;
    };

    enum AttributeType
    {
        ATTRTYPE_UNSPECIFIED,
        ATTRTYPE_STRING,
        ATTRTYPE_INT,
        ATTRTYPE_DOUBLE,
        ATTRTYPE_BOOL
    };

    struct OSGEARTHFEATURES_EXPORT AttributeValue : public std::pair<AttributeType,AttributeValueUnion>
    {    
        std::string getString() const;
        double getDouble( double defaultValue =0.0 ) const;
        int getInt( int defaultValue =0 ) const;
        bool getBool( bool defaultValue =false ) const;              
    };
    
    typedef std::map<std::string, AttributeValue, CIStringComp> AttributeTable;

    typedef unsigned long FeatureID;

    /**
     * Wraps a FeatureID in a referenced object.
     */
    class RefFeatureID : public osg::Referenced
    {
    public:
        RefFeatureID( FeatureID fid ) : _fid(fid) { }
        virtual ~RefFeatureID() { }

        operator FeatureID () const { return _fid; }
    protected:
        FeatureID _fid;
    };


    typedef std::map< std::string, AttributeType > FeatureSchema;

    class Feature;

    typedef std::list< osg::ref_ptr<Feature> > FeatureList;

    /**
     * Basic building block of vector feature data.
     */
    class OSGEARTHFEATURES_EXPORT Feature : public osg::Object
    {
    public:        

        Feature( Geometry* geom, const SpatialReference* srs, const Style& style =Style(), FeatureID fid =0L );

        /** Copy contructor */
        Feature( const Feature& rhs, const osg::CopyOp& copyop =osg::CopyOp::DEEP_COPY_ALL );

        META_Object( osgEarthFeatures, Feature );

    public:

        /**
         * The unique ID of this feature (unique relative to its provider)
         */
        FeatureID getFID() const;

        /**
         * Set the FID of this feature.
         */
        void setFID(FeatureID fid);

        /**
         * Gets the GeoExtent of this Feature
         */
        GeoExtent getExtent() const;

        /**
         * The geometry in this feature.
         */
        void setGeometry( Symbology::Geometry* geom );
        Symbology::Geometry* getGeometry() { dirty(); return _geom.get(); }
        const Symbology::Geometry* getGeometry() const { return _geom.get(); }

        /**
         * The spatial reference of the geometry in this feature.
         */
        const SpatialReference* getSRS() const { return _srs.get(); }
        void setSRS( const SpatialReference* srs );

        /**
         * Computes the bound of this feature in the specified SRS.
         */
        bool getWorldBound( const SpatialReference* srs, osg::BoundingSphered& out_bound ) const;

        /** 
         * Gets a polytope, in world coordinates (proj or ECEF) that bounds the
         * geographic extents covered by this feature. This is useful for roughly
         * intersecting the feature with the terrain graph.
         */
        bool getWorldBoundingPolytope( const SpatialReference* srs, osg::Polytope& out_polytope ) const;

        /** 
         * Gets a polytope, in world coordinates (proj or ECEF) that bounds the
         * world coordinates covered by the given bounding sphere. This is useful for roughly
         * intersecting features with the terrain graph.
         */
        static bool getWorldBoundingPolytope( const osg::BoundingSphered& bs, const SpatialReference* srs, osg::Polytope& out_polytope );

        /**
         * Calculates the extent of this feature.
         */
        GeoExtent calculateExtent() const;


        const AttributeTable& getAttrs() const { return _attrs; }

        void set( const std::string& name, const std::string& value );
        void set( const std::string& name, double value );
        void set( const std::string& name, int value );
        void set( const std::string& name, bool value );
        void set( const std::string& name, const AttributeValue& value);

        /** Sets the attribute to NULL */         
        void setNull( const std::string& name );
        void setNull( const std::string& name, AttributeType type );

        bool hasAttr( const std::string& name ) const;

        std::string getString( const std::string& name ) const;
        double getDouble( const std::string& name, double defaultValue =0.0 ) const;
        int getInt( const std::string& name, int defaultValue =0 ) const;
        bool getBool( const std::string& name, bool defaultValue =false ) const;

        /**
         * Gets whether the attribute is set, meaning it is non-NULL
         */
        bool isSet( const std::string& name ) const;

        /** Embedded style. */
        optional<Style>& style() { return _style; }
        const optional<Style>& style() const { return _style; }

        /** Geodetic interpolation method. */
        optional<GeoInterpolation>& geoInterp() { dirty(); return _geoInterp; }
        const optional<GeoInterpolation>& geoInterp() const { return _geoInterp; }

        /** populates the variables of an expression with attribute values and evals the expression. */
        double eval(NumericExpression& expr, const FilterContext* context) const;
        double eval(NumericExpression& expr, Session* session) const;
        
        /** populates the variables of an expression with attribute values and evals the expression. */
        const std::string& eval(StringExpression& expr, const FilterContext* context) const;
        const std::string& eval(StringExpression& expr, Session* session) const;

    public:
        /** Gets a GeoJSON representation of this Feature */
        std::string getGeoJSON() const;

        /** Gets a FeatureList as a GeoJSON FeatureCollection */
        static std::string featuresToGeoJSON(const FeatureList& features);

    public:
        /**
         * Transforms this Feature to the given SpatialReference
         */
        void transform( const SpatialReference* srs );

        /**
         * Splits this feature into multiple features if it is a geodetic feature and cross the date line.
         */
        void splitAcrossDateLine(FeatureList& splitFeatures);

    protected:

        Feature( FeatureID fid =0L );

        virtual ~Feature();

        FeatureID                            _fid;
        osg::ref_ptr<Geometry>               _geom;
        osg::ref_ptr<const SpatialReference> _srs;
        AttributeTable                       _attrs;
        optional<Style>                      _style;
        optional<GeoInterpolation>           _geoInterp;
        GeoExtent                            _cachedExtent;

        void dirty();
    };



} } // namespace osgEarth::Features

#endif // OSGEARTHFEATURES_FEATURE_H
