/* osgEarth
 * Copyright 2025 Pelican Mapping
 * MIT License
 */

#include "Common"
#include "SilverLiningOptions"
#include "SilverLiningCallback"
#include "SilverLiningAPIWrapper"
#include <osgEarth/Sky>
#include <osgEarth/PhongLightingEffect>
#include <osg/Light>
#include <osg/LightSource>

namespace osgEarth { namespace SilverLining
{
    class SilverLiningContext;
	class SilverLiningContextNode;

    /**
     * Node that roots the silverlining adapter.
     */
    class OSGEARTHSILVERLINING_EXPORT SilverLiningNode : public osgEarth::SkyNode
    {
    public:
        SilverLiningNode(
            const SilverLiningOptions& options,
            Callback*                  callback =0L);

        //! Access the clouds stateset for context "index" so you can modify its render bin details, etc.
        osg::StateSet* getCloudsStateSet(unsigned contextIndex) const;

        //! Access the sky stateset for context "index"
        osg::StateSet* getSkyStateSet(unsigned contextIndex) const;

        //! Number of contexts (each context is tied to a camera/viewport)
        unsigned getNumContexts() const;

    public: // SkyNode

        //! The (sun) light that this node controls
        osg::Light* getSunLight() const override { 
            return _light.get();
        }

        //! Attach to a view so that this node controls its light.
        void attach(osg::View* view, int lightNum) override;

    protected: // SkyNode

        // callbacks from base class.
        void onSetDateTime() override;

    public: // osg::Node

        void traverse(osg::NodeVisitor&);

    protected:
        virtual ~SilverLiningNode();

        osg::ref_ptr<osg::LightSource> _lightSource;
        osg::ref_ptr<osg::Light> _light;
        const SilverLiningOptions _options;
        osg::ref_ptr<osgEarth::Util::PhongLightingEffect> _lighting;
        osg::ref_ptr<Callback> _callback;

        typedef std::map<osg::Camera*, osg::ref_ptr<osg::Node> > CameraContextMap;
        CameraContextMap _contexts;
        osg::NodeList _contextList;

        typedef std::set<osg::ref_ptr<osg::Camera> > CameraSet;
        CameraSet _camerasToAdd;

        //! post-ctor construction
        void construct();
    };

} } // namespace osgEarth::SilverLining
