/* -*-c++-*- */
/* osgEarth - Geospatial SDK for OpenSceneGraph
 * Copyright 2020 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_XYZ_H
#define OSGEARTH_XYZ_H

#include <osgEarth/Common>
#include <osgEarth/ImageLayer>
#include <osgEarth/ElevationLayer>
#include <osgEarth/URI>
#include <osgEarth/Threading>

/**
 * XYZ layers. These are general purpose tiled layers that conform
 * to a X/Y/Z prototype where Z is the tiling level and X/Y are the
 * tile offsets on that level.
 */

//! XYZ namespace contains support classes used to the Layers
namespace osgEarth { namespace XYZ
{
    /**
     * Underlying XYZ driver that does the actual I/O
     */
    class OSGEARTH_EXPORT Driver
    {
    public:
        Status open(
            const URI& uri,
            const std::string& format,
            DataExtentList& out_dataExtents,
            const osgDB::Options* readOptions);

        ReadResult read(
            const URI& uri,
            const TileKey& key, 
            bool invertY,
            ProgressCallback* progress,
            const osgDB::Options* readOptions) const;

    protected:
        std::string _format;
        std::string _template;
        std::string _rotateChoices;
        std::string _rotateString;
        std::string::size_type _rotateStart, _rotateEnd;
        mutable std::atomic_int _rotate_iter;
    };

    // Internal serialization options
    class OSGEARTH_EXPORT XYZImageLayerOptions : public ImageLayer::Options
    {
    public:
        META_LayerOptions(osgEarth, XYZImageLayerOptions, ImageLayer::Options);
        OE_OPTION(URI, url);
        OE_OPTION(bool, invertY);
        OE_OPTION(std::string, format);
        static Config getMetadata();
        virtual Config getConfig() const;
    private:
        void fromConfig(const Config& conf);
    };

    // Internal serialization options
    class OSGEARTH_EXPORT XYZElevationLayerOptions : public ElevationLayer::Options
    {
    public:
        META_LayerOptions(osgEarth, XYZElevationLayerOptions, ElevationLayer::Options);
        OE_OPTION(URI, url);
        OE_OPTION(bool, invertY);
        OE_OPTION(std::string, format);
        OE_OPTION(std::string, elevationEncoding);
        static Config getMetadata();
        virtual Config getConfig() const;
    private:
        void fromConfig(const Config& conf);
    };
} }


namespace osgEarth
{
    /**
     * Image layer connected to a generic, raw tile service and accesible
     * via a URL template.
     *
     * The template pattern will vary depending on the structure of the data source.
     * Here is an example URL:
     *
     *    http://[abc].tile.openstreetmap.org/{z}/{x}/{y}.png
     *
     * {z} is the level of detail. {x} and {y} are the tile indices at that
     * level of detail. The [] delimiters indicate a URL "rotation"; for each
     * subsequent request, one and only one of the characters inside the []
     * will be used.
     *
     * XYZ accesses a "raw" data source and reads no metadata. Thus you must
     * expressly provide a geospatial Profile by calling setProfile() on the
     * layer before opening it or adding it to the Map. For example, for the
     * pattern above you might want a spherical mercator profile:
     *
     *    layer->setProfile( Profile::create(Profile::SPHERICAL_MERCATOR) );
     */
    class OSGEARTH_EXPORT XYZImageLayer : public ImageLayer
    {
    public: // serialization
        typedef XYZ::XYZImageLayerOptions Options;

    public:
        META_Layer(osgEarth, XYZImageLayer, Options, ImageLayer, XYZImage);

    public:
        //! Base URL for requests
        void setURL(const URI& value);
        const URI& getURL() const;

        //! Tiling profile (required)
        void setProfile(const Profile* profile) override;

        //! Whether to flip the Y axis for tile indexing
        void setInvertY(const bool& value);
        const bool& getInvertY() const;

        //! Data format to request from the service
        void setFormat(const std::string& value);
        const std::string& getFormat() const;

    public: // Layer
        
        //! Establishes a connection to the data
        virtual Status openImplementation() override;

        //! Creates a raster image for the given tile key
        virtual GeoImage createImageImplementation(const TileKey& key, ProgressCallback* progress) const override;

    protected: // Layer

        //! Called by constructors
        virtual void init();

    protected:

        //! Destructor
        virtual ~XYZImageLayer() { }

    private:
        XYZ::Driver _driver;
    };


    /**
     * Elevation layer that pulls from an XYZ encoded URL
     */
    class OSGEARTH_EXPORT XYZElevationLayer : public ElevationLayer
    {
    public:
        typedef XYZ::XYZElevationLayerOptions Options;

    public:
        META_Layer(osgEarth, XYZElevationLayer, Options, ElevationLayer, XYZElevation);
        
        //! Base URL for requests
        void setURL(const URI& value);
        const URI& getURL() const;

        //! Tiling profile (required)
        void setProfile(const Profile* profile) override;

        //! Whether to flip the Y axis for tile indexing
        void setInvertY(const bool& value);
        const bool& getInvertY() const;

        //! Data format to request from the service
        void setFormat(const std::string& value);
        const std::string& getFormat() const;

        //! Encoding encoding type
        void setElevationEncoding(const std::string& value);
        const std::string& getElevationEncoding() const;

    public: // Layer
        
        //! Establishes a connection to the XYZ data
        virtual Status openImplementation() override;

        //! Creates a heightfield for the given tile key
        virtual GeoHeightField createHeightFieldImplementation(const TileKey& key, ProgressCallback* progress) const override;

    protected: // Layer

        //! Called by constructors
        virtual void init();

    protected:

        //! Destructor
        virtual ~XYZElevationLayer() { }

    private:
        osg::ref_ptr<XYZImageLayer> _imageLayer;
    };

} // namespace osgEarth

OSGEARTH_SPECIALIZE_CONFIG(osgEarth::XYZImageLayer::Options);
OSGEARTH_SPECIALIZE_CONFIG(osgEarth::XYZElevationLayer::Options);

#endif // OSGEARTH_XYZ_H
