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

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

class CustomColorLayer
{
public:
    CustomColorLayer() { }

    CustomColorLayer(
        const osgEarth::ImageLayer* imageLayer,
        osg::Image* image,
        const osgTerrain::Locator* locator,
        int lod )
        : _layer(imageLayer), _locator(locator), _image(image), _lod(lod) { }

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

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

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

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

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

    int getLevelOfDetail() const {
        return _lod; }

    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;
    int                                      _lod;
};

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

#if 0
class CustomColorLayer : public osgTerrain::ImageLayer
{
public:
    CustomColorLayer(const osg::Image* image, osgEarth::ImageLayer* mapLayer):
      osgTerrain::ImageLayer( const_cast<osg::Image*>(image) ),
          _mapLayer(mapLayer),
          _lod(-1)
      {
      }

      UID getUID() const
      {
          return _mapLayer->getUID();
      }

      float getOpacity() const
      {
          return _mapLayer->getOpacity();
      }

      bool getEnabled() const
      {
          return _mapLayer->getEnabled();
      }

      int getLevelOfDetail() const
      {
          return _lod;
      }

      void setLevelOfDetail(int lod)
      {
          _lod = lod;
      }

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

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

      osg::ref_ptr<osgEarth::ImageLayer> _mapLayer;
      int _lod;
};
#endif

#endif // OSGEARTH_ENGINE_TRANSPARENT_LAYER
