/* osgEarth
 * Copyright 2025 Pelican Mapping
 * MIT License
 */
#ifndef OSGEARTH_TRITON_CONTEXT_H
#define OSGEARTH_TRITON_CONTEXT_H

#include <Triton.h>
#include <Camera.h> // Triton
#include "Common"
#include "TritonLayer"
#include "TritonAPIWrapper"
#include "TritonCallback"
#include <osg/Referenced>
#include <osg/Light>
#include <osgEarth/Threading>

namespace osgEarth {
    class SpatialReference;
}

namespace osgEarth { namespace Triton
{
    /**
     * Contains all the Triton SDK handles.
     */
    class TritonContext : public osg::Object
    {
    public:
        META_Object(osgEarth, TritonContext);

        TritonContext(const TritonLayer::Options&);

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

        /** Sets the user callback */
        void setCallback(Callback* callback);
        Callback* getCallback() const { return _callback.get(); }

    public: // accessors

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

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

        void initialize(osg::RenderInfo& renderInfo);

        bool intersect(const osg::Vec3d& start, const osg::Vec3d& dir, float& out_height, osg::Vec3f& out_normal) const;

        ::Triton::Environment* getEnvironment() { return _environment; }
        Environment& getEnvironmentWrapper() const { return *_environmentWrapper; }

        ::Triton::Ocean* getOcean() { return _ocean; }
        Ocean& getOceanWrapper() const { return *_oceanWrapper; }

        bool passHeightMapToTriton() const;

        int getHeightMapSize() const;

        const std::string& getMaskLayerName() const;

    public: // osg::Object

        void resizeGLObjectBuffers(unsigned maxSize);

        /** If State is non-zero, this function releases any associated OpenGL objects for
           * the specified graphics context. Otherwise, releases OpenGL objects
           * for all graphics contexts. */
        void releaseGLObjects(osg::State* state) const;

    protected:

        virtual ~TritonContext();

        // hidden ctors (for META_Object)
        TritonContext() { }
        TritonContext(const TritonContext&, const osg::CopyOp&) { }

    private:
        TritonLayer::Options _options;

        bool             _initAttempted;
        bool             _initFailed;
        std::mutex _initMutex;

        osg::ref_ptr<const osgEarth::SpatialReference> _srs;

        mutable ::Triton::ResourceLoader* _resourceLoader;
        mutable ::Triton::Environment*    _environment;
        mutable ::Triton::Ocean*          _ocean;

        Environment* _environmentWrapper;
        Ocean*       _oceanWrapper;

        osg::ref_ptr<Callback> _callback;
    };

} } // namespace osgEarth::Triton

#endif // OSGEARTH_TRITON_CONTEXT_H