/* -*-c++-*- */
/* osgEarth - Geospatial SDK for OpenSceneGraph
 * Copyright 2020 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 OSGEARTH_NOTIFY_H
#define OSGEARTH_NOTIFY_H 1

#include <osgEarth/Export>
#include <osg/Notify>
#include <osg/Timer>
#include <string>
#include <cstring>

namespace osgEarth
{
    /** set the notify level, overriding the default or the value set by
      * the environmental variable OSGNOTIFYLEVEL.
      */
    extern OSGEARTH_EXPORT void setNotifyLevel(osg::NotifySeverity severity);

    /** get the notify level. */
    extern OSGEARTH_EXPORT osg::NotifySeverity getNotifyLevel();

    /** is notification enabled, given the current setNotifyLevel() setting? */
    extern OSGEARTH_EXPORT bool isNotifyEnabled(osg::NotifySeverity severity);

    /** initialize notify level. */
    extern OSGEARTH_EXPORT bool initNotifyLevel();
  
    extern OSGEARTH_EXPORT std::ostream& notify(const osg::NotifySeverity severity);

    inline std::ostream& notify(void) { return osgEarth::notify(osg::INFO); }


#define OE_NOTIFY( X,Y ) if(osgEarth::isNotifyEnabled( X )) osgEarth::notify( X ) << Y

#ifdef HAVE_SPDLOG
#define OE_CRITICAL OE_NOTIFY(osg::ALWAYS,"")
#define OE_FATAL OE_NOTIFY(osg::FATAL,"")
#define OE_WARN OE_NOTIFY(osg::WARN,"")
#define OE_NOTICE OE_NOTIFY(osg::NOTICE,"")
#define OE_INFO OE_NOTIFY(osg::INFO,"")
#define OE_INFO_CONTINUE OE_NOTIFY(osg::INFO, "")
#define OE_DEBUG OE_NOTIFY(osg::DEBUG_INFO,"")
#else
#define OE_CRITICAL OE_NOTIFY(osg::ALWAYS,"[osgEarth]**")
#define OE_FATAL OE_NOTIFY(osg::FATAL,"[osgEarth]* ")
#define OE_WARN OE_NOTIFY(osg::WARN,"[osgEarth]* ")
#define OE_NOTICE OE_NOTIFY(osg::NOTICE,"[osgEarth]  ")
#define OE_INFO OE_NOTIFY(osg::INFO,"[osgEarth]  ")
#define OE_INFO_CONTINUE OE_NOTIFY(osg::INFO, "")
#define OE_DEBUG OE_NOTIFY(osg::DEBUG_INFO,"[osgEarth]  ")
#endif

#define OE_NULL if(false) osgEarth::notify(osg::ALWAYS)

#define OE_START_TIMER(VAR) osg::Timer_t VAR##_oe_timer = osg::Timer::instance()->tick()
#define OE_STOP_TIMER(VAR) osg::Timer::instance()->delta_s( VAR##_oe_timer, osg::Timer::instance()->tick() )
#define OE_GET_TIMER(VAR) osg::Timer::instance()->delta_s( VAR##_oe_timer, osg::Timer::instance()->tick() )

#define OE_DEPRECATED(A, B) OE_WARN << #A << " is deprecated; please use " << #B << std::endl

#if defined(_MSC_VER)
#define OE_FILE (std::strrchr(__FILE__, '\\') ? std::strrchr(__FILE__, '\\') + 1 : __FILE__)
#else
#define OE_FILE (std::strrchr(__FILE__, '/') ? std::strrchr(__FILE__, '/') + 1 : __FILE__)
#endif


#define OE_SOFT_ASSERT(EXPR, ...) if(!(EXPR)) { OE_WARN << "ASSERTION FAILURE (" << __func__ << " @ " << OE_FILE << ":" << __LINE__ << ") " #EXPR " ..." << __VA_ARGS__ "" << std::endl; }
#define OE_SOFT_ASSERT_AND_RETURN(EXPR, RETVAL, ...) if(!(EXPR)) { OE_WARN << "ASSERTION FAILURE (" << __func__ << " @ " << OE_FILE << ":" << __LINE__ << ") " #EXPR " ..." << __VA_ARGS__ "" << std::endl; return RETVAL; }
#define OE_IF_SOFT_ASSERT(EXPR, ...) if(!(EXPR)) { OE_WARN << "ASSERTION FAILURE (" << __func__ << " @ " << OE_FILE << ":" << __LINE__ << ") " #EXPR " ..." << __VA_ARGS__ "" << std::endl; } else
#define OE_HARD_ASSERT(EXPR, ...) if(!(EXPR)) { OE_WARN << "FATAL ASSERTION FAILURE (" << __func__ << " @ " << OE_FILE << ":" << __LINE__ << ") " #EXPR " ..." << __VA_ARGS__ "" << std::endl; abort(); }

extern OSGEARTH_EXPORT void setNotifyHandler(osg::NotifyHandler *handler);

/** Get currrent notification handler. */
extern OSGEARTH_EXPORT osg::NotifyHandler *getNotifyHandler();

}

#endif // OSGEARTH_NOTIFY_H
