// 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.

// TODO: Verify that this is is still needed..
#define NTDDI_VERSION 0x06010000

#include <v8.h>
#include "nan.h"
#include <string>
#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 v8::Array;
using v8::String;
using v8::Handle;
using v8::Value;
using v8::Boolean;
using v8::Integer;
using v8::FunctionTemplate;
using v8::Object;
using v8::Local;
using v8::Function;
using v8::Date;
using v8::Number;
using v8::PropertyAttribute;
using v8::Primitive;
using Nan::HandleScope;
using Nan::Persistent;
using Nan::Undefined;
using Nan::True;
using Nan::False;
using Nan::Null;
using Nan::MaybeLocal;
using Nan::EscapableHandleScope;
using Nan::HandleScope;
using Nan::TryCatch;
using namespace concurrency;

namespace NodeRT { namespace Windows { namespace ApplicationModel { namespace Appointments { namespace AppointmentsProvider { 

  v8::Local<v8::Value> WrapAppointmentsProviderLaunchActionVerbs(::Windows::ApplicationModel::Appointments::AppointmentsProvider::AppointmentsProviderLaunchActionVerbs^ wintRtInstance);
  ::Windows::ApplicationModel::Appointments::AppointmentsProvider::AppointmentsProviderLaunchActionVerbs^ UnwrapAppointmentsProviderLaunchActionVerbs(Local<Value> value);
  
  v8::Local<v8::Value> WrapAddAppointmentOperation(::Windows::ApplicationModel::Appointments::AppointmentsProvider::AddAppointmentOperation^ wintRtInstance);
  ::Windows::ApplicationModel::Appointments::AppointmentsProvider::AddAppointmentOperation^ UnwrapAddAppointmentOperation(Local<Value> value);
  
  v8::Local<v8::Value> WrapReplaceAppointmentOperation(::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^ wintRtInstance);
  ::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^ UnwrapReplaceAppointmentOperation(Local<Value> value);
  
  v8::Local<v8::Value> WrapRemoveAppointmentOperation(::Windows::ApplicationModel::Appointments::AppointmentsProvider::RemoveAppointmentOperation^ wintRtInstance);
  ::Windows::ApplicationModel::Appointments::AppointmentsProvider::RemoveAppointmentOperation^ UnwrapRemoveAppointmentOperation(Local<Value> value);
  



  
  class AppointmentsProviderLaunchActionVerbs : public WrapperBase
  {
  public:    
    static void Init(const Local<Object> exports)
    {
      HandleScope scope;
      
      Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(New);
      s_constructorTemplate.Reset(localRef);
      localRef->SetClassName(Nan::New<String>("AppointmentsProviderLaunchActionVerbs").ToLocalChecked());
      localRef->InstanceTemplate()->SetInternalFieldCount(1);
      
                              
      Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
	  Nan::SetMethod(constructor, "castFrom", CastFrom);

      Nan::SetAccessor(constructor, Nan::New<String>("addAppointment").ToLocalChecked(), AddAppointmentGetter);
      Nan::SetAccessor(constructor, Nan::New<String>("removeAppointment").ToLocalChecked(), RemoveAppointmentGetter);
      Nan::SetAccessor(constructor, Nan::New<String>("replaceAppointment").ToLocalChecked(), ReplaceAppointmentGetter);
      Nan::SetAccessor(constructor, Nan::New<String>("showTimeFrame").ToLocalChecked(), ShowTimeFrameGetter);
      Nan::SetAccessor(constructor, Nan::New<String>("showAppointmentDetails").ToLocalChecked(), ShowAppointmentDetailsGetter);

      Nan::Set(exports, Nan::New<String>("AppointmentsProviderLaunchActionVerbs").ToLocalChecked(), constructor);
    }


    virtual ::Platform::Object^ GetObjectInstance() const override
    {
      return _instance;
    }

  private:
    
    AppointmentsProviderLaunchActionVerbs(::Windows::ApplicationModel::Appointments::AppointmentsProvider::AppointmentsProviderLaunchActionVerbs^ instance)
    {
      _instance = instance;
    }
    
    
    static void New(Nan::NAN_METHOD_ARGS_TYPE info)
    {
      HandleScope scope;

	    Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(s_constructorTemplate);

      // in case the constructor was called without the new operator
      if (!localRef->HasInstance(info.This()))
      {
        if (info.Length() > 0)
        {
          std::unique_ptr<Local<Value> []> constructorArgs(new Local<Value>[info.Length()]);

          Local<Value> *argsPtr = constructorArgs.get();
          for (int i = 0; i < info.Length(); i++)
          {
            argsPtr[i] = info[i];
          }

		  MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), constructorArgs.get());
		  if (res.IsEmpty())
		  {
			  return;
		  }
		  info.GetReturnValue().Set(res.ToLocalChecked());
		  return;
		}
		else
		{
          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), nullptr);
          if (res.IsEmpty())
          {
            return;
          }
          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        }
      }
      
