// Copyright (c) Microsoft Corporation
// All rights reserved. 
//
// Licensed under the Apache License, Version 2.0 (the ""License""); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
// THIS CODE IS PROVIDED ON AN  *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. 
//
// See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.

#define NTDDI_VERSION 0x06010000

#include <v8.h>
#include <string>
#include <node_object_wrap.h>
#include <ppltasks.h>
#include "CollectionsConverter.h"
#include "CollectionsWrap.h"
#include "node-async.h"
#include "NodeRtUtils.h"
#include "OpaqueWrapper.h"
#include "WrapperBase.h"

#using <Windows.WinMD>

// this undefs fixes the issues of compiling Windows.Data.Json, Windows.Storag.FileProperties, and Windows.Stroage.Search
// Some of the node header files brings windows definitions with the same names as some of the WinRT methods
#undef DocumentProperties
#undef GetObject
#undef CreateEvent
#undef FindText
#undef SendMessage

const char* REGISTRATION_TOKEN_MAP_PROPERTY_NAME = "__registrationTokenMap__";

using namespace v8;
using namespace node;
using namespace concurrency;

namespace NodeRT { namespace Windows { namespace Devices { namespace Geolocation { namespace Geofencing { 
  v8::Handle<v8::Value> WrapGeofence(::Windows::Devices::Geolocation::Geofencing::Geofence^ wintRtInstance);
  ::Windows::Devices::Geolocation::Geofencing::Geofence^ UnwrapGeofence(Handle<Value> value);
  
  v8::Handle<v8::Value> WrapGeofenceMonitor(::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^ wintRtInstance);
  ::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^ UnwrapGeofenceMonitor(Handle<Value> value);
  
  v8::Handle<v8::Value> WrapGeofenceStateChangeReport(::Windows::Devices::Geolocation::Geofencing::GeofenceStateChangeReport^ wintRtInstance);
  ::Windows::Devices::Geolocation::Geofencing::GeofenceStateChangeReport^ UnwrapGeofenceStateChangeReport(Handle<Value> value);
  


  static v8::Handle<v8::Value> InitMonitoredGeofenceStatesEnum(const Handle<Object> exports)
  {
    HandleScope scope;
    
    Handle<Object> enumObject = Object::New();
    exports->Set(String::NewSymbol("MonitoredGeofenceStates"), enumObject);

    enumObject->Set(String::NewSymbol("none"), Integer::New(0));
    enumObject->Set(String::NewSymbol("entered"), Integer::New(1));
    enumObject->Set(String::NewSymbol("exited"), Integer::New(2));
    enumObject->Set(String::NewSymbol("removed"), Integer::New(3));

    return scope.Close(Undefined());
  }


  static v8::Handle<v8::Value> InitGeofenceStateEnum(const Handle<Object> exports)
  {
    HandleScope scope;
    
    Handle<Object> enumObject = Object::New();
    exports->Set(String::NewSymbol("GeofenceState"), enumObject);

    enumObject->Set(String::NewSymbol("none"), Integer::New(0));
    enumObject->Set(String::NewSymbol("entered"), Integer::New(1));
    enumObject->Set(String::NewSymbol("exited"), Integer::New(2));
    enumObject->Set(String::NewSymbol("removed"), Integer::New(3));

    return scope.Close(Undefined());
  }


  static v8::Handle<v8::Value> InitGeofenceMonitorStatusEnum(const Handle<Object> exports)
  {
    HandleScope scope;
    
    Handle<Object> enumObject = Object::New();
    exports->Set(String::NewSymbol("GeofenceMonitorStatus"), enumObject);

    enumObject->Set(String::NewSymbol("ready"), Integer::New(0));
    enumObject->Set(String::NewSymbol("initializing"), Integer::New(1));
    enumObject->Set(String::NewSymbol("noData"), Integer::New(2));
    enumObject->Set(String::NewSymbol("disabled"), Integer::New(3));
    enumObject->Set(String::NewSymbol("notInitialized"), Integer::New(4));
    enumObject->Set(String::NewSymbol("notAvailable"), Integer::New(5));

    return scope.Close(Undefined());
  }


  static v8::Handle<v8::Value> InitGeofenceRemovalReasonEnum(const Handle<Object> exports)
  {
    HandleScope scope;
    
    Handle<Object> enumObject = Object::New();
    exports->Set(String::NewSymbol("GeofenceRemovalReason"), enumObject);

    enumObject->Set(String::NewSymbol("used"), Integer::New(0));
    enumObject->Set(String::NewSymbol("expired"), Integer::New(1));

    return scope.Close(Undefined());
  }



  
  class Geofence : public WrapperBase
  {
  public:    
    static v8::Handle<v8::Value> Init(const Handle<Object> exports)
    {
      HandleScope scope;
      
      s_constructorTemplate = Persistent<FunctionTemplate>::New(FunctionTemplate::New(New));
      s_constructorTemplate->SetClassName(String::NewSymbol("Geofence"));
      s_constructorTemplate->InstanceTemplate()->SetInternalFieldCount(1);
      
                              
      s_constructorTemplate->PrototypeTemplate()->SetAccessor(String::NewSymbol("duration"), DurationGetter);
      s_constructorTemplate->PrototypeTemplate()->SetAccessor(String::NewSymbol("dwellTime"), DwellTimeGetter);
      s_constructorTemplate->PrototypeTemplate()->SetAccessor(String::NewSymbol("geoshape"), GeoshapeGetter);
      s_constructorTemplate->PrototypeTemplate()->SetAccessor(String::NewSymbol("id"), IdGetter);
      s_constructorTemplate->PrototypeTemplate()->SetAccessor(String::NewSymbol("monitoredStates"), MonitoredStatesGetter);
      s_constructorTemplate->PrototypeTemplate()->SetAccessor(String::NewSymbol("singleUse"), SingleUseGetter);
      s_constructorTemplate->PrototypeTemplate()->SetAccessor(String::NewSymbol("startTime"), StartTimeGetter);
      
      Local<Function> constructor = s_constructorTemplate->GetFunction();


      exports->Set(String::NewSymbol("Geofence"), constructor);
      return scope.Close(Undefined());
    }


