/* osgEarth
 * Copyright 2025 Pelican Mapping
 * MIT License
 */
#include "Common"
#include "SilverLiningOptions"
#include "SilverLiningAPIWrapper"
#include "SilverLiningCallback"
#include <osg/Referenced>
#include <osg/Light>
#include <osg/Camera>
#include <osgEarth/Threading>

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 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;
        std::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