      ::Windows::ApplicationModel::Appointments::AppointmentsProvider::AppointmentsProviderLaunchActionVerbs^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::AppointmentsProviderLaunchActionVerbs^>(info[0]))
      {
        try 
        {
          winRtInstance = (::Windows::ApplicationModel::Appointments::AppointmentsProvider::AppointmentsProviderLaunchActionVerbs^) NodeRT::Utils::GetObjectInstance(info[0]);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else
      {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no suitable constructor found")));
	    	return;
      }

      NodeRT::Utils::SetHiddenValue(info.This(), Nan::New<String>("__winRtInstance__").ToLocalChecked(), True());

      AppointmentsProviderLaunchActionVerbs *wrapperInstance = new AppointmentsProviderLaunchActionVerbs(winRtInstance);
      wrapperInstance->Wrap(info.This());

      info.GetReturnValue().Set(info.This());
    }


	
    static void CastFrom(Nan::NAN_METHOD_ARGS_TYPE info)
    {
		HandleScope scope;
		if (info.Length() < 1 || !NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::AppointmentsProviderLaunchActionVerbs^>(info[0]))
		{
			Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no object provided, or given object could not be casted to requested type")));
			return;
		}

		::Windows::ApplicationModel::Appointments::AppointmentsProvider::AppointmentsProviderLaunchActionVerbs^ winRtInstance;
		try
		{
			winRtInstance = (::Windows::ApplicationModel::Appointments::AppointmentsProvider::AppointmentsProviderLaunchActionVerbs^) NodeRT::Utils::GetObjectInstance(info[0]);
		}
		catch (Platform::Exception ^exception)
		{
			NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
			return;
		}

		info.GetReturnValue().Set(WrapAppointmentsProviderLaunchActionVerbs(winRtInstance));
    }


  




    static void AddAppointmentGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;

      try 
      {
        Platform::String^ result = ::Windows::ApplicationModel::Appointments::AppointmentsProvider::AppointmentsProviderLaunchActionVerbs::AddAppointment;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void RemoveAppointmentGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;

      try 
      {
        Platform::String^ result = ::Windows::ApplicationModel::Appointments::AppointmentsProvider::AppointmentsProviderLaunchActionVerbs::RemoveAppointment;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void ReplaceAppointmentGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;

      try 
      {
        Platform::String^ result = ::Windows::ApplicationModel::Appointments::AppointmentsProvider::AppointmentsProviderLaunchActionVerbs::ReplaceAppointment;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void ShowTimeFrameGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;

      try 
      {
        Platform::String^ result = ::Windows::ApplicationModel::Appointments::AppointmentsProvider::AppointmentsProviderLaunchActionVerbs::ShowTimeFrame;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void ShowAppointmentDetailsGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;

      try 
      {
        Platform::String^ result = ::Windows::ApplicationModel::Appointments::AppointmentsProvider::AppointmentsProviderLaunchActionVerbs::ShowAppointmentDetails;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    

  private:
    ::Windows::ApplicationModel::Appointments::AppointmentsProvider::AppointmentsProviderLaunchActionVerbs^ _instance;
    static Persistent<FunctionTemplate> s_constructorTemplate;

    friend v8::Local<v8::Value> WrapAppointmentsProviderLaunchActionVerbs(::Windows::ApplicationModel::Appointments::AppointmentsProvider::AppointmentsProviderLaunchActionVerbs^ wintRtInstance);
    friend ::Windows::ApplicationModel::Appointments::AppointmentsProvider::AppointmentsProviderLaunchActionVerbs^ UnwrapAppointmentsProviderLaunchActionVerbs(Local<Value> value);
  };
  Persistent<FunctionTemplate> AppointmentsProviderLaunchActionVerbs::s_constructorTemplate;

  v8::Local<v8::Value> WrapAppointmentsProviderLaunchActionVerbs(::Windows::ApplicationModel::Appointments::AppointmentsProvider::AppointmentsProviderLaunchActionVerbs^ winRtInstance)
  {
    EscapableHandleScope scope;

    if (winRtInstance == nullptr)
    {
      return scope.Escape(Undefined());
    }

    Local<Value> opaqueWrapper = CreateOpaqueWrapper(winRtInstance);
    Local<Value> args[] = {opaqueWrapper};
    Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(AppointmentsProviderLaunchActionVerbs::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::ApplicationModel::Appointments::AppointmentsProvider::AppointmentsProviderLaunchActionVerbs^ UnwrapAppointmentsProviderLaunchActionVerbs(Local<Value> value)
  {
     return AppointmentsProviderLaunchActionVerbs::Unwrap<AppointmentsProviderLaunchActionVerbs>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitAppointmentsProviderLaunchActionVerbs(Local<Object> exports)
  {
    AppointmentsProviderLaunchActionVerbs::Init(exports);
  }

  class AddAppointmentOperation : public WrapperBase
  {
  public:    
    static void Init(const Local<Object> exports)
    {
      HandleScope scope;
      
      Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(New);
      s_constructorTemplate.Reset(localRef);
      localRef->SetClassName(Nan::New<String>("AddAppointmentOperation").ToLocalChecked());
      localRef->InstanceTemplate()->SetInternalFieldCount(1);
      
            
      Nan::SetPrototypeMethod(localRef, "reportCompleted", ReportCompleted);
      Nan::SetPrototypeMethod(localRef, "reportCanceled", ReportCanceled);
      Nan::SetPrototypeMethod(localRef, "reportError", ReportError);
      Nan::SetPrototypeMethod(localRef, "dismissUI", DismissUI);
      
                        
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("appointmentInformation").ToLocalChecked(), AppointmentInformationGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("sourcePackageFamilyName").ToLocalChecked(), SourcePackageFamilyNameGetter);
      
      Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
	  Nan::SetMethod(constructor, "castFrom", CastFrom);


      Nan::Set(exports, Nan::New<String>("AddAppointmentOperation").ToLocalChecked(), constructor);
    }


    virtual ::Platform::Object^ GetObjectInstance() const override
    {
      return _instance;
    }

  private:
    
    AddAppointmentOperation(::Windows::ApplicationModel::Appointments::AppointmentsProvider::AddAppointmentOperation^ instance)
    {
      _instance = instance;
    }
    
    
    static void New(Nan::NAN_METHOD_ARGS_TYPE info)
    {
      HandleScope scope;

	    Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(s_constructorTemplate);

      // in case the constructor was called without the new operator
      if (!localRef->HasInstance(info.This()))
      {
        if (info.Length() > 0)
        {
          std::unique_ptr<Local<Value> []> constructorArgs(new Local<Value>[info.Length()]);

          Local<Value> *argsPtr = constructorArgs.get();
          for (int i = 0; i < info.Length(); i++)
          {
            argsPtr[i] = info[i];
          }

		  MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), constructorArgs.get());
		  if (res.IsEmpty())
		  {
			  return;
		  }
		  info.GetReturnValue().Set(res.ToLocalChecked());
		  return;
		}
		else
		{
          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), nullptr);
          if (res.IsEmpty())
          {
            return;
          }
          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        }
      }
      
      ::Windows::ApplicationModel::Appointments::AppointmentsProvider::AddAppointmentOperation^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::AddAppointmentOperation^>(info[0]))
      {
        try 
        {
          winRtInstance = (::Windows::ApplicationModel::Appointments::AppointmentsProvider::AddAppointmentOperation^) NodeRT::Utils::GetObjectInstance(info[0]);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else
      {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no suitable constructor found")));
	    	return;
      }

      NodeRT::Utils::SetHiddenValue(info.This(), Nan::New<String>("__winRtInstance__").ToLocalChecked(), True());

      AddAppointmentOperation *wrapperInstance = new AddAppointmentOperation(winRtInstance);
      wrapperInstance->Wrap(info.This());

      info.GetReturnValue().Set(info.This());
    }


	
    static void CastFrom(Nan::NAN_METHOD_ARGS_TYPE info)
    {
		HandleScope scope;
		if (info.Length() < 1 || !NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::AddAppointmentOperation^>(info[0]))
		{
			Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no object provided, or given object could not be casted to requested type")));
			return;
		}

		::Windows::ApplicationModel::Appointments::AppointmentsProvider::AddAppointmentOperation^ winRtInstance;
		try
		{
			winRtInstance = (::Windows::ApplicationModel::Appointments::AppointmentsProvider::AddAppointmentOperation^) NodeRT::Utils::GetObjectInstance(info[0]);
		}
		catch (Platform::Exception ^exception)
		{
			NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
			return;
		}

		info.GetReturnValue().Set(WrapAddAppointmentOperation(winRtInstance));
    }


  
    static void ReportCompleted(Nan::NAN_METHOD_ARGS_TYPE info)
    {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::AddAppointmentOperation^>(info.This()))
      {
        return;
      }

      AddAppointmentOperation *wrapper = AddAppointmentOperation::Unwrap<AddAppointmentOperation>(info.This());

      if (info.Length() == 1
        && info[0]->IsString())
      {
        try
        {
          Platform::String^ arg0 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(info[0])));
          
          wrapper->_instance->ReportCompleted(arg0);
          return;   
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else 
      {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }
    }
    static void ReportCanceled(Nan::NAN_METHOD_ARGS_TYPE info)
    {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::AddAppointmentOperation^>(info.This()))
      {
        return;
      }

      AddAppointmentOperation *wrapper = AddAppointmentOperation::Unwrap<AddAppointmentOperation>(info.This());

      if (info.Length() == 0)
      {
        try
        {
          wrapper->_instance->ReportCanceled();
          return;   
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else 
      {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }
    }
    static void ReportError(Nan::NAN_METHOD_ARGS_TYPE info)
    {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::AddAppointmentOperation^>(info.This()))
      {
        return;
      }

      AddAppointmentOperation *wrapper = AddAppointmentOperation::Unwrap<AddAppointmentOperation>(info.This());

      if (info.Length() == 1
        && info[0]->IsString())
      {
        try
        {
          Platform::String^ arg0 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(info[0])));
          
          wrapper->_instance->ReportError(arg0);
          return;   
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else 
      {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }
    }
    static void DismissUI(Nan::NAN_METHOD_ARGS_TYPE info)
    {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::AddAppointmentOperation^>(info.This()))
      {
        return;
      }

      AddAppointmentOperation *wrapper = AddAppointmentOperation::Unwrap<AddAppointmentOperation>(info.This());

      if (info.Length() == 0)
      {
        try
        {
          wrapper->_instance->DismissUI();
          return;   
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else 
      {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }
    }



    static void AppointmentInformationGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::AddAppointmentOperation^>(info.This()))
      {
        return;
      }

      AddAppointmentOperation *wrapper = AddAppointmentOperation::Unwrap<AddAppointmentOperation>(info.This());

      try 
      {
        ::Windows::ApplicationModel::Appointments::Appointment^ result = wrapper->_instance->AppointmentInformation;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.ApplicationModel.Appointments", "Appointment", result));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void SourcePackageFamilyNameGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::AddAppointmentOperation^>(info.This()))
      {
        return;
      }

      AddAppointmentOperation *wrapper = AddAppointmentOperation::Unwrap<AddAppointmentOperation>(info.This());

      try 
      {
        Platform::String^ result = wrapper->_instance->SourcePackageFamilyName;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    


  private:
    ::Windows::ApplicationModel::Appointments::AppointmentsProvider::AddAppointmentOperation^ _instance;
    static Persistent<FunctionTemplate> s_constructorTemplate;

    friend v8::Local<v8::Value> WrapAddAppointmentOperation(::Windows::ApplicationModel::Appointments::AppointmentsProvider::AddAppointmentOperation^ wintRtInstance);
    friend ::Windows::ApplicationModel::Appointments::AppointmentsProvider::AddAppointmentOperation^ UnwrapAddAppointmentOperation(Local<Value> value);
  };
  Persistent<FunctionTemplate> AddAppointmentOperation::s_constructorTemplate;

  v8::Local<v8::Value> WrapAddAppointmentOperation(::Windows::ApplicationModel::Appointments::AppointmentsProvider::AddAppointmentOperation^ winRtInstance)
  {
    EscapableHandleScope scope;

    if (winRtInstance == nullptr)
    {
      return scope.Escape(Undefined());
    }

    Local<Value> opaqueWrapper = CreateOpaqueWrapper(winRtInstance);
    Local<Value> args[] = {opaqueWrapper};
    Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(AddAppointmentOperation::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::ApplicationModel::Appointments::AppointmentsProvider::AddAppointmentOperation^ UnwrapAddAppointmentOperation(Local<Value> value)
  {
     return AddAppointmentOperation::Unwrap<AddAppointmentOperation>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitAddAppointmentOperation(Local<Object> exports)
  {
    AddAppointmentOperation::Init(exports);
  }

  class ReplaceAppointmentOperation : public WrapperBase
  {
  public:    
    static void Init(const Local<Object> exports)
    {
      HandleScope scope;
      
      Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(New);
      s_constructorTemplate.Reset(localRef);
      localRef->SetClassName(Nan::New<String>("ReplaceAppointmentOperation").ToLocalChecked());
      localRef->InstanceTemplate()->SetInternalFieldCount(1);
      
            
      Nan::SetPrototypeMethod(localRef, "reportCompleted", ReportCompleted);
      Nan::SetPrototypeMethod(localRef, "reportCanceled", ReportCanceled);
      Nan::SetPrototypeMethod(localRef, "reportError", ReportError);
      Nan::SetPrototypeMethod(localRef, "dismissUI", DismissUI);
      
                        
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("appointmentId").ToLocalChecked(), AppointmentIdGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("appointmentInformation").ToLocalChecked(), AppointmentInformationGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("instanceStartDate").ToLocalChecked(), InstanceStartDateGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("sourcePackageFamilyName").ToLocalChecked(), SourcePackageFamilyNameGetter);
      
      Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
	  Nan::SetMethod(constructor, "castFrom", CastFrom);


      Nan::Set(exports, Nan::New<String>("ReplaceAppointmentOperation").ToLocalChecked(), constructor);
    }


    virtual ::Platform::Object^ GetObjectInstance() const override
    {
      return _instance;
    }

  private:
    
    ReplaceAppointmentOperation(::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^ instance)
    {
      _instance = instance;
    }
    
    
    static void New(Nan::NAN_METHOD_ARGS_TYPE info)
    {
      HandleScope scope;

	    Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(s_constructorTemplate);

      // in case the constructor was called without the new operator
      if (!localRef->HasInstance(info.This()))
      {
        if (info.Length() > 0)
        {
          std::unique_ptr<Local<Value> []> constructorArgs(new Local<Value>[info.Length()]);

          Local<Value> *argsPtr = constructorArgs.get();
          for (int i = 0; i < info.Length(); i++)
          {
            argsPtr[i] = info[i];
          }

		  MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), constructorArgs.get());
		  if (res.IsEmpty())
		  {
			  return;
		  }
		  info.GetReturnValue().Set(res.ToLocalChecked());
		  return;
		}
		else
		{
          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), nullptr);
          if (res.IsEmpty())
          {
            return;
          }
          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        }
      }
      
      ::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^>(info[0]))
      {
        try 
        {
          winRtInstance = (::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^) NodeRT::Utils::GetObjectInstance(info[0]);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else
      {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no suitable constructor found")));
	    	return;
      }

      NodeRT::Utils::SetHiddenValue(info.This(), Nan::New<String>("__winRtInstance__").ToLocalChecked(), True());

      ReplaceAppointmentOperation *wrapperInstance = new ReplaceAppointmentOperation(winRtInstance);
      wrapperInstance->Wrap(info.This());

      info.GetReturnValue().Set(info.This());
    }


	
    static void CastFrom(Nan::NAN_METHOD_ARGS_TYPE info)
    {
		HandleScope scope;
		if (info.Length() < 1 || !NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^>(info[0]))
		{
			Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no object provided, or given object could not be casted to requested type")));
			return;
		}

		::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^ winRtInstance;
		try
		{
			winRtInstance = (::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^) NodeRT::Utils::GetObjectInstance(info[0]);
		}
		catch (Platform::Exception ^exception)
		{
			NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
			return;
		}

		info.GetReturnValue().Set(WrapReplaceAppointmentOperation(winRtInstance));
    }


  
    static void ReportCompleted(Nan::NAN_METHOD_ARGS_TYPE info)
    {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^>(info.This()))
      {
        return;
      }

      ReplaceAppointmentOperation *wrapper = ReplaceAppointmentOperation::Unwrap<ReplaceAppointmentOperation>(info.This());

      if (info.Length() == 1
        && info[0]->IsString())
      {
        try
        {
          Platform::String^ arg0 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(info[0])));
          
          wrapper->_instance->ReportCompleted(arg0);
          return;   
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else 
      {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }
    }
    static void ReportCanceled(Nan::NAN_METHOD_ARGS_TYPE info)
    {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^>(info.This()))
      {
        return;
      }

      ReplaceAppointmentOperation *wrapper = ReplaceAppointmentOperation::Unwrap<ReplaceAppointmentOperation>(info.This());

      if (info.Length() == 0)
      {
        try
        {
          wrapper->_instance->ReportCanceled();
          return;   
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else 
      {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }
    }
    static void ReportError(Nan::NAN_METHOD_ARGS_TYPE info)
    {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^>(info.This()))
      {
        return;
      }

      ReplaceAppointmentOperation *wrapper = ReplaceAppointmentOperation::Unwrap<ReplaceAppointmentOperation>(info.This());

      if (info.Length() == 1
        && info[0]->IsString())
      {
        try
        {
          Platform::String^ arg0 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(info[0])));
          
          wrapper->_instance->ReportError(arg0);
          return;   
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else 
      {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }
    }
    static void DismissUI(Nan::NAN_METHOD_ARGS_TYPE info)
    {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^>(info.This()))
      {
        return;
      }

      ReplaceAppointmentOperation *wrapper = ReplaceAppointmentOperation::Unwrap<ReplaceAppointmentOperation>(info.This());

      if (info.Length() == 0)
      {
        try
        {
          wrapper->_instance->DismissUI();
          return;   
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else 
      {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }
    }



    static void AppointmentIdGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^>(info.This()))
      {
        return;
      }

      ReplaceAppointmentOperation *wrapper = ReplaceAppointmentOperation::Unwrap<ReplaceAppointmentOperation>(info.This());

      try 
      {
        Platform::String^ result = wrapper->_instance->AppointmentId;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void AppointmentInformationGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^>(info.This()))
      {
        return;
      }

      ReplaceAppointmentOperation *wrapper = ReplaceAppointmentOperation::Unwrap<ReplaceAppointmentOperation>(info.This());

      try 
      {
        ::Windows::ApplicationModel::Appointments::Appointment^ result = wrapper->_instance->AppointmentInformation;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.ApplicationModel.Appointments", "Appointment", result));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void InstanceStartDateGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^>(info.This()))
      {
        return;
      }

      ReplaceAppointmentOperation *wrapper = ReplaceAppointmentOperation::Unwrap<ReplaceAppointmentOperation>(info.This());

      try 
      {
        ::Platform::IBox<::Windows::Foundation::DateTime>^ result = wrapper->_instance->InstanceStartDate;
        info.GetReturnValue().Set(result ? static_cast<Local<Value>>(NodeRT::Utils::DateTimeToJS(result->Value)) : Undefined());
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void SourcePackageFamilyNameGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^>(info.This()))
      {
        return;
      }

      ReplaceAppointmentOperation *wrapper = ReplaceAppointmentOperation::Unwrap<ReplaceAppointmentOperation>(info.This());

      try 
      {
        Platform::String^ result = wrapper->_instance->SourcePackageFamilyName;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    


  private:
    ::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^ _instance;
    static Persistent<FunctionTemplate> s_constructorTemplate;

    friend v8::Local<v8::Value> WrapReplaceAppointmentOperation(::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^ wintRtInstance);
    friend ::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^ UnwrapReplaceAppointmentOperation(Local<Value> value);
  };
  Persistent<FunctionTemplate> ReplaceAppointmentOperation::s_constructorTemplate;

  v8::Local<v8::Value> WrapReplaceAppointmentOperation(::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^ winRtInstance)
  {
    EscapableHandleScope scope;

    if (winRtInstance == nullptr)
    {
      return scope.Escape(Undefined());
    }

    Local<Value> opaqueWrapper = CreateOpaqueWrapper(winRtInstance);
    Local<Value> args[] = {opaqueWrapper};
    Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(ReplaceAppointmentOperation::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::ApplicationModel::Appointments::AppointmentsProvider::ReplaceAppointmentOperation^ UnwrapReplaceAppointmentOperation(Local<Value> value)
  {
     return ReplaceAppointmentOperation::Unwrap<ReplaceAppointmentOperation>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitReplaceAppointmentOperation(Local<Object> exports)
  {
    ReplaceAppointmentOperation::Init(exports);
  }

  class RemoveAppointmentOperation : public WrapperBase
  {
  public:    
    static void Init(const Local<Object> exports)
    {
      HandleScope scope;
      
      Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(New);
      s_constructorTemplate.Reset(localRef);
      localRef->SetClassName(Nan::New<String>("RemoveAppointmentOperation").ToLocalChecked());
      localRef->InstanceTemplate()->SetInternalFieldCount(1);
      
            
      Nan::SetPrototypeMethod(localRef, "reportCompleted", ReportCompleted);
      Nan::SetPrototypeMethod(localRef, "reportCanceled", ReportCanceled);
      Nan::SetPrototypeMethod(localRef, "reportError", ReportError);
      Nan::SetPrototypeMethod(localRef, "dismissUI", DismissUI);
      
                        
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("appointmentId").ToLocalChecked(), AppointmentIdGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("instanceStartDate").ToLocalChecked(), InstanceStartDateGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("sourcePackageFamilyName").ToLocalChecked(), SourcePackageFamilyNameGetter);
      
      Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
	  Nan::SetMethod(constructor, "castFrom", CastFrom);


      Nan::Set(exports, Nan::New<String>("RemoveAppointmentOperation").ToLocalChecked(), constructor);
    }


    virtual ::Platform::Object^ GetObjectInstance() const override
    {
      return _instance;
    }

  private:
    
    RemoveAppointmentOperation(::Windows::ApplicationModel::Appointments::AppointmentsProvider::RemoveAppointmentOperation^ instance)
    {
      _instance = instance;
    }
    
    
    static void New(Nan::NAN_METHOD_ARGS_TYPE info)
    {
      HandleScope scope;

	    Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(s_constructorTemplate);

      // in case the constructor was called without the new operator
      if (!localRef->HasInstance(info.This()))
      {
        if (info.Length() > 0)
        {
          std::unique_ptr<Local<Value> []> constructorArgs(new Local<Value>[info.Length()]);

          Local<Value> *argsPtr = constructorArgs.get();
          for (int i = 0; i < info.Length(); i++)
          {
            argsPtr[i] = info[i];
          }

		  MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), constructorArgs.get());
		  if (res.IsEmpty())
		  {
			  return;
		  }
		  info.GetReturnValue().Set(res.ToLocalChecked());
		  return;
		}
		else
		{
          MaybeLocal<Object> res = Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(), info.Length(), nullptr);
          if (res.IsEmpty())
          {
            return;
          }
          info.GetReturnValue().Set(res.ToLocalChecked());
          return;
        }
      }
      
      ::Windows::ApplicationModel::Appointments::AppointmentsProvider::RemoveAppointmentOperation^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::RemoveAppointmentOperation^>(info[0]))
      {
        try 
        {
          winRtInstance = (::Windows::ApplicationModel::Appointments::AppointmentsProvider::RemoveAppointmentOperation^) NodeRT::Utils::GetObjectInstance(info[0]);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else
      {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no suitable constructor found")));
	    	return;
      }

      NodeRT::Utils::SetHiddenValue(info.This(), Nan::New<String>("__winRtInstance__").ToLocalChecked(), True());

      RemoveAppointmentOperation *wrapperInstance = new RemoveAppointmentOperation(winRtInstance);
      wrapperInstance->Wrap(info.This());

      info.GetReturnValue().Set(info.This());
    }


	
    static void CastFrom(Nan::NAN_METHOD_ARGS_TYPE info)
    {
		HandleScope scope;
		if (info.Length() < 1 || !NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::RemoveAppointmentOperation^>(info[0]))
		{
			Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Invalid arguments, no object provided, or given object could not be casted to requested type")));
			return;
		}

		::Windows::ApplicationModel::Appointments::AppointmentsProvider::RemoveAppointmentOperation^ winRtInstance;
		try
		{
			winRtInstance = (::Windows::ApplicationModel::Appointments::AppointmentsProvider::RemoveAppointmentOperation^) NodeRT::Utils::GetObjectInstance(info[0]);
		}
		catch (Platform::Exception ^exception)
		{
			NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
			return;
		}

		info.GetReturnValue().Set(WrapRemoveAppointmentOperation(winRtInstance));
    }


  
    static void ReportCompleted(Nan::NAN_METHOD_ARGS_TYPE info)
    {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::RemoveAppointmentOperation^>(info.This()))
      {
        return;
      }

      RemoveAppointmentOperation *wrapper = RemoveAppointmentOperation::Unwrap<RemoveAppointmentOperation>(info.This());

      if (info.Length() == 0)
      {
        try
        {
          wrapper->_instance->ReportCompleted();
          return;   
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else 
      {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }
    }
    static void ReportCanceled(Nan::NAN_METHOD_ARGS_TYPE info)
    {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::RemoveAppointmentOperation^>(info.This()))
      {
        return;
      }

      RemoveAppointmentOperation *wrapper = RemoveAppointmentOperation::Unwrap<RemoveAppointmentOperation>(info.This());

      if (info.Length() == 0)
      {
        try
        {
          wrapper->_instance->ReportCanceled();
          return;   
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else 
      {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }
    }
    static void ReportError(Nan::NAN_METHOD_ARGS_TYPE info)
    {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::RemoveAppointmentOperation^>(info.This()))
      {
        return;
      }

      RemoveAppointmentOperation *wrapper = RemoveAppointmentOperation::Unwrap<RemoveAppointmentOperation>(info.This());

      if (info.Length() == 1
        && info[0]->IsString())
      {
        try
        {
          Platform::String^ arg0 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(info[0])));
          
          wrapper->_instance->ReportError(arg0);
          return;   
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else 
      {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }
    }
    static void DismissUI(Nan::NAN_METHOD_ARGS_TYPE info)
    {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::RemoveAppointmentOperation^>(info.This()))
      {
        return;
      }

      RemoveAppointmentOperation *wrapper = RemoveAppointmentOperation::Unwrap<RemoveAppointmentOperation>(info.This());

      if (info.Length() == 0)
      {
        try
        {
          wrapper->_instance->DismissUI();
          return;   
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else 
      {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Bad arguments: no suitable overload found")));
        return;
      }
    }



    static void AppointmentIdGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::RemoveAppointmentOperation^>(info.This()))
      {
        return;
      }

      RemoveAppointmentOperation *wrapper = RemoveAppointmentOperation::Unwrap<RemoveAppointmentOperation>(info.This());

      try 
      {
        Platform::String^ result = wrapper->_instance->AppointmentId;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void InstanceStartDateGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::RemoveAppointmentOperation^>(info.This()))
      {
        return;
      }

      RemoveAppointmentOperation *wrapper = RemoveAppointmentOperation::Unwrap<RemoveAppointmentOperation>(info.This());

      try 
      {
        ::Platform::IBox<::Windows::Foundation::DateTime>^ result = wrapper->_instance->InstanceStartDate;
        info.GetReturnValue().Set(result ? static_cast<Local<Value>>(NodeRT::Utils::DateTimeToJS(result->Value)) : Undefined());
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void SourcePackageFamilyNameGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Appointments::AppointmentsProvider::RemoveAppointmentOperation^>(info.This()))
      {
        return;
      }

      RemoveAppointmentOperation *wrapper = RemoveAppointmentOperation::Unwrap<RemoveAppointmentOperation>(info.This());

      try 
      {
        Platform::String^ result = wrapper->_instance->SourcePackageFamilyName;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    


  private:
    ::Windows::ApplicationModel::Appointments::AppointmentsProvider::RemoveAppointmentOperation^ _instance;
    static Persistent<FunctionTemplate> s_constructorTemplate;

    friend v8::Local<v8::Value> WrapRemoveAppointmentOperation(::Windows::ApplicationModel::Appointments::AppointmentsProvider::RemoveAppointmentOperation^ wintRtInstance);
    friend ::Windows::ApplicationModel::Appointments::AppointmentsProvider::RemoveAppointmentOperation^ UnwrapRemoveAppointmentOperation(Local<Value> value);
  };
  Persistent<FunctionTemplate> RemoveAppointmentOperation::s_constructorTemplate;

  v8::Local<v8::Value> WrapRemoveAppointmentOperation(::Windows::ApplicationModel::Appointments::AppointmentsProvider::RemoveAppointmentOperation^ winRtInstance)
  {
    EscapableHandleScope scope;

    if (winRtInstance == nullptr)
    {
      return scope.Escape(Undefined());
    }

    Local<Value> opaqueWrapper = CreateOpaqueWrapper(winRtInstance);
    Local<Value> args[] = {opaqueWrapper};
    Local<FunctionTemplate> localRef = Nan::New<FunctionTemplate>(RemoveAppointmentOperation::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::ApplicationModel::Appointments::AppointmentsProvider::RemoveAppointmentOperation^ UnwrapRemoveAppointmentOperation(Local<Value> value)
  {
     return RemoveAppointmentOperation::Unwrap<RemoveAppointmentOperation>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitRemoveAppointmentOperation(Local<Object> exports)
  {
    RemoveAppointmentOperation::Init(exports);
  }

} } } } } 

NAN_MODULE_INIT(init)
{
  // we ignore failures for now since it probably means that the initialization already happened for STA, and that's cool
  CoInitializeEx(nullptr, COINIT_MULTITHREADED);
  //if (FAILED(CoInitializeEx(nullptr, COINIT_MULTITHREADED)))
  /*{
    Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"error in CoInitializeEx()")));
    return;
  }*/
  
  NodeRT::Windows::ApplicationModel::Appointments::AppointmentsProvider::InitAppointmentsProviderLaunchActionVerbs(target);
  NodeRT::Windows::ApplicationModel::Appointments::AppointmentsProvider::InitAddAppointmentOperation(target);
  NodeRT::Windows::ApplicationModel::Appointments::AppointmentsProvider::InitReplaceAppointmentOperation(target);
  NodeRT::Windows::ApplicationModel::Appointments::AppointmentsProvider::InitRemoveAppointmentOperation(target);

  NodeRT::Utils::RegisterNameSpace("Windows.ApplicationModel.Appointments.AppointmentsProvider", target);
}


NODE_MODULE(binding, init)