/* -*-c++-*- */
/* osgEarth - Geospatial SDK for OpenSceneGraph
 * Copyright 2018 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_TRITON_LAYER
#define OSGEARTH_TRITON_LAYER 1

#include "Common"

#include <osgEarth/VisibleLayer>
#include <osgEarth/LayerListener>
#include <osgEarth/ImageLayer>
#include <osgEarthUtil/Ocean>
#include <osgEarthTriton/TritonCallback>

namespace osgEarth { namespace Triton
{

    /**
     * Serializable options for initializing the TritonLayer.
     */
    class OSGEARTHTRITON_EXPORT TritonLayerOptions : public osgEarth::VisibleLayerOptions
    {
    public:
        TritonLayerOptions(const osgEarth::ConfigOptions& options = osgEarth::ConfigOptions()) :
            osgEarth::VisibleLayerOptions( options )
        {
            _useHeightMap.init( true );
            _heightMapSize.init( 1024 );
            _renderBinNumber.init( 12 );
            _maxAltitude.init(50000);
            fromConfig( _conf );
        }

        /* User name for license activation */
        osgEarth::optional<std::string>& user() { return _user; }
        const osgEarth::optional<std::string>& user() const { return _user; }

        /* License code string */
        osgEarth::optional<std::string>& licenseCode() { return _licenseCode; }
        const osgEarth::optional<std::string>& licenseCode() const { return _licenseCode; }

        /* Triton resource path */
        osgEarth::optional<std::string>& resourcePath() { return _resourcePath; }
        const osgEarth::optional<std::string>& resourcePath() const { return _resourcePath; }

        /** Whether to pass a heightmap raster to Triton to mitigate coastline interference */
        osgEarth::optional<bool>& useHeightMap() { return _useHeightMap; }
        const osgEarth::optional<bool>& useHeightMap() const { return _useHeightMap; }

        /** Texture size to use for the height map */
        osgEarth::optional<int>& heightMapSize() { return _heightMapSize; }
        const osgEarth::optional<int>& heightMapSize() const { return _heightMapSize; }

        /** Render bin number to assign to the ocean (in DepthSortedBin) */
        optional<int>& renderBinNumber() { return _renderBinNumber; }
        const optional<int>& renderBinNumber() const { return _renderBinNumber; }

        /** Name of optional land mask layer. A land mask layer is an image layer
          * in the Map that has the alpha channel set to 1 over land and 0 over ocean.
          * This is used in addition to the height map to prevent drawing the ocean
          * over land areas.
          */
        optional<std::string>& maskLayer() { return _maskLayerName; }
        const optional<std::string>& maskLayer() const { return _maskLayerName; }

        /** Render bin number to assign to the ocean (in DepthSortedBin) */
        optional<float>& maxAltitude() { return _maxAltitude; }
        const optional<float>& maxAltitude() const { return _maxAltitude; }

    public:
        osgEarth::Config getConfig() const {
            osgEarth::Config conf = osgEarth::VisibleLayerOptions::getConfig();
            conf.set("user", _user);
            conf.set("license_code", _licenseCode);
            conf.set("resource_path", _resourcePath);
            conf.set("use_height_map", _useHeightMap);
            conf.set("height_map_size", _heightMapSize);
            conf.set("render_bin_number", _renderBinNumber);
            conf.set("mask_layer", _maskLayerName);
            conf.set("max_altitude", _maxAltitude);
            return conf;
        }

    protected:
        void mergeConfig( const osgEarth::Config& conf ) {
            osgEarth::VisibleLayerOptions::mergeConfig( conf );
            fromConfig( conf );
        }

    private:
        void fromConfig( const osgEarth::Config& conf ) {
            conf.get("user", _user);
            conf.get("license_code", _licenseCode);
            conf.get("resource_path", _resourcePath);
            conf.get("use_height_map", _useHeightMap);
            conf.get("height_map_size", _heightMapSize);
            conf.get("render_bin_number", _renderBinNumber);
            conf.get("mask_layer", _maskLayerName);
            conf.get("max_altitude", _maxAltitude);
        }

    private:
        osgEarth::optional<std::string> _user;
        osgEarth::optional<std::string> _licenseCode;
        osgEarth::optional<std::string> _resourcePath;
        osgEarth::optional<bool>        _useHeightMap;
        osgEarth::optional<int>         _heightMapSize;
        osgEarth::optional<int>         _renderBinNumber;
        osgEarth::optional<std::string> _maskLayerName;
        osgEarth::optional<float>       _maxAltitude;
    };


    /**
     * Node that roots the Triton adapter.
     */
    class OSGEARTHTRITON_EXPORT TritonLayer : public osgEarth::VisibleLayer
    {
    public:
        META_Layer(osgEarth, TritonLayer, TritonLayerOptions, triton);

        explicit TritonLayer(Callback* userCallback =0L);

        TritonLayer(const TritonLayerOptions& options, Callback* userCallback =0L);

        /** Masking layer for the ocean */
        void setMaskLayer(const osgEarth::ImageLayer* maskLayer);

        /** Maximum visibility altitude */
        void setMaxAltitude(float alt);
        float getMaxAltitude() const;

    public: // Layer

        virtual osg::Node* getNode() const;

    protected: // Layer

        virtual void init();

        virtual void addedToMap(const class osgEarth::Map*);

        virtual void removedFromMap(const class osgEarth::Map*);

    private:

        osg::ref_ptr<osg::Group> _root;
        osg::ref_ptr<osg::Node> _tritonNode;
        osg::ref_ptr<Callback> _callback;
        float _seaLevel;
        float _opacity;

    private:

        osgEarth::LayerListener<TritonLayer, const osgEarth::ImageLayer> _layerListener;
    };

} } // namespace osgEarth::Triton

#endif // OSGEARTH_TRITON_LAYER
