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

#include <osgEarth/StringUtils>
#include <osgEarthFeatures/Feature>
#include <osgEarthFeatures/Script>
#include <osgEarthFeatures/ScriptEngine>

#include <v8.h>

//namespace osgEarth { namespace Drivers { namespace JavascriptV8
//{

  using namespace osgEarth::Features;

  class JavascriptEngineV8 : public ScriptEngine
  {
  public:
    JavascriptEngineV8(const ScriptEngineOptions& options =ScriptEngineOptions());
    virtual ~JavascriptEngineV8();

    bool supported(std::string lang) { return osgEarth::toLower(lang).compare("javascript") == 0; }
    bool supported(Script* script) { return script && supported(script->getLanguage()); }

    ScriptResult run(Script* script, osgEarth::Features::Feature const* feature=0L, osgEarth::Features::FilterContext const* context=0L);
    ScriptResult run(const std::string& code, osgEarth::Features::Feature const* feature=0L, osgEarth::Features::FilterContext const* context=0L);

    ScriptResult call(const std::string& function, osgEarth::Features::Feature const* feature=0L, osgEarth::Features::FilterContext const* context=0L);

  protected:
    static void logCallback(const v8::FunctionCallbackInfo<v8::Value>& info);
    //static void constructFeatureCallback(const v8::FunctionCallbackInfo<v8::Value> &info);
    static void constructBoundsCallback(const v8::FunctionCallbackInfo<v8::Value> &info);
    static void constructVec3dCallback(const v8::FunctionCallbackInfo<v8::Value> &info);
    static void constructGeoExtentCallback(const v8::FunctionCallbackInfo<v8::Value> &info);

    static void constructSpatialReferenceCallback(const v8::FunctionCallbackInfo<v8::Value> &info);
    //static void constructSymbologyGeometryCallback(const v8::FunctionCallbackInfo<v8::Value> &info);

    v8::Handle<v8::Context> createGlobalContext();

    /** Compiles and runs javascript in the current context. */
    ScriptResult executeScript(v8::Handle<v8::String> script, bool ignoreUndefinedResult=false);

  protected:
    v8::Persistent<v8::Context> _globalContext;
    v8::Isolate* _isolate;
  private:
    ScriptResult createErrorResult( std::string prefix, const v8::TryCatch& try_catch );

  };

//} } } // namespace osgEarth::Drivers::JavascriptV8

#endif // OSGEARTHDRIVERS_JAVASCRIPT_ENGINE_V8_H
