/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 * Copyright 2008-2010 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_STYLE_H
#define OSGEARTHSYMBOLOGY_STYLE_H 1

#include <osgEarthSymbology/Common>
#include <osgEarthSymbology/Query>
#include <osgEarthSymbology/Symbol>
#include <osgEarth/Config>
#include <osgEarth/Revisioning>
#include <osg/Object>
#include <vector>

namespace osgEarth { namespace Symbology 
{
    /**
     * A Style is an unordered collection of Symbols.
     */
    class OSGEARTHSYMBOLOGY_EXPORT Style : public osgEarth::Revisioned<osg::Object>
    {
    public:
        /** Constructs a new, empty style. */
        Style();

        /** Constructs a style by deserializing it from a Config. */
        Style(const Config&);

        /** Copy constructor. */
        Style(const Style&, const osg::CopyOp&);

        META_Object(osgEarthSymbology, Style);

        /** Adds a symbol to the collection. */
        void addSymbol(Symbol* symbol);

        /** Gets a typed symbol from the style (the first one found). */
        template<typename T>
        T* getSymbol()
        {
            for (SymbolList::const_iterator it = _symbols.begin(); it != _symbols.end(); ++it)
            {
                Symbol* symbol = *it;
                T* s = dynamic_cast<T*>(symbol);
                if (s)
                    return s;
            }
            return 0L;
        }

        /** Gets a typed symbol from the style (the first one found) */
        template<typename T>
        const T* getSymbol() const
        {
            for (SymbolList::const_iterator it = _symbols.begin(); it != _symbols.end(); ++it)
            {
                Symbol* symbol = *it;
                const T* s = dynamic_cast<const T*>(symbol);
                if (s)
                    return s;
            }
            return 0L;
        }

        /** Gets a typed symbol from the style (first one found), or creates/adds/returns one of 
            that type if it doesn't already exist. */
        template<typename T>
        T* getOrCreateSymbol()
        {
            T* sym = getSymbol<T>();
            if ( !sym )
            {
                sym = new T();
                addSymbol( sym );
            }
            return sym;
        }

        /** Access sub-styles. */
        Style* getSubStyle( const std::string& name );
        const Style* getSubStyle( const std::string& name ) const;
        void addSubStyle( Style* style );

        /** Serializes this object into a Config. */
        virtual Config getConfig() const;

        void mergeConfig( const Config& conf );
        void fromCSS( const std::string& css );

    protected:
        SymbolList                  _symbols;

        std::string                 _origType;
        std::string                 _origData;
        optional<std::string>       _url;

        typedef std::map<std::string,osg::ref_ptr<Style> > StylesByName;
        StylesByName _subStyles;
    };

    typedef std::vector< osg::ref_ptr<Style> > StyleList;
    typedef std::map< std::string, osg::ref_ptr<Style> > StyleMap;


    class OSGEARTHSYMBOLOGY_EXPORT StyleComponent
    {
    };

    /**
     * A style selector lets you classify styles based on rules, such as a
     * feature query. By default the selector selects the style with the
     * same name as the selector, but you can override this by settings
     * the styleName property.
     */
    class OSGEARTHSYMBOLOGY_EXPORT StyleSelector : public StyleComponent
    {
    public:
        StyleSelector( const Config& conf =Config() );

    public: // properties

        /** Name of this style class. */
        std::string& name() { return _name; }
        const std::string& name() const { return _name; }

        /** Name of the style to select */
        optional<std::string>& styleName() { return _styleName; }
        const optional<std::string>& styleName() const { return _styleName; }

        /** Expression/spatial filter used to select items to which the style will apply */
        optional<Query>& query() { return _query; }
        const optional<Query>& query() const { return _query; }

        /** Returns the styleName() property, it set; otherwise returns the selector name. */
        std::string getSelectedStyleName() const;

        //Configurable
        virtual void mergeConfig( const Config& conf );
        virtual Config getConfig() const;

    protected:
        std::string _name;
        optional<std::string> _styleName;
        optional<Query> _query;
    };

    typedef std::list<StyleSelector> StyleSelectorList;



    class OSGEARTHSYMBOLOGY_EXPORT StyleCatalog : public Configurable
    {
    public:
        StyleCatalog( const Config& conf =Config() );

        StyleSelectorList& selectors() { return _selectors; }
        const StyleSelectorList& selectors() const { return _selectors; }

        void addStyle( Style* style );
        void removeStyle( const std::string& name );
        bool getStyle( const std::string& name, Style*& out ) const;
        const Style* getDefaultStyle() const;

        virtual Config getConfig() const;
        virtual void mergeConfig( const Config& conf );

    protected:
        StyleSelectorList _selectors;
        StyleMap _styles;
        osg::ref_ptr<Style> _emptyStyle;
    };

} } // namespace osgEarth::Symbology


#endif // OSGEARTHSYMBOLOGY_STYLE_H
