/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 * Copyright 2016 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/>
 */
#include "Common"
#include "SilverLiningOptions"
#include "SilverLiningAPIWrapper"
#include "SilverLiningCallback"
#include <osg/Referenced>
#include <osg/Light>
#include <osg/Camera>
#include <osgEarth/ThreadingUtils>

namespace SilverLining {
    class Atmosphere;
    class CloudLayer;
    class MillisecondTimer;
}
namespace osgEarth {
    class SpatialReference;
}

namespace osgEarth { namespace SilverLining
{
    /**
     * Contains all the SilverLining SDK pointers.
     */
    class SilverLiningContext : public osg::Referenced
    {
    public:
        SilverLiningContext(const SilverLiningOptions& options);

        /** Sets the light source that will represent the sun */
        void setLight(osg::Light* light);

        /** Sets the spatial reference system of the map */
        void setSRS(const SpatialReference* srs);

        /** Sets the minimum ambient lighting value */
        void setMinimumAmbient(const osg::Vec4f& value);

        /** Installs a user callback for SL initialization */
        void setCallback(Callback*);
        Callback* getCallback() const { return _callback.get(); }

    public: // accessors

        bool ready() const { return _initAttempted && !_initFailed; }

        ::SilverLining::Atmosphere* getAtmosphere() { return _atmosphere; }

        Atmosphere& getAtmosphereWrapper() { return *_atmosphereWrapper; }

        /** Spatial reference of the map */
        const SpatialReference* getSRS() const { return _srs.get(); }

        void setSkyBoxSize(double size) { _skyBoxSize = size; }
        double getSkyBoxSize() const { return _skyBoxSize; }

        void initialize(osg::RenderInfo& renderInfo);

        void updateLocation();

        void updateLight();

        /** Set/get the cached camers. NOT THREAD/MULTI-CAM SAFE. */
        /** TODO */
        void setCamera(osg::Camera* camera) { _camera = camera; }
        osg::Camera* getCamera() { return _camera.get(); }

        void setCameraPosition(const osg::Vec3d& pos) { _cameraPos = pos; }
        const osg::Vec3d& getCameraPosition() const { return _cameraPos; }

    protected:

        virtual ~SilverLiningContext();

    private:

        void setupClouds();

    private:
        ::SilverLining::Atmosphere* _atmosphere;

        double _skyBoxSize;

        osg::observer_ptr<osg::Light>                  _light;
        osg::ref_ptr<const osgEarth::SpatialReference> _srs;

        bool                       _initAttempted;
        bool                       _initFailed;
        osgEarth::Threading::Mutex _initMutex;

        double _maxAmbientLightingAlt;

        osg::observer_ptr<osg::Camera> _camera;
        osg::Vec3d                     _cameraPos; // eye point
        osg::Vec4f                     _minAmbient;

        SilverLiningOptions _options;

        osg::ref_ptr<Callback> _callback;

        Atmosphere* _atmosphereWrapper;

        ::SilverLining::MillisecondTimer* _msTimer;
    };

} } // namespace osgEarth::SilverLining
