/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
* Copyright 2008-2010 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 OSGEARTHUTIL_SKY_NODE
#define OSGEARTHUTIL_SKY_NODE

#include <osgEarthUtil/Common>
#include <osgEarth/Map>
#include <osg/MatrixTransform>
#include <osg/Uniform>
#include <osg/Group>
#include <osg/View>

namespace osgEarth { namespace Util 
{
    using namespace osgEarth;

    /**
     * A sky model.
     */
    class OSGEARTHUTIL_EXPORT SkyNode : public osg::Group
    {
    public:
        /** Creates a new sky node based on the provided map. */        
        SkyNode( Map* map, const std::string& starFile="" );

        /** Attached this sky node to a view (placing a sky light). */
        void attach( osg::View* view, int lightNum =0 );

        /** Sets the sun's position as a unit vector. */
        void setSunPosition( const osg::Vec3& pos, osg::View* view =0L );

        /** Sets the sun's position as a latitude and longitude. */         
        void setSunPosition( double lat_degrees, double lon_degrees, osg::View* view =0L );
        
        /** Sets the sun's position based on a julian date. */
        void setDateTime( int year, int month, int date, double hoursUTC, osg::View* view =0L );

        /** The minimum brightness for non-sunlit areas. */
        void setAmbientBrightness( float value, osg::View* view =0L );
        float getAmbientBrightness( osg::View* view =0L ) const;

        /** Whether the stars are visible */
        void setStarsVisible( bool value, osg::View* view =0L );
        bool getStarsVisible( osg::View* view =0L ) const;

    public:
        //override
        virtual void traverse( osg::NodeVisitor& nv );

        //override
        virtual osg::BoundingSphere computeBound() const;

    private:
        struct StarData
        {
            std::string name;
            double right_ascension;
            double declination;
            double magnitude;
            
            StarData() { }
            StarData( std::stringstream &ss );
        };

        struct PerViewData
        {
            osg::Vec3f                         _lightPos;
            osg::ref_ptr<osg::Light>           _light;
            osg::ref_ptr<osg::Uniform>         _lightPosUniform;
            osg::Matrixd                       _sunMatrix;            
            osg::Matrixd                       _starsMatrix;
            bool                               _starsVisible;

            // only available in per-view structures..not default
            osg::ref_ptr<osg::Group>           _cullContainer;
            osg::ref_ptr<osg::MatrixTransform> _sunXform;
            osg::ref_ptr<osg::MatrixTransform> _starsXform;
        };

        PerViewData _defaultPerViewData;
        typedef std::map<osg::View*, PerViewData> PerViewDataMap;
        PerViewDataMap _perViewData;

        float _innerRadius, _outerRadius, _sunDistance, _starRadius;
        osg::ref_ptr<osg::Node> _sun, _stars, _atmosphere;
        osg::ref_ptr<osg::Uniform> _starAlpha;
        osg::ref_ptr<osg::Uniform> _starPointSize;

        osg::ref_ptr< const osg::EllipsoidModel > _ellipsoidModel;

        void makeAtmosphere( const osg::EllipsoidModel* );
        void makeSun();

        void makeStars(const std::string& starFile);
        osg::Geode* buildStarGeometry(const std::vector<StarData>& stars);
        void getDefaultStars(std::vector<StarData>& out_stars);
        bool parseStarFile(const std::string& starFile, std::vector<StarData>& out_stars);

        void setAmbientBrightness( PerViewData& data, float value );
        void setSunPosition( PerViewData& data, const osg::Vec3& pos );
    };

} } // namespace osgEarth::Util

#endif //OSGEARTHUTIL_SKY_NODE
