/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
 *
 * This library is open source and may be redistributed and/or modified under
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 *
 * This library 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
 * OpenSceneGraph Public License for more details.
*/

#ifndef OSGPRESENTATION_GROUP
#define OSGPRESENTATION_GROUP 1

#include <osg/MatrixTransform>
#include <osg/ValueObject>
#include <osgPresentation/Action>

#include <sstream>

namespace osgPresentation {

typedef std::pair< osg::ref_ptr<osg::Object>, std::string> ObjectDescription;
typedef std::list< ObjectDescription > PropertyList;

/** META_Presentation macro define the standard clone, isSameKindAs, className
  * and accept methods.  Use when subclassing from Node to make it
  * more convenient to define the required pure virtual methods.*/
#define META_Presentation(name) \
        virtual osg::Object* cloneType() const { return new name (); } \
        virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new name (*this,copyop); } \
        virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const name *>(obj)!=NULL; } \
        virtual const char* className() const { return #name; } \
        virtual const char* libraryName() const { return "osgPresentation"; } \
        virtual void accept(osg::NodeVisitor& nv) \
        { \
            if (nv.validNodeMask(*this)) \
            { \
                nv.pushOntoNodePath(this); \
                osgPresentation::Action* action = dynamic_cast<osgPresentation::Action*>(&nv); \
                if (action) action->apply(*this); \
                else nv.apply(*this); \
                nv.popFromNodePath(); \
            } \
        }

/** osgPresentation::Group
*/
class OSGPRESENTATION_EXPORT Group : public osg::MatrixTransform
{
    public :

        Group() {}

        /** Copy constructor using CopyOp to manage deep vs shallow copy. */
        Group(const Group& group,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) : osg::MatrixTransform(group,copyop) {}

        META_Presentation(Group);

        /** Convinience method that casts the named UserObject to osg::TemplateValueObject<T> and gets the value.
            * To use this template method you need to include the osg/ValueObject header.*/
        template<typename T>
        bool getProperty(const std::string& name, T& value) const
        {
            return getUserValue(name, value);
        }

        /** Convinience method that creates the osg::TemplateValueObject<T> to store the
            * specified value and adds it as a named UserObject.
            * To use this template method you need to include the osg/ValueObject header. */
        template<typename T>
        void setProperty(const std::string& name, const T& value)
        {
            setUserValue(name, value);
        }

        /** Check for named Property, if it doesn't exist on this object check parents recursively for instances.*/
        osg::Object* getPropertyObject(const std::string& name, bool checkParents = true);

        /** Check for name Property, and convert to desired template value where possible. */
        template<typename T>
        bool getPropertyValue(const std::string& name, T& value, bool checkParents = true)
        {
            osg::Object* object = getPropertyObject(name, checkParents);
            if (!object) return false;

            typedef osg::TemplateValueObject<T> UserValueObject;
            const UserValueObject* uvo = dynamic_cast<const UserValueObject*>(object);
            if (uvo)
            {
                value = uvo->getValue();
                return true;
            }

            osg::StringValueObject* svo = dynamic_cast<osg::StringValueObject*>(object);
            if (svo)
            {
                std::istringstream str(svo->getValue());
                str >> value;
                return !str.fail();
            }
            return false;
        }

        /** Get all types of Properties supported by Presentation Object type, return true if the Properties are supported, false otherwise.*/
        virtual bool getSupportedProperties(PropertyList&) { return false; }


    protected :

        virtual ~Group() {}
};

}

#endif
