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

#include <osgEarth/Common>
#include <osgEarth/Profile>
#include <osg/Referenced>
#include <osg/Image>
#include <osg/Shape>
#include <osg/Version>
#include <osgDB/ReaderWriter>
#include <osgTerrain/TerrainTile>
#include <string>

namespace osgEarth
{
    /**
     * Uniquely identifies a single tile on the map, relative to a Profile.
     */
    class OSGEARTH_EXPORT TileKey
    {
    public:        
        /**
         * Creates a new TileKey with the given tile xy at the specified level of detail
         * 
         * @param lod
         *       The level of detail (subdivision recursion level) of the tile
         * @param tile_x
         *       The x index of the tile
         * @param tile_y
         *       The y index of the tile
         * @param profile
         *       The profile for the tile
         */
        TileKey(
            unsigned int lod,
            unsigned int tile_x,
            unsigned int tile_y,
            const Profile* profile );

        TileKey( const TileKey& rhs );

        bool operator == (const TileKey& rhs) const {
            return valid() && rhs.valid() && _lod==rhs._lod && _x==rhs._x && _y==rhs._y;
        }
        bool operator != (const TileKey& rhs) const {
            return !(*this == rhs);
        }
        bool operator < (const TileKey& rhs) const {
            if (_lod < rhs._lod) return true;
            if (_lod > rhs._lod) return false;
            if (_x < rhs._x) return true;
            if (_x > rhs._x) return false;
            return _y < rhs._y;
        }

        /**
         * Canonical invalid tile key.
         */
        static TileKey INVALID;

        /**
         * Gets the string representation of the key, formatted like:
         * "lod_x_y"
         */
        std::string str() const { return _key; }

        /**
         * Gets a TileID corresponding to this key.
         */
        osgTerrain::TileID getTileId() const;

        /**
         * Gets the profile within which this keyes/>hs._x) return false;
            return _y < rhs._y;
        }

        /**
         * Canonical invalid tile key.
         */
        static TileKey INVALID;

        /**
         * Gets the string representation of the key, formatted like:
         * "lod_x_y"
         */
        std::string str() const { return _key; }

        /**
         * Gets a TileID corresponding to this key.
         */
        osgTerrain::TileID getTileId() const;

        /**
         * Gets the profile within which this keyes/>hs._x) return false;
            return _y < rhs._y;
        }

        /**
         * Canonical invalid tile key.
         */
        static TileKey INVALID;

        /**
         * Gets the string representation of the key, formatted like:
         * "lod_x_y"
         */
        std::string str() const { return _key; }

        /**
         * Gets a TileID corresponding to this key.
         */
        osgTerrain::TileID getTileId() const;

        /**
         * Gets the profile within which this keyes/>hs._x) return false;
            return _y < rhs._y;
        }

        /**
         * Canonical invalid tile key.
         */
        static TileKey INVALID;

        /**
         * Gets the string representation of the key, formatted like:
         * "lod_x_y"
         */
        std::string str() const { return _key; }

        /**
         * Gets a TileID corresponding to this key.
         */
        osgTerrain::TileID getTileId() const;

        /**
         * Gets the profile within which this keyes/>hs._x) return false;
            return _y < rhs._y;
        }

        /**
         * Canonical invalid tile key.
         */
        static TileKey INVALID;

        /**
         * Gets the string representation of the key, formatted like:
         * "lod_x_y"
         */
        std::string str() const { return _key; }

        /**
         * Gets a TileID corresponding to this key.
         */
        osgTerrain::TileID getTileId() const;

        /**
         * Gets the profile within which this keyes/>hs._x) return false;
            return _y < rhs._y;
        }

        /**
         * Canonical invalid tile key.
         */
        static TileKey INVALID;

        /**
         * Gets the string representation of the key, formatted like:
         * "lod_x_y"
         */
        std::string str() const { return _key; }

        /**
         * Gets a TileID corresponding to this key.
         */
        osgTerrain::TileID getTileId() const;

        /**
         * Gets the profile within which this keyes/>hs._x) return false;
            return _y < rhs._y;
        }

        /**
         * Canonical invalid tile key.
         */
        static TileKey INVALID;

        /**
         * Gets the string representation of the key, formatted like:
         * "