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

#include "Common"
#include <osgEarth/Common>
#include <osgTerrain/Locator>
#include <osgTerrain/Layer>
#include <osgEarth/ImageLayer>
#include <osgEarth/TileKey>
#include <osg/Image>

namespace osgEarth_engine_osgterrain
{
    class CustomColorLayer
    {
    public:
        CustomColorLayer() { }

        /** dtor */
        virtual ~CustomColorLayer() { }

        CustomColorLayer(
            const osgEarth::ImageLayer* imageLayer,
            osg::Image* image,
            const osgTerrain::Locator* locator,
            int lod,
            const osgEarth::TileKey& tileKey,
            bool fallbackData =false )
            : _layer(imageLayer), _locator(locator), _image(image),  _tileKey(tileKey), _lod(lod), _fallbackData(fallbackData) { }

        CustomColorLayer( const CustomColorLayer& rhs ) :
            _layer( rhs._layer.get() ),        
            _locator( rhs._locator.get() ),
            _image( rhs._image.get() ),
            _tileKey( rhs._tileKey ),
            _lod( rhs._lod ),
            _fallbackData( rhs._fallbackData ) { }

        osgEarth::UID getUID() const {
            return _layer->getUID();
        }

        const osgTerrain::Locator* getLocator() const {
            return _locator.get();
        }

        osg::Image* getImage() const { 
            return _image.get(); }

        const osgEarth::TileKey& getTileKey() const {
            return _tileKey; }

        const osgEarth::ImageLayer* getMapLayer() const {
            return _layer.get(); }

        int getLevelOfDetail() const {
            return _lod; }

        bool isFallbackData() const {
            return _fallbackData; }

        osg::BoundingSphere computeBound() const {
            osg::BoundingSphere bs;
            osg::Vec3d v;
            if (getLocator()->convertLocalToModel(osg::Vec3d(0.5,0.5,0.0), v)) {
                bs.center() = v;
            }
            if (getLocator()->convertLocalToModel(osg::Vec3d(0.0,0.0,0.0), v)) {
                bs.radius() = (bs.center() - v).length();
            }
            return bs;
        }


    private:
        osg::ref_ptr<const osgEarth::ImageLayer> _layer;
        osg::ref_ptr<const osgTerrain::Locator>  _locator;
        osg::ref_ptr<osg::Image>                 _image;
        osgEarth::TileKey                        _tileKey;
        int                                      _lod;
        bool                                     _fallbackData;
    };

    class CustomColorLayerRef : public osg::Referenced
    {
    public:
        CustomColorLayerRef( const CustomColorLayer& layer ) : _layer(layer) { }
        CustomColorLayer _layer;
    };

    class CustomElevLayer
    {
    public:
        CustomElevLayer() { }

        /** dtor */
        virtual ~CustomElevLayer() { }

        CustomElevLayer( osgTerrain::HeightFieldLayer* hfLayer, bool fallbackData =false )
            : _hfLayer(hfLayer), _fallbackData(fallbackData) { }

        osgTerrain::HeightFieldLayer* getHFLayer() {
            return _hfLayer.get(); }

        bool isFallbackData() const {
            return _fallbackData; }

    private:
        osg::ref_ptr<osgTerrain::HeightFieldLayer> _hfLayer;
        bool _fallbackData;
    };

} // namespace osgEarth_engine_osgterrain

#endif // OSGEARTH_ENGINE_TRANSPARENT_LAYER