    virtual ::Platform::Object^ GetObjectInstance() const override
    {
      return _instance;
    }

  private:
    
    Geofence(::Windows::Devices::Geolocation::Geofencing::Geofence^ instance)
    {
      _instance = instance;
    }
    
    
    static v8::Handle<v8::Value> New(const v8::Arguments& args)
    {
      HandleScope scope;

      // in case the constructor was called without the new operator
      if (!s_constructorTemplate->HasInstance(args.This()))
      {
        if (args.Length() > 0)
        {
          std::unique_ptr<Handle<Value> []> constructorArgs(new Handle<Value>[args.Length()]);

          Handle<Value> *argsPtr = constructorArgs.get();
          for (int i = 0; i < args.Length(); i++)
          {
            argsPtr[i] = args[i];
          }

          return s_constructorTemplate->GetFunction()->CallAsConstructor(args.Length(), constructorArgs.get());
        }
        else
        {
          return s_constructorTemplate->GetFunction()->CallAsConstructor(args.Length(), nullptr);
        }
      }
      
      ::Windows::Devices::Geolocation::Geofencing::Geofence^ winRtInstance;


      if (args.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(args[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::Geofence^>(args[0]))
      {
        try 
        {
          winRtInstance = (::Windows::Devices::Geolocation::Geofencing::Geofence^) NodeRT::Utils::GetObjectInstance(args[0]);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return scope.Close(Undefined());
        }
      }
      else if (args.Length() == 2
        && args[0]->IsString()
        && NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::IGeoshape^>(args[1]))
      {
        try
        {
          Platform::String^ arg0 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(args[0])));
          ::Windows::Devices::Geolocation::IGeoshape^ arg1 = dynamic_cast<::Windows::Devices::Geolocation::IGeoshape^>(NodeRT::Utils::GetObjectInstance(args[1]));
          
          winRtInstance = ref new ::Windows::Devices::Geolocation::Geofencing::Geofence(arg0,arg1);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return scope.Close(Undefined());
        }
      }
      else if (args.Length() == 4
        && args[0]->IsString()
        && NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::IGeoshape^>(args[1])
        && args[2]->IsInt32()
        && args[3]->IsBoolean())
      {
        try
        {
          Platform::String^ arg0 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(args[0])));
          ::Windows::Devices::Geolocation::IGeoshape^ arg1 = dynamic_cast<::Windows::Devices::Geolocation::IGeoshape^>(NodeRT::Utils::GetObjectInstance(args[1]));
          ::Windows::Devices::Geolocation::Geofencing::MonitoredGeofenceStates arg2 = static_cast<::Windows::Devices::Geolocation::Geofencing::MonitoredGeofenceStates>(args[2]->Int32Value());
          bool arg3 = args[3]->BooleanValue();
          
          winRtInstance = ref new ::Windows::Devices::Geolocation::Geofencing::Geofence(arg0,arg1,arg2,arg3);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return scope.Close(Undefined());
        }
      }
      else if (args.Length() == 5
        && args[0]->IsString()
        && NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::IGeoshape^>(args[1])
        && args[2]->IsInt32()
        && args[3]->IsBoolean()
        && args[4]->IsNumber())
      {
        try
        {
          Platform::String^ arg0 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(args[0])));
          ::Windows::Devices::Geolocation::IGeoshape^ arg1 = dynamic_cast<::Windows::Devices::Geolocation::IGeoshape^>(NodeRT::Utils::GetObjectInstance(args[1]));
          ::Windows::Devices::Geolocation::Geofencing::MonitoredGeofenceStates arg2 = static_cast<::Windows::Devices::Geolocation::Geofencing::MonitoredGeofenceStates>(args[2]->Int32Value());
          bool arg3 = args[3]->BooleanValue();
          ::Windows::Foundation::TimeSpan arg4 = NodeRT::Utils::TimeSpanFromMilli(args[4]->IntegerValue());
          
          winRtInstance = ref new ::Windows::Devices::Geolocation::Geofencing::Geofence(arg0,arg1,arg2,arg3,arg4);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return scope.Close(Undefined());
        }
      }
      else if (args.Length() == 7
        && args[0]->IsString()
        && NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::IGeoshape^>(args[1])
        && args[2]->IsInt32()
        && args[3]->IsBoolean()
        && args[4]->IsNumber()
        && args[5]->IsDate()
        && args[6]->IsNumber())
      {
        try
        {
          Platform::String^ arg0 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(args[0])));
          ::Windows::Devices::Geolocation::IGeoshape^ arg1 = dynamic_cast<::Windows::Devices::Geolocation::IGeoshape^>(NodeRT::Utils::GetObjectInstance(args[1]));
          ::Windows::Devices::Geolocation::Geofencing::MonitoredGeofenceStates arg2 = static_cast<::Windows::Devices::Geolocation::Geofencing::MonitoredGeofenceStates>(args[2]->Int32Value());
          bool arg3 = args[3]->BooleanValue();
          ::Windows::Foundation::TimeSpan arg4 = NodeRT::Utils::TimeSpanFromMilli(args[4]->IntegerValue());
          ::Windows::Foundation::DateTime arg5 = NodeRT::Utils::DateTimeFromJSDate(args[5]);
          ::Windows::Foundation::TimeSpan arg6 = NodeRT::Utils::TimeSpanFromMilli(args[6]->IntegerValue());
          
