/* osgEarth
 * Copyright 2008-2014 Pelican Mapping
 * MIT License
 */
#ifndef OSGEARTH_DRIVERS_REX_RENDER_BINDINGS_H
#define OSGEARTH_DRIVERS_REX_RENDER_BINDINGS_H 1

#include "Common"
#include <osgEarth/Common>
#include <osgEarth/optional>
#include <osgEarth/Containers>
#include <osgEarth/GLUtils>
#include <osg/Texture>
#include <osg/Matrix>

namespace osgEarth { namespace REX
{
    using namespace osgEarth;
    using namespace osgEarth::Util;

    /**
     * Defines the usage information for a single texture sampler.
     */
    class SamplerBinding
    {
    public:
        enum Usage
        {
            COLOR         = 0,
            COLOR_PARENT  = 1,
            ELEVATION     = 2,
            NORMAL        = 3,
            LANDCOVER     = 4,
            SHARED        = 5   // non-core shared layers start at this index
        };

    public:
        SamplerBinding() : _unit(-1) { }

        /** Optional UID of the source (usually a layer) to which this binding applies */
        optional<osgEarth::UID>& sourceUID()             { return _sourceUID; }
        const optional<osgEarth::UID>& sourceUID() const { return _sourceUID; }

        /** Optional usage hint */
        optional<Usage>& usage()             { return _usage; }
        const optional<Usage>& usage() const { return _usage; }

        /** Texture image unit of the sampler */
        int& unit() { return _unit; }
        const int& unit() const { return _unit; }

        /** Uniform name of the sampler */
        std::string& samplerName() { return _samplerName; }
        const std::string& samplerName() const { return _samplerName; }

        /** Uniform name of the sampler matrix */
        std::string& matrixName() { return _matrixName; }
        const std::string& matrixName() const { return _matrixName; }

        //! Default image to use at the top level when no data it available
        osg::Texture* getDefaultTexture() const { return _defaultImage.get(); }
        void setDefaultTexture(osg::Texture* value) { _defaultImage = value; }

    public:
        /** True if this binding is bound to a teture image unit and therefore active. */
        bool isActive() const {
            return _unit >= 0 || usage().isSet() || sourceUID().isSet();
        }

    private:
        optional<UID>   _sourceUID;
        optional<Usage> _usage;
        int             _unit;
        std::string     _samplerName;
        std::string     _matrixName;
        osg::ref_ptr<osg::Texture> _defaultImage;
    };

    /**
     * Array of render bindings. This array is always indexed by the 
     * SamplerBinding::USAGE itself. So for example, RenderBindings[0] is always
     * the COLOR usage, [2] is the ELEVATION usage, etc. Shared layer bindings
     * (i.e., custom samplers) start at index SHARED and go up from there.
     */
    typedef AutoArray<SamplerBinding> RenderBindings;

} } // namespace osgEarth::REX

#endif // OSGEARTH_DRIVERS_REX_RENDER_BINDINGS_H
