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

#include <osgEarthSymbology/Common>
#include <osgEarth/Config>
#include <osgEarth/StringUtils>
#include <osg/CopyOp>
#include <vector>
#include <set>
#include <algorithm>

namespace osgEarth { namespace Symbology
{
    typedef std::vector<std::string> TagVector;
    typedef std::set<std::string>    TagSet;

    template<typename T>
    class Taggable : public T
    {
    public:
        Taggable() { }

        //Taggable(const osgEarth::Config& conf) : T(conf) { }
        
        Taggable(const Taggable& rhs,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
        T(rhs, copyop)
        {
            _tags = rhs._tags;
        }

        void addTag( const std::string& tag ) {
            _tags.insert( normalize( tag ) );
        }
        void addTags( const TagVector& tags ) {
            for( TagVector::const_iterator i = tags.begin(); i != tags.end(); ++i )
                _tags.insert( normalize(*i) );
        }
        void addTags( const std::string& tagString ) {
            TagVector tags;
            StringTokenizer( tagString, tags, " ", "\"'", false, true );
            addTags( tags );
        }
        void removeTag( const std::string& tag ) {
            _tags.erase( normalize( tag ) );
        }
        bool containsTag( const std::string& tag ) const {
            return _tags.find( normalize( tag )) != _tags.end();
        }

        bool containsTags( const TagSet& tags) const {
            for( TagSet::const_iterator i = tags.begin(); i != tags.end(); i++ ) {
               if ( _tags.find( normalize( *i ) ) == _tags.end() )
                  return false;
            }
            return true;            
        }

        bool containsTags( const TagVector& tags) const {
            for( TagVector::const_iterator i = tags.begin(); i != tags.end(); i++ ) {
               if ( _tags.find( normalize( *i ) ) == _tags.end() )
                  return false;
            }
            return true;            
        }

        const TagSet& tags() const { return _tags; }

        static std::string tagString(const TagSet& tags) {
            std::stringstream buf;
            for( TagSet::const_iterator i = tags.begin(); i != tags.end(); i++ )
                buf << (i != tags.begin()? " " : "") << *i;
            std::string result = buf.str();
            return result;
        }

        static std::string tagString(const TagVector& tags) {
            std::stringstream buf;
            for( TagVector::const_iterator i = tags.begin(); i != tags.end(); i++ )
                buf << (i != tags.begin()? " " : "") << *i;
            std::string result = buf.str();
            return result;
        }

        std::string tagString() const {
            std::stringstream buf;
            for( TagSet::const_iterator i = _tags.begin(); i != _tags.end(); i++ )
                buf << (i != _tags.begin()? " " : "") << *i;
            std::string result = buf.str();
            return result;
        }

    protected:
        
        TagSet _tags;

    private:

        std::string normalize( const std::string& input ) const {
            return osgEarth::toLower(input);
        }
    };

} } // namespace osgEarth::Symbology

#endif // OSGEARTHSYMBOLOGY_QUERY_H