          winRtInstance = ref new ::Windows::Devices::Geolocation::Geofencing::Geofence(arg0,arg1,arg2,arg3,arg4,arg5,arg6);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return scope.Close(Undefined());
        }
      }
      else
      {
        ThrowException(Exception::Error(NodeRT::Utils::NewString(L"Invalid arguments, no suitable constructor found")));
        return scope.Close(Undefined());
      }

      args.This()->SetHiddenValue(String::NewSymbol("__winRtInstance__"), True());

      Geofence *wrapperInstance = new Geofence(winRtInstance);
      wrapperInstance->Wrap(args.This());

      return args.This();
    }


  



    static Handle<Value> DurationGetter(Local<String> property, const AccessorInfo &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::Geofence^>(info.This()))
      {
        return scope.Close(Undefined());
      }

      Geofence *wrapper = Geofence::Unwrap<Geofence>(info.This());

      try 
      {
        ::Windows::Foundation::TimeSpan result = wrapper->_instance->Duration;
        return scope.Close(Number::New(result.Duration/10000.0));
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return scope.Close(Undefined());
      }
    }
    
    static Handle<Value> DwellTimeGetter(Local<String> property, const AccessorInfo &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::Geofence^>(info.This()))
      {
        return scope.Close(Undefined());
      }

      Geofence *wrapper = Geofence::Unwrap<Geofence>(info.This());

      try 
      {
        ::Windows::Foundation::TimeSpan result = wrapper->_instance->DwellTime;
        return scope.Close(Number::New(result.Duration/10000.0));
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return scope.Close(Undefined());
      }
    }
    
    static Handle<Value> GeoshapeGetter(Local<String> property, const AccessorInfo &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::Geofence^>(info.This()))
      {
        return scope.Close(Undefined());
      }

      Geofence *wrapper = Geofence::Unwrap<Geofence>(info.This());

      try 
      {
        ::Windows::Devices::Geolocation::IGeoshape^ result = wrapper->_instance->Geoshape;
        return scope.Close(NodeRT::Utils::CreateExternalWinRTObject("Windows.Devices.Geolocation", "IGeoshape", result));
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return scope.Close(Undefined());
      }
    }
    
    static Handle<Value> IdGetter(Local<String> property, const AccessorInfo &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::Geofence^>(info.This()))
      {
        return scope.Close(Undefined());
      }

      Geofence *wrapper = Geofence::Unwrap<Geofence>(info.This());

      try 
      {
        Platform::String^ result = wrapper->_instance->Id;
        return scope.Close(NodeRT::Utils::NewString(result->Data()));
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return scope.Close(Undefined());
      }
    }
    
    static Handle<Value> MonitoredStatesGetter(Local<String> property, const AccessorInfo &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::Geofence^>(info.This()))
      {
        return scope.Close(Undefined());
      }

      Geofence *wrapper = Geofence::Unwrap<Geofence>(info.This());

      try 
      {
        ::Windows::Devices::Geolocation::Geofencing::MonitoredGeofenceStates result = wrapper->_instance->MonitoredStates;
        return scope.Close(Integer::New(static_cast<int>(result)));
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return scope.Close(Undefined());
      }
    }
    
    static Handle<Value> SingleUseGetter(Local<String> property, const AccessorInfo &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::Geofence^>(info.This()))
      {
        return scope.Close(Undefined());
      }

      Geofence *wrapper = Geofence::Unwrap<Geofence>(info.This());

      try 
      {
        bool result = wrapper->_instance->SingleUse;
        return scope.Close(Boolean::New(result));
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return scope.Close(Undefined());
      }
    }
    
    static Handle<Value> StartTimeGetter(Local<String> property, const AccessorInfo &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::Geofence^>(info.This()))
      {
        return scope.Close(Undefined());
      }

      Geofence *wrapper = Geofence::Unwrap<Geofence>(info.This());

      try 
      {
        ::Windows::Foundation::DateTime result = wrapper->_instance->StartTime;
        return scope.Close(NodeRT::Utils::DateTimeToJS(result));
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return scope.Close(Undefined());
      }
    }
    


  private:
    ::Windows::Devices::Geolocation::Geofencing::Geofence^ _instance;
    static Persistent<FunctionTemplate> s_constructorTemplate;

    friend v8::Handle<v8::Value> WrapGeofence(::Windows::Devices::Geolocation::Geofencing::Geofence^ wintRtInstance);
    friend ::Windows::Devices::Geolocation::Geofencing::Geofence^ UnwrapGeofence(Handle<Value> value);
    friend bool IsGeofenceWrapper(Handle<Value> value);
  };
  Persistent<FunctionTemplate> Geofence::s_constructorTemplate;

  v8::Handle<v8::Value> WrapGeofence(::Windows::Devices::Geolocation::Geofencing::Geofence^ winRtInstance)
  {
    HandleScope scope;

    if (winRtInstance == nullptr)
    {
      return scope.Close(Undefined());
    }

    Handle<Object> opaqueWrapper = CreateOpaqueWrapper(winRtInstance);
    Handle<Value> args[] = {opaqueWrapper};
    return scope.Close(Geofence::s_constructorTemplate->GetFunction()->NewInstance(_countof(args), args));
  }

  ::Windows::Devices::Geolocation::Geofencing::Geofence^ UnwrapGeofence(Handle<Value> value)
  {
     return Geofence::Unwrap<Geofence>(value.As<Object>())->_instance;
  }

  void InitGeofence(Handle<Object> exports)
  {
    Geofence::Init(exports);
  }

  class GeofenceMonitor : public WrapperBase
  {
  public:    
    static v8::Handle<v8::Value> Init(const Handle<Object> exports)
    {
      HandleScope scope;
      
      s_constructorTemplate = Persistent<FunctionTemplate>::New(FunctionTemplate::New(New));
      s_constructorTemplate->SetClassName(String::NewSymbol("GeofenceMonitor"));
      s_constructorTemplate->InstanceTemplate()->SetInternalFieldCount(1);
      
            
      s_constructorTemplate->PrototypeTemplate()->Set(String::NewSymbol("readReports"), FunctionTemplate::New(ReadReports)->GetFunction());
      
                  
      Local<Function> addListenerFunc = FunctionTemplate::New(AddListener)->GetFunction();
      s_constructorTemplate->PrototypeTemplate()->Set(String::NewSymbol("addListener"), addListenerFunc);
      s_constructorTemplate->PrototypeTemplate()->Set(String::NewSymbol("on"), addListenerFunc);
      Local<Function> removeListenerFunc = FunctionTemplate::New(RemoveListener)->GetFunction();
      s_constructorTemplate->PrototypeTemplate()->Set(String::NewSymbol("removeListener"), removeListenerFunc);
      s_constructorTemplate->PrototypeTemplate()->Set(String::NewSymbol("off"), removeListenerFunc);
            
      s_constructorTemplate->PrototypeTemplate()->SetAccessor(String::NewSymbol("geofences"), GeofencesGetter);
      s_constructorTemplate->PrototypeTemplate()->SetAccessor(String::NewSymbol("lastKnownGeoposition"), LastKnownGeopositionGetter);
      s_constructorTemplate->PrototypeTemplate()->SetAccessor(String::NewSymbol("status"), StatusGetter);
      
      Local<Function> constructor = s_constructorTemplate->GetFunction();

      constructor->SetAccessor(String::NewSymbol("current"), CurrentGetter);

      exports->Set(String::NewSymbol("GeofenceMonitor"), constructor);
      return scope.Close(Undefined());
    }


    virtual ::Platform::Object^ GetObjectInstance() const override
    {
      return _instance;
    }

  private:
    
    GeofenceMonitor(::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^ instance)
    {
      _instance = instance;
    }
    
    
    static v8::Handle<v8::Value> New(const v8::Arguments& args)
    {
      HandleScope scope;

      // in case the constructor was called without the new operator
      if (!s_constructorTemplate->HasInstance(args.This()))
      {
        if (args.Length() > 0)
        {
          std::unique_ptr<Handle<Value> []> constructorArgs(new Handle<Value>[args.Length()]);

          Handle<Value> *argsPtr = constructorArgs.get();
          for (int i = 0; i < args.Length(); i++)
          {
            argsPtr[i] = args[i];
          }

          return s_constructorTemplate->GetFunction()->CallAsConstructor(args.Length(), constructorArgs.get());
        }
        else
        {
          return s_constructorTemplate->GetFunction()->CallAsConstructor(args.Length(), nullptr);
        }
      }
      
      ::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^ winRtInstance;


      if (args.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(args[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^>(args[0]))
      {
        try 
        {
          winRtInstance = (::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^) NodeRT::Utils::GetObjectInstance(args[0]);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return scope.Close(Undefined());
        }
      }
      else
      {
        ThrowException(Exception::Error(NodeRT::Utils::NewString(L"Invalid arguments, no suitable constructor found")));
        return scope.Close(Undefined());
      }

      args.This()->SetHiddenValue(String::NewSymbol("__winRtInstance__"), True());

      GeofenceMonitor *wrapperInstance = new GeofenceMonitor(winRtInstance);
      wrapperInstance->Wrap(args.This());

      return args.This();
    }


  
    static Handle<Value> ReadReports(const v8::Arguments& args)
    {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^>(args.This()))
      {
        return scope.Close(Undefined());
      }

      GeofenceMonitor *wrapper = GeofenceMonitor::Unwrap<GeofenceMonitor>(args.This());

      if (args.Length() == 0)
      {
        try
        {
          ::Windows::Foundation::Collections::IVectorView<::Windows::Devices::Geolocation::Geofencing::GeofenceStateChangeReport^>^ result;
          result = wrapper->_instance->ReadReports();
          return scope.Close(NodeRT::Collections::VectorViewWrapper<::Windows::Devices::Geolocation::Geofencing::GeofenceStateChangeReport^>::CreateVectorViewWrapper(result, 
            [](::Windows::Devices::Geolocation::Geofencing::GeofenceStateChangeReport^ val) -> Handle<Value> {
              return WrapGeofenceStateChangeReport(val);
            },
            [](Handle<Value> value) -> bool {
              return NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::GeofenceStateChangeReport^>(value);
            },
            [](Handle<Value> value) -> ::Windows::Devices::Geolocation::Geofencing::GeofenceStateChangeReport^ {
              return UnwrapGeofenceStateChangeReport(value);
            }
          ));
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return scope.Close(Undefined());
        }
      }
      else 
      {
        ThrowException(Exception::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return scope.Close(Undefined());
      }

      return scope.Close(Undefined());
    }



    static Handle<Value> GeofencesGetter(Local<String> property, const AccessorInfo &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^>(info.This()))
      {
        return scope.Close(Undefined());
      }

      GeofenceMonitor *wrapper = GeofenceMonitor::Unwrap<GeofenceMonitor>(info.This());

      try 
      {
        ::Windows::Foundation::Collections::IVector<::Windows::Devices::Geolocation::Geofencing::Geofence^>^ result = wrapper->_instance->Geofences;
        return scope.Close(NodeRT::Collections::VectorWrapper<::Windows::Devices::Geolocation::Geofencing::Geofence^>::CreateVectorWrapper(result, 
            [](::Windows::Devices::Geolocation::Geofencing::Geofence^ val) -> Handle<Value> {
              return WrapGeofence(val);
            },
            [](Handle<Value> value) -> bool {
              return NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::Geofence^>(value);
            },
            [](Handle<Value> value) -> ::Windows::Devices::Geolocation::Geofencing::Geofence^ {
              return UnwrapGeofence(value);
            }
          ));
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return scope.Close(Undefined());
      }
    }
    
    static Handle<Value> LastKnownGeopositionGetter(Local<String> property, const AccessorInfo &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^>(info.This()))
      {
        return scope.Close(Undefined());
      }

      GeofenceMonitor *wrapper = GeofenceMonitor::Unwrap<GeofenceMonitor>(info.This());

      try 
      {
        ::Windows::Devices::Geolocation::Geoposition^ result = wrapper->_instance->LastKnownGeoposition;
        return scope.Close(NodeRT::Utils::CreateExternalWinRTObject("Windows.Devices.Geolocation", "Geoposition", result));
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return scope.Close(Undefined());
      }
    }
    
    static Handle<Value> StatusGetter(Local<String> property, const AccessorInfo &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^>(info.This()))
      {
        return scope.Close(Undefined());
      }

      GeofenceMonitor *wrapper = GeofenceMonitor::Unwrap<GeofenceMonitor>(info.This());

      try 
      {
        ::Windows::Devices::Geolocation::Geofencing::GeofenceMonitorStatus result = wrapper->_instance->Status;
        return scope.Close(Integer::New(static_cast<int>(result)));
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return scope.Close(Undefined());
      }
    }
    

    static Handle<Value> CurrentGetter(Local<String> property, const AccessorInfo &info)
    {
      HandleScope scope;

      try 
      {
        ::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^ result = ::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor::Current;
        return scope.Close(WrapGeofenceMonitor(result));
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return scope.Close(Undefined());
      }
    }
    

    static v8::Handle<v8::Value> AddListener(const v8::Arguments& args)
    {
      HandleScope scope;

      if (args.Length() < 2 || !args[0]->IsString() || !args[1]->IsFunction())
      {
        ThrowException(Exception::Error(NodeRT::Utils::NewString(L"wrong arguments, expected arguments are eventName(string),callback(function)")));
        return scope.Close(Undefined());
      }

      String::Value eventName(args[0]);
      auto str = *eventName;
      
      Local<Function> callback = args[1].As<Function>();
      
      ::Windows::Foundation::EventRegistrationToken registrationToken;
      if (NodeRT::Utils::CaseInsenstiveEquals(L"geofenceStateChanged", str))
      {
        if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^>(args.This()))
        {
          ThrowException(Exception::Error(NodeRT::Utils::NewString(L"The caller of this method isn't of the expected type or internal WinRt object was disposed")));
          return scope.Close(Undefined());
        }
        GeofenceMonitor *wrapper = GeofenceMonitor::Unwrap<GeofenceMonitor>(args.This());
      
        try
        {
          std::shared_ptr<Persistent<Object>> callbackObjPtr(new Persistent<Object>(Persistent<Object>::New(NodeRT::Utils::CreateCallbackObjectInDomain(callback))), 
            [] (Persistent<Object> *ptr ) {
              NodeUtils::Async::RunOnMain([ptr]() {
                ptr->Dispose();
                delete ptr;
            });
          });

          registrationToken = wrapper->_instance->GeofenceStateChanged::add(
            ref new ::Windows::Foundation::TypedEventHandler<::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^, ::Platform::Object^>(
            [callbackObjPtr](::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^ arg0, ::Platform::Object^ arg1) {
              NodeUtils::Async::RunOnMain([callbackObjPtr , arg0, arg1]() {
                TryCatch tryCatch;
              
                Handle<Value> error;

                Handle<Value> wrappedArg0 = WrapGeofenceMonitor(arg0);
                Handle<Value> wrappedArg1 = CreateOpaqueWrapper(arg1);

                if (tryCatch.HasCaught())
                {
                  error = tryCatch.Exception()->ToObject();
                }
                else 
                {
                  error = Undefined();
                }


                if (wrappedArg0.IsEmpty()) wrappedArg0 = Undefined();
                if (wrappedArg1.IsEmpty()) wrappedArg1 = Undefined();

                Handle<Value> args[] = { wrappedArg0, wrappedArg1 };
                NodeRT::Utils::CallCallbackInDomain(*callbackObjPtr, _countof(args), args);
              });
            })
          );
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return scope.Close(Undefined());
        }

      }
      else if (NodeRT::Utils::CaseInsenstiveEquals(L"statusChanged", str))
      {
        if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^>(args.This()))
        {
          ThrowException(Exception::Error(NodeRT::Utils::NewString(L"The caller of this method isn't of the expected type or internal WinRt object was disposed")));
          return scope.Close(Undefined());
        }
        GeofenceMonitor *wrapper = GeofenceMonitor::Unwrap<GeofenceMonitor>(args.This());
      
        try
        {
          std::shared_ptr<Persistent<Object>> callbackObjPtr(new Persistent<Object>(Persistent<Object>::New(NodeRT::Utils::CreateCallbackObjectInDomain(callback))), 
            [] (Persistent<Object> *ptr ) {
              NodeUtils::Async::RunOnMain([ptr]() {
                ptr->Dispose();
                delete ptr;
            });
          });

          registrationToken = wrapper->_instance->StatusChanged::add(
            ref new ::Windows::Foundation::TypedEventHandler<::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^, ::Platform::Object^>(
            [callbackObjPtr](::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^ arg0, ::Platform::Object^ arg1) {
              NodeUtils::Async::RunOnMain([callbackObjPtr , arg0, arg1]() {
                TryCatch tryCatch;
              
                Handle<Value> error;

                Handle<Value> wrappedArg0 = WrapGeofenceMonitor(arg0);
                Handle<Value> wrappedArg1 = CreateOpaqueWrapper(arg1);

                if (tryCatch.HasCaught())
                {
                  error = tryCatch.Exception()->ToObject();
                }
                else 
                {
                  error = Undefined();
                }


                if (wrappedArg0.IsEmpty()) wrappedArg0 = Undefined();
                if (wrappedArg1.IsEmpty()) wrappedArg1 = Undefined();

                Handle<Value> args[] = { wrappedArg0, wrappedArg1 };
                NodeRT::Utils::CallCallbackInDomain(*callbackObjPtr, _countof(args), args);
              });
            })
          );
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return scope.Close(Undefined());
        }

      }
      else 
      {
        ThrowException(Exception::Error(String::Concat(NodeRT::Utils::NewString(L"given event name isn't supported: "), args[0].As<String>())));
        return scope.Close(Undefined());
      }

      Local<Value> tokenMap = callback->GetHiddenValue(String::NewSymbol(REGISTRATION_TOKEN_MAP_PROPERTY_NAME));
                
      if (tokenMap.IsEmpty() || tokenMap->Equals(Undefined()))
      {
          tokenMap = Object::New();
          callback->SetHiddenValue(String::NewSymbol(REGISTRATION_TOKEN_MAP_PROPERTY_NAME), tokenMap);
      }

      tokenMap.As<Object>()->Set(args[1], CreateOpaqueWrapper(::Windows::Foundation::PropertyValue::CreateInt64(registrationToken.Value)));
                
      return scope.Close(Undefined());
    }

    static v8::Handle<v8::Value> RemoveListener(const v8::Arguments& args)
    {
      HandleScope scope;

      if (args.Length() < 2 || !args[0]->IsString() || !args[1]->IsFunction())
      {
        ThrowException(Exception::Error(NodeRT::Utils::NewString(L"wrong arguments, expected a string and a callback")));
        return scope.Close(Undefined());
      }

      String::Value eventName(args[0]);
      auto str = *eventName;

      if ((NodeRT::Utils::CaseInsenstiveEquals(L"geofenceStateChanged", str)) &&(NodeRT::Utils::CaseInsenstiveEquals(L"statusChanged", str)))
      {
        ThrowException(Exception::Error(String::Concat(NodeRT::Utils::NewString(L"given event name isn't supported: "), args[0].As<String>())));
        return scope.Close(Undefined());
      }

      Local<Function> callback = args[1].As<Function>();
      Handle<Value> tokenMap = callback->GetHiddenValue(String::NewSymbol(REGISTRATION_TOKEN_MAP_PROPERTY_NAME));
                
      if (tokenMap.IsEmpty() || tokenMap->Equals(Undefined()))
      {
        return scope.Close(Undefined());
      }

      Handle<Value> opaqueWrapperObj =  tokenMap.As<Object>()->Get(args[1]);

      if (opaqueWrapperObj.IsEmpty() || opaqueWrapperObj->Equals(Undefined()))
      {
        return scope.Close(Undefined());
      }

      OpaqueWrapper *opaqueWrapper = OpaqueWrapper::Unwrap<OpaqueWrapper>(opaqueWrapperObj.As<Object>());
            
      long long tokenValue = (long long) opaqueWrapper->GetObjectInstance();
      ::Windows::Foundation::EventRegistrationToken registrationToken;
      registrationToken.Value = tokenValue;
        
      try 
      {
        if (NodeRT::Utils::CaseInsenstiveEquals(L"geofenceStateChanged", str))
        {
          if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^>(args.This()))
          {
            ThrowException(Exception::Error(NodeRT::Utils::NewString(L"The caller of this method isn't of the expected type or internal WinRt object was disposed")));
            return scope.Close(Undefined());
          }
          GeofenceMonitor *wrapper = GeofenceMonitor::Unwrap<GeofenceMonitor>(args.This());
          wrapper->_instance->GeofenceStateChanged::remove(registrationToken);
        }
        else if (NodeRT::Utils::CaseInsenstiveEquals(L"statusChanged", str))
        {
          if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^>(args.This()))
          {
            ThrowException(Exception::Error(NodeRT::Utils::NewString(L"The caller of this method isn't of the expected type or internal WinRt object was disposed")));
            return scope.Close(Undefined());
          }
          GeofenceMonitor *wrapper = GeofenceMonitor::Unwrap<GeofenceMonitor>(args.This());
          wrapper->_instance->StatusChanged::remove(registrationToken);
        }
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
      }

      tokenMap.As<Object>()->Delete(args[0].As<String>());

      return scope.Close(Undefined());
    }
  private:
    ::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^ _instance;
    static Persistent<FunctionTemplate> s_constructorTemplate;

    friend v8::Handle<v8::Value> WrapGeofenceMonitor(::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^ wintRtInstance);
    friend ::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^ UnwrapGeofenceMonitor(Handle<Value> value);
    friend bool IsGeofenceMonitorWrapper(Handle<Value> value);
  };
  Persistent<FunctionTemplate> GeofenceMonitor::s_constructorTemplate;

  v8::Handle<v8::Value> WrapGeofenceMonitor(::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^ winRtInstance)
  {
    HandleScope scope;

    if (winRtInstance == nullptr)
    {
      return scope.Close(Undefined());
    }

    Handle<Object> opaqueWrapper = CreateOpaqueWrapper(winRtInstance);
    Handle<Value> args[] = {opaqueWrapper};
    return scope.Close(GeofenceMonitor::s_constructorTemplate->GetFunction()->NewInstance(_countof(args), args));
  }

  ::Windows::Devices::Geolocation::Geofencing::GeofenceMonitor^ UnwrapGeofenceMonitor(Handle<Value> value)
  {
     return GeofenceMonitor::Unwrap<GeofenceMonitor>(value.As<Object>())->_instance;
  }

  void InitGeofenceMonitor(Handle<Object> exports)
  {
    GeofenceMonitor::Init(exports);
  }

  class GeofenceStateChangeReport : public WrapperBase
  {
  public:    
    static v8::Handle<v8::Value> Init(const Handle<Object> exports)
    {
      HandleScope scope;
      
      s_constructorTemplate = Persistent<FunctionTemplate>::New(FunctionTemplate::New(New));
      s_constructorTemplate->SetClassName(String::NewSymbol("GeofenceStateChangeReport"));
      s_constructorTemplate->InstanceTemplate()->SetInternalFieldCount(1);
      
                              
      s_constructorTemplate->PrototypeTemplate()->SetAccessor(String::NewSymbol("geofence"), GeofenceGetter);
      s_constructorTemplate->PrototypeTemplate()->SetAccessor(String::NewSymbol("geoposition"), GeopositionGetter);
      s_constructorTemplate->PrototypeTemplate()->SetAccessor(String::NewSymbol("newState"), NewStateGetter);
      s_constructorTemplate->PrototypeTemplate()->SetAccessor(String::NewSymbol("removalReason"), RemovalReasonGetter);
      
      Local<Function> constructor = s_constructorTemplate->GetFunction();


      exports->Set(String::NewSymbol("GeofenceStateChangeReport"), constructor);
      return scope.Close(Undefined());
    }


    virtual ::Platform::Object^ GetObjectInstance() const override
    {
      return _instance;
    }

  private:
    
    GeofenceStateChangeReport(::Windows::Devices::Geolocation::Geofencing::GeofenceStateChangeReport^ instance)
    {
      _instance = instance;
    }
    
    
    static v8::Handle<v8::Value> New(const v8::Arguments& args)
    {
      HandleScope scope;

      // in case the constructor was called without the new operator
      if (!s_constructorTemplate->HasInstance(args.This()))
      {
        if (args.Length() > 0)
        {
          std::unique_ptr<Handle<Value> []> constructorArgs(new Handle<Value>[args.Length()]);

          Handle<Value> *argsPtr = constructorArgs.get();
          for (int i = 0; i < args.Length(); i++)
          {
            argsPtr[i] = args[i];
          }

          return s_constructorTemplate->GetFunction()->CallAsConstructor(args.Length(), constructorArgs.get());
        }
        else
        {
          return s_constructorTemplate->GetFunction()->CallAsConstructor(args.Length(), nullptr);
        }
      }
      
      ::Windows::Devices::Geolocation::Geofencing::GeofenceStateChangeReport^ winRtInstance;


      if (args.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(args[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::GeofenceStateChangeReport^>(args[0]))
      {
        try 
        {
          winRtInstance = (::Windows::Devices::Geolocation::Geofencing::GeofenceStateChangeReport^) NodeRT::Utils::GetObjectInstance(args[0]);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return scope.Close(Undefined());
        }
      }
      else
      {
        ThrowException(Exception::Error(NodeRT::Utils::NewString(L"Invalid arguments, no suitable constructor found")));
        return scope.Close(Undefined());
      }

      args.This()->SetHiddenValue(String::NewSymbol("__winRtInstance__"), True());

      GeofenceStateChangeReport *wrapperInstance = new GeofenceStateChangeReport(winRtInstance);
      wrapperInstance->Wrap(args.This());

      return args.This();
    }


  



    static Handle<Value> GeofenceGetter(Local<String> property, const AccessorInfo &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::GeofenceStateChangeReport^>(info.This()))
      {
        return scope.Close(Undefined());
      }

      GeofenceStateChangeReport *wrapper = GeofenceStateChangeReport::Unwrap<GeofenceStateChangeReport>(info.This());

      try 
      {
        ::Windows::Devices::Geolocation::Geofencing::Geofence^ result = wrapper->_instance->Geofence;
        return scope.Close(WrapGeofence(result));
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return scope.Close(Undefined());
      }
    }
    
    static Handle<Value> GeopositionGetter(Local<String> property, const AccessorInfo &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::GeofenceStateChangeReport^>(info.This()))
      {
        return scope.Close(Undefined());
      }

      GeofenceStateChangeReport *wrapper = GeofenceStateChangeReport::Unwrap<GeofenceStateChangeReport>(info.This());

      try 
      {
        ::Windows::Devices::Geolocation::Geoposition^ result = wrapper->_instance->Geoposition;
        return scope.Close(NodeRT::Utils::CreateExternalWinRTObject("Windows.Devices.Geolocation", "Geoposition", result));
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return scope.Close(Undefined());
      }
    }
    
    static Handle<Value> NewStateGetter(Local<String> property, const AccessorInfo &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::GeofenceStateChangeReport^>(info.This()))
      {
        return scope.Close(Undefined());
      }

      GeofenceStateChangeReport *wrapper = GeofenceStateChangeReport::Unwrap<GeofenceStateChangeReport>(info.This());

      try 
      {
        ::Windows::Devices::Geolocation::Geofencing::GeofenceState result = wrapper->_instance->NewState;
        return scope.Close(Integer::New(static_cast<int>(result)));
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return scope.Close(Undefined());
      }
    }
    
    static Handle<Value> RemovalReasonGetter(Local<String> property, const AccessorInfo &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Devices::Geolocation::Geofencing::GeofenceStateChangeReport^>(info.This()))
      {
        return scope.Close(Undefined());
      }

      GeofenceStateChangeReport *wrapper = GeofenceStateChangeReport::Unwrap<GeofenceStateChangeReport>(info.This());

      try 
      {
        ::Windows::Devices::Geolocation::Geofencing::GeofenceRemovalReason result = wrapper->_instance->RemovalReason;
        return scope.Close(Integer::New(static_cast<int>(result)));
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return scope.Close(Undefined());
      }
    }
    


  private:
    ::Windows::Devices::Geolocation::Geofencing::GeofenceStateChangeReport^ _instance;
    static Persistent<FunctionTemplate> s_constructorTemplate;

    friend v8::Handle<v8::Value> WrapGeofenceStateChangeReport(::Windows::Devices::Geolocation::Geofencing::GeofenceStateChangeReport^ wintRtInstance);
    friend ::Windows::Devices::Geolocation::Geofencing::GeofenceStateChangeReport^ UnwrapGeofenceStateChangeReport(Handle<Value> value);
    friend bool IsGeofenceStateChangeReportWrapper(Handle<Value> value);
  };
  Persistent<FunctionTemplate> GeofenceStateChangeReport::s_constructorTemplate;

  v8::Handle<v8::Value> WrapGeofenceStateChangeReport(::Windows::Devices::Geolocation::Geofencing::GeofenceStateChangeReport^ winRtInstance)
  {
    HandleScope scope;

    if (winRtInstance == nullptr)
    {
      return scope.Close(Undefined());
    }

    Handle<Object> opaqueWrapper = CreateOpaqueWrapper(winRtInstance);
    Handle<Value> args[] = {opaqueWrapper};
    return scope.Close(GeofenceStateChangeReport::s_constructorTemplate->GetFunction()->NewInstance(_countof(args), args));
  }

  ::Windows::Devices::Geolocation::Geofencing::GeofenceStateChangeReport^ UnwrapGeofenceStateChangeReport(Handle<Value> value)
  {
     return GeofenceStateChangeReport::Unwrap<GeofenceStateChangeReport>(value.As<Object>())->_instance;
  }

  void InitGeofenceStateChangeReport(Handle<Object> exports)
  {
    GeofenceStateChangeReport::Init(exports);
  }

} } } } } 

void init(Handle<Object> exports)
{
  if (FAILED(CoInitializeEx(nullptr, COINIT_MULTITHREADED)))
  {
    ThrowException(v8::Exception::Error(NodeRT::Utils::NewString(L"error in CoInitializeEx()")));
    return;
  }
  
  NodeRT::Windows::Devices::Geolocation::Geofencing::InitMonitoredGeofenceStatesEnum(exports);
  NodeRT::Windows::Devices::Geolocation::Geofencing::InitGeofenceStateEnum(exports);
  NodeRT::Windows::Devices::Geolocation::Geofencing::InitGeofenceMonitorStatusEnum(exports);
  NodeRT::Windows::Devices::Geolocation::Geofencing::InitGeofenceRemovalReasonEnum(exports);
  NodeRT::Windows::Devices::Geolocation::Geofencing::InitGeofence(exports);
  NodeRT::Windows::Devices::Geolocation::Geofencing::InitGeofenceMonitor(exports);
  NodeRT::Windows::Devices::Geolocation::Geofencing::InitGeofenceStateChangeReport(exports);

  NodeRT::Utils::RegisterNameSpace("Windows.Devices.Geolocation.Geofencing", exports);
}


NODE_MODULE(binding, init)