// Copyright (c) The NodeRT Contributors
// 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::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 Storage { namespace Provider { 
  v8::Local<v8::Value> WrapCachedFileUpdaterUI(::Windows::Storage::Provider::CachedFileUpdaterUI^ wintRtInstance);
  ::Windows::Storage::Provider::CachedFileUpdaterUI^ UnwrapCachedFileUpdaterUI(Local<Value> value);
  
  v8::Local<v8::Value> WrapFileUpdateRequestedEventArgs(::Windows::Storage::Provider::FileUpdateRequestedEventArgs^ wintRtInstance);
  ::Windows::Storage::Provider::FileUpdateRequestedEventArgs^ UnwrapFileUpdateRequestedEventArgs(Local<Value> value);
  
  v8::Local<v8::Value> WrapFileUpdateRequest(::Windows::Storage::Provider::FileUpdateRequest^ wintRtInstance);
  ::Windows::Storage::Provider::FileUpdateRequest^ UnwrapFileUpdateRequest(Local<Value> value);
  
  v8::Local<v8::Value> WrapFileUpdateRequestDeferral(::Windows::Storage::Provider::FileUpdateRequestDeferral^ wintRtInstance);
  ::Windows::Storage::Provider::FileUpdateRequestDeferral^ UnwrapFileUpdateRequestDeferral(Local<Value> value);
  
  v8::Local<v8::Value> WrapCachedFileUpdater(::Windows::Storage::Provider::CachedFileUpdater^ wintRtInstance);
  ::Windows::Storage::Provider::CachedFileUpdater^ UnwrapCachedFileUpdater(Local<Value> value);
  



  static void InitCachedFileTargetEnum(const Local<Object> exports) {
    HandleScope scope;

    Local<Object> enumObject = Nan::New<Object>();

    Nan::Set(exports, Nan::New<String>("CachedFileTarget").ToLocalChecked(), enumObject);
    Nan::Set(enumObject, Nan::New<String>("local").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Storage::Provider::CachedFileTarget::Local)));
    Nan::Set(enumObject, Nan::New<String>("remote").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Storage::Provider::CachedFileTarget::Remote)));
  }

  static void InitUIStatusEnum(const Local<Object> exports) {
    HandleScope scope;

    Local<Object> enumObject = Nan::New<Object>();

    Nan::Set(exports, Nan::New<String>("UIStatus").ToLocalChecked(), enumObject);
    Nan::Set(enumObject, Nan::New<String>("unavailable").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Storage::Provider::UIStatus::Unavailable)));
    Nan::Set(enumObject, Nan::New<String>("hidden").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Storage::Provider::UIStatus::Hidden)));
    Nan::Set(enumObject, Nan::New<String>("visible").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Storage::Provider::UIStatus::Visible)));
    Nan::Set(enumObject, Nan::New<String>("complete").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Storage::Provider::UIStatus::Complete)));
  }

  static void InitFileUpdateStatusEnum(const Local<Object> exports) {
    HandleScope scope;

    Local<Object> enumObject = Nan::New<Object>();

    Nan::Set(exports, Nan::New<String>("FileUpdateStatus").ToLocalChecked(), enumObject);
    Nan::Set(enumObject, Nan::New<String>("incomplete").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Storage::Provider::FileUpdateStatus::Incomplete)));
    Nan::Set(enumObject, Nan::New<String>("complete").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Storage::Provider::FileUpdateStatus::Complete)));
    Nan::Set(enumObject, Nan::New<String>("userInputNeeded").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Storage::Provider::FileUpdateStatus::UserInputNeeded)));
    Nan::Set(enumObject, Nan::New<String>("currentlyUnavailable").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Storage::Provider::FileUpdateStatus::CurrentlyUnavailable)));
    Nan::Set(enumObject, Nan::New<String>("failed").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Storage::Provider::FileUpdateStatus::Failed)));
    Nan::Set(enumObject, Nan::New<String>("completeAndRenamed").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Storage::Provider::FileUpdateStatus::CompleteAndRenamed)));
  }

  static void InitCachedFileOptionsEnum(const Local<Object> exports) {
    HandleScope scope;

    Local<Object> enumObject = Nan::New<Object>();

    Nan::Set(exports, Nan::New<String>("CachedFileOptions").ToLocalChecked(), enumObject);
    Nan::Set(enumObject, Nan::New<String>("none").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Storage::Provider::CachedFileOptions::None)));
    Nan::Set(enumObject, Nan::New<String>("requireUpdateOnAccess").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Storage::Provider::CachedFileOptions::RequireUpdateOnAccess)));
    Nan::Set(enumObject, Nan::New<String>("useCachedFileWhenOffline").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Storage::Provider::CachedFileOptions::UseCachedFileWhenOffline)));
    Nan::Set(enumObject, Nan::New<String>("denyAccessWhenOffline").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Storage::Provider::CachedFileOptions::DenyAccessWhenOffline)));
  }

  static void InitReadActivationModeEnum(const Local<Object> exports) {
    HandleScope scope;

    Local<Object> enumObject = Nan::New<Object>();

    Nan::Set(exports, Nan::New<String>("ReadActivationMode").ToLocalChecked(), enumObject);
    Nan::Set(enumObject, Nan::New<String>("notNeeded").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Storage::Provider::ReadActivationMode::NotNeeded)));
    Nan::Set(enumObject, Nan::New<String>("beforeAccess").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Storage::Provider::ReadActivationMode::BeforeAccess)));
  }

  static void InitWriteActivationModeEnum(const Local<Object> exports) {
    HandleScope scope;

    Local<Object> enumObject = Nan::New<Object>();

    Nan::Set(exports, Nan::New<String>("WriteActivationMode").ToLocalChecked(), enumObject);
    Nan::Set(enumObject, Nan::New<String>("readOnly").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Storage::Provider::WriteActivationMode::ReadOnly)));
    Nan::Set(enumObject, Nan::New<String>("notNeeded").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Storage::Provider::WriteActivationMode::NotNeeded)));
    Nan::Set(enumObject, Nan::New<String>("afterWrite").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Storage::Provider::WriteActivationMode::AfterWrite)));
  }



  class CachedFileUpdaterUI : 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>("CachedFileUpdaterUI").ToLocalChecked());
        localRef->InstanceTemplate()->SetInternalFieldCount(1);


          
            Nan::SetPrototypeMethod(localRef, "getDeferral", GetDeferral);
          


          
          Nan::SetPrototypeMethod(localRef,"addListener", AddListener);
          Nan::SetPrototypeMethod(localRef,"on", AddListener);
          Nan::SetPrototypeMethod(localRef,"removeListener", RemoveListener);
          Nan::SetPrototypeMethod(localRef, "off", RemoveListener);

          
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("title").ToLocalChecked(), TitleGetter, TitleSetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("uIStatus").ToLocalChecked(), UIStatusGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("updateTarget").ToLocalChecked(), UpdateTargetGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("updateRequest").ToLocalChecked(), UpdateRequestGetter);

        Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
        Nan::SetMethod(constructor, "castFrom", CastFrom);



        Nan::Set(exports, Nan::New<String>("CachedFileUpdaterUI").ToLocalChecked(), constructor);
      }

      virtual ::Platform::Object^ GetObjectInstance() const override {
        return _instance;
      }

    private:

      CachedFileUpdaterUI(::Windows::Storage::Provider::CachedFileUpdaterUI^ 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::Storage::Provider::CachedFileUpdaterUI^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::CachedFileUpdaterUI^>(info[0])) {
        try {
          winRtInstance = (::Windows::Storage::Provider::CachedFileUpdaterUI^) 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());

      CachedFileUpdaterUI *wrapperInstance = new CachedFileUpdaterUI(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::Storage::Provider::CachedFileUpdaterUI^>(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::Storage::Provider::CachedFileUpdaterUI^ winRtInstance;
      try {
        winRtInstance = (::Windows::Storage::Provider::CachedFileUpdaterUI^) NodeRT::Utils::GetObjectInstance(info[0]);
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }

      info.GetReturnValue().Set(WrapCachedFileUpdaterUI(winRtInstance));
    }


    static void GetDeferral(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::CachedFileUpdaterUI^>(info.This())) {
        return;
      }

      CachedFileUpdaterUI *wrapper = CachedFileUpdaterUI::Unwrap<CachedFileUpdaterUI>(info.This());

      if (info.Length() == 0)
      {
        try
        {
          ::Windows::Storage::Provider::FileUpdateRequestDeferral^ result;
          result = wrapper->_instance->GetDeferral();
          info.GetReturnValue().Set(WrapFileUpdateRequestDeferral(result));
          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 TitleGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::CachedFileUpdaterUI^>(info.This())) {
        return;
      }

      CachedFileUpdaterUI *wrapper = CachedFileUpdaterUI::Unwrap<CachedFileUpdaterUI>(info.This());

      try  {
        Platform::String^ result = wrapper->_instance->Title;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void TitleSetter(Local<String> property, Local<Value> value, const Nan::PropertyCallbackInfo<void> &info) {
      HandleScope scope;

      if (!value->IsString()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Value to set is of unexpected type")));
        return;
      }

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::CachedFileUpdaterUI^>(info.This())) {
        return;
      }

      CachedFileUpdaterUI *wrapper = CachedFileUpdaterUI::Unwrap<CachedFileUpdaterUI>(info.This());

      try {

        Platform::String^ winRtValue = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(v8::Isolate::GetCurrent(), value)));

        wrapper->_instance->Title = winRtValue;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
      }
    }
      
    static void UIStatusGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::CachedFileUpdaterUI^>(info.This())) {
        return;
      }

      CachedFileUpdaterUI *wrapper = CachedFileUpdaterUI::Unwrap<CachedFileUpdaterUI>(info.This());

      try  {
        ::Windows::Storage::Provider::UIStatus result = wrapper->_instance->UIStatus;
        info.GetReturnValue().Set(Nan::New<Integer>(static_cast<int>(result)));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void UpdateTargetGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::CachedFileUpdaterUI^>(info.This())) {
        return;
      }

      CachedFileUpdaterUI *wrapper = CachedFileUpdaterUI::Unwrap<CachedFileUpdaterUI>(info.This());

      try  {
        ::Windows::Storage::Provider::CachedFileTarget result = wrapper->_instance->UpdateTarget;
        info.GetReturnValue().Set(Nan::New<Integer>(static_cast<int>(result)));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void UpdateRequestGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::CachedFileUpdaterUI^>(info.This())) {
        return;
      }

      CachedFileUpdaterUI *wrapper = CachedFileUpdaterUI::Unwrap<CachedFileUpdaterUI>(info.This());

      try  {
        ::Windows::Storage::Provider::FileUpdateRequest^ result = wrapper->_instance->UpdateRequest;
        info.GetReturnValue().Set(WrapFileUpdateRequest(result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      


    static void AddListener(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (info.Length() < 2 || !info[0]->IsString() || !info[1]->IsFunction()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"wrong arguments, expected arguments are eventName(string),callback(function)")));
        return;
      }

      String::Value eventName(v8::Isolate::GetCurrent(), info[0]);
      auto str = *eventName;

      Local<Function> callback = info[1].As<Function>();

      ::Windows::Foundation::EventRegistrationToken registrationToken;
      if (NodeRT::Utils::CaseInsenstiveEquals(L"fileUpdateRequested", str))
      {
        if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::CachedFileUpdaterUI^>(info.This()))
        {
          Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"The caller of this method isn't of the expected type or internal WinRt object was disposed")));
      return;
        }
        CachedFileUpdaterUI *wrapper = CachedFileUpdaterUI::Unwrap<CachedFileUpdaterUI>(info.This());
      
        try {
          Persistent<Object>* perstPtr = new Persistent<Object>();
          perstPtr->Reset(NodeRT::Utils::CreateCallbackObjectInDomain(callback));
          std::shared_ptr<Persistent<Object>> callbackObjPtr(perstPtr,
            [] (Persistent<Object> *ptr ) {
              NodeUtils::Async::RunOnMain([ptr]() {
                ptr->Reset();
                delete ptr;
            });
          });

          registrationToken = wrapper->_instance->FileUpdateRequested::add(
            ref new ::Windows::Foundation::TypedEventHandler<::Windows::Storage::Provider::CachedFileUpdaterUI^, ::Windows::Storage::Provider::FileUpdateRequestedEventArgs^>(
            [callbackObjPtr](::Windows::Storage::Provider::CachedFileUpdaterUI^ arg0, ::Windows::Storage::Provider::FileUpdateRequestedEventArgs^ arg1) {
              NodeUtils::Async::RunOnMain([callbackObjPtr , arg0, arg1]() {
                HandleScope scope;


                Local<Value> wrappedArg0;
                Local<Value> wrappedArg1;

                {
                  TryCatch tryCatch;


                  wrappedArg0 = WrapCachedFileUpdaterUI(arg0);
                  wrappedArg1 = WrapFileUpdateRequestedEventArgs(arg1);


                  if (wrappedArg0.IsEmpty()) wrappedArg0 = Undefined();
                  if (wrappedArg1.IsEmpty()) wrappedArg1 = Undefined();
                }

                Local<Value> args[] = { wrappedArg0, wrappedArg1 };
                Local<Object> callbackObjLocalRef = Nan::New<Object>(*callbackObjPtr);
                NodeRT::Utils::CallCallbackInDomain(callbackObjLocalRef, _countof(args), args);
              });
            })
          );
        }
        catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }

      }
      else if (NodeRT::Utils::CaseInsenstiveEquals(L"uIRequested", str))
      {
        if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::CachedFileUpdaterUI^>(info.This()))
        {
          Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"The caller of this method isn't of the expected type or internal WinRt object was disposed")));
      return;
        }
        CachedFileUpdaterUI *wrapper = CachedFileUpdaterUI::Unwrap<CachedFileUpdaterUI>(info.This());
      
        try {
          Persistent<Object>* perstPtr = new Persistent<Object>();
          perstPtr->Reset(NodeRT::Utils::CreateCallbackObjectInDomain(callback));
          std::shared_ptr<Persistent<Object>> callbackObjPtr(perstPtr,
            [] (Persistent<Object> *ptr ) {
              NodeUtils::Async::RunOnMain([ptr]() {
                ptr->Reset();
                delete ptr;
            });
          });

          registrationToken = wrapper->_instance->UIRequested::add(
            ref new ::Windows::Foundation::TypedEventHandler<::Windows::Storage::Provider::CachedFileUpdaterUI^, ::Platform::Object^>(
            [callbackObjPtr](::Windows::Storage::Provider::CachedFileUpdaterUI^ arg0, ::Platform::Object^ arg1) {
              NodeUtils::Async::RunOnMain([callbackObjPtr , arg0, arg1]() {
                HandleScope scope;


                Local<Value> wrappedArg0;
                Local<Value> wrappedArg1;

                {
                  TryCatch tryCatch;


                  wrappedArg0 = WrapCachedFileUpdaterUI(arg0);
                  wrappedArg1 = CreateOpaqueWrapper(arg1);


                  if (wrappedArg0.IsEmpty()) wrappedArg0 = Undefined();
                  if (wrappedArg1.IsEmpty()) wrappedArg1 = Undefined();
                }

                Local<Value> args[] = { wrappedArg0, wrappedArg1 };
                Local<Object> callbackObjLocalRef = Nan::New<Object>(*callbackObjPtr);
                NodeRT::Utils::CallCallbackInDomain(callbackObjLocalRef, _countof(args), args);
              });
            })
          );
        }
        catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }

      }
 else  {
        Nan::ThrowError(Nan::Error(String::Concat(v8::Isolate::GetCurrent(), NodeRT::Utils::NewString(L"given event name isn't supported: "), info[0].As<String>())));
        return;
      }

      Local<Value> tokenMapVal = NodeRT::Utils::GetHiddenValue(callback, Nan::New<String>(REGISTRATION_TOKEN_MAP_PROPERTY_NAME).ToLocalChecked());
      Local<Object> tokenMap;

      if (tokenMapVal.IsEmpty() || Nan::Equals(tokenMapVal, Undefined()).FromMaybe(false)) {
        tokenMap = Nan::New<Object>();
        NodeRT::Utils::SetHiddenValueWithObject(callback, Nan::New<String>(REGISTRATION_TOKEN_MAP_PROPERTY_NAME).ToLocalChecked(), tokenMap);
      } else {
        tokenMap = Nan::To<Object>(tokenMapVal).ToLocalChecked();
      }

      Nan::Set(tokenMap, info[0], CreateOpaqueWrapper(::Windows::Foundation::PropertyValue::CreateInt64(registrationToken.Value)));
    }

    static void RemoveListener(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (info.Length() < 2 || !info[0]->IsString() || !info[1]->IsFunction()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"wrong arguments, expected a string and a callback")));
        return;
      }

      String::Value eventName(v8::Isolate::GetCurrent(), info[0]);
      auto str = *eventName;

      if ((!NodeRT::Utils::CaseInsenstiveEquals(L"fileUpdateRequested", str)) &&(!NodeRT::Utils::CaseInsenstiveEquals(L"uIRequested", str))) {
        Nan::ThrowError(Nan::Error(String::Concat(v8::Isolate::GetCurrent(), NodeRT::Utils::NewString(L"given event name isn't supported: "), info[0].As<String>())));
        return;
      }

      Local<Function> callback = info[1].As<Function>();
      Local<Value> tokenMap = NodeRT::Utils::GetHiddenValue(callback, Nan::New<String>(REGISTRATION_TOKEN_MAP_PROPERTY_NAME).ToLocalChecked());

      if (tokenMap.IsEmpty() || Nan::Equals(tokenMap, Undefined()).FromMaybe(false)) {
        return;
      }

      Local<Value> opaqueWrapperObj =  Nan::Get(Nan::To<Object>(tokenMap).ToLocalChecked(), info[0]).ToLocalChecked();

      if (opaqueWrapperObj.IsEmpty() || Nan::Equals(opaqueWrapperObj,Undefined()).FromMaybe(false)) {
        return;
      }

      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"fileUpdateRequested", str)) {
          if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::CachedFileUpdaterUI^>(info.This()))
          {
            Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"The caller of this method isn't of the expected type or internal WinRt object was disposed")));
            return;
          }
          CachedFileUpdaterUI *wrapper = CachedFileUpdaterUI::Unwrap<CachedFileUpdaterUI>(info.This());
          wrapper->_instance->FileUpdateRequested::remove(registrationToken);
        }
        else if (NodeRT::Utils::CaseInsenstiveEquals(L"uIRequested", str))
        {
          if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::CachedFileUpdaterUI^>(info.This()))
          {
            Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"The caller of this method isn't of the expected type or internal WinRt object was disposed")));
            return;
          }
          CachedFileUpdaterUI *wrapper = CachedFileUpdaterUI::Unwrap<CachedFileUpdaterUI>(info.This());
          wrapper->_instance->UIRequested::remove(registrationToken);
        }
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
      }

      Nan::Delete(Nan::To<Object>(tokenMap).ToLocalChecked(), Nan::To<String>(info[0]).ToLocalChecked());
    }
    private:
      ::Windows::Storage::Provider::CachedFileUpdaterUI^ _instance;
      static Persistent<FunctionTemplate> s_constructorTemplate;

      friend v8::Local<v8::Value> WrapCachedFileUpdaterUI(::Windows::Storage::Provider::CachedFileUpdaterUI^ wintRtInstance);
      friend ::Windows::Storage::Provider::CachedFileUpdaterUI^ UnwrapCachedFileUpdaterUI(Local<Value> value);
  };

  Persistent<FunctionTemplate> CachedFileUpdaterUI::s_constructorTemplate;

  v8::Local<v8::Value> WrapCachedFileUpdaterUI(::Windows::Storage::Provider::CachedFileUpdaterUI^ 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>(CachedFileUpdaterUI::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::Storage::Provider::CachedFileUpdaterUI^ UnwrapCachedFileUpdaterUI(Local<Value> value) {
     return CachedFileUpdaterUI::Unwrap<CachedFileUpdaterUI>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitCachedFileUpdaterUI(Local<Object> exports) {
    CachedFileUpdaterUI::Init(exports);
  }

  class FileUpdateRequestedEventArgs : 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>("FileUpdateRequestedEventArgs").ToLocalChecked());
        localRef->InstanceTemplate()->SetInternalFieldCount(1);





          
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("request").ToLocalChecked(), RequestGetter);

        Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
        Nan::SetMethod(constructor, "castFrom", CastFrom);



        Nan::Set(exports, Nan::New<String>("FileUpdateRequestedEventArgs").ToLocalChecked(), constructor);
      }

      virtual ::Platform::Object^ GetObjectInstance() const override {
        return _instance;
      }

    private:

      FileUpdateRequestedEventArgs(::Windows::Storage::Provider::FileUpdateRequestedEventArgs^ 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::Storage::Provider::FileUpdateRequestedEventArgs^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::FileUpdateRequestedEventArgs^>(info[0])) {
        try {
          winRtInstance = (::Windows::Storage::Provider::FileUpdateRequestedEventArgs^) 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());

      FileUpdateRequestedEventArgs *wrapperInstance = new FileUpdateRequestedEventArgs(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::Storage::Provider::FileUpdateRequestedEventArgs^>(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::Storage::Provider::FileUpdateRequestedEventArgs^ winRtInstance;
      try {
        winRtInstance = (::Windows::Storage::Provider::FileUpdateRequestedEventArgs^) NodeRT::Utils::GetObjectInstance(info[0]);
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }

      info.GetReturnValue().Set(WrapFileUpdateRequestedEventArgs(winRtInstance));
    }





    static void RequestGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::FileUpdateRequestedEventArgs^>(info.This())) {
        return;
      }

      FileUpdateRequestedEventArgs *wrapper = FileUpdateRequestedEventArgs::Unwrap<FileUpdateRequestedEventArgs>(info.This());

      try  {
        ::Windows::Storage::Provider::FileUpdateRequest^ result = wrapper->_instance->Request;
        info.GetReturnValue().Set(WrapFileUpdateRequest(result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      


    private:
      ::Windows::Storage::Provider::FileUpdateRequestedEventArgs^ _instance;
      static Persistent<FunctionTemplate> s_constructorTemplate;

      friend v8::Local<v8::Value> WrapFileUpdateRequestedEventArgs(::Windows::Storage::Provider::FileUpdateRequestedEventArgs^ wintRtInstance);
      friend ::Windows::Storage::Provider::FileUpdateRequestedEventArgs^ UnwrapFileUpdateRequestedEventArgs(Local<Value> value);
  };

  Persistent<FunctionTemplate> FileUpdateRequestedEventArgs::s_constructorTemplate;

  v8::Local<v8::Value> WrapFileUpdateRequestedEventArgs(::Windows::Storage::Provider::FileUpdateRequestedEventArgs^ 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>(FileUpdateRequestedEventArgs::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::Storage::Provider::FileUpdateRequestedEventArgs^ UnwrapFileUpdateRequestedEventArgs(Local<Value> value) {
     return FileUpdateRequestedEventArgs::Unwrap<FileUpdateRequestedEventArgs>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitFileUpdateRequestedEventArgs(Local<Object> exports) {
    FileUpdateRequestedEventArgs::Init(exports);
  }

  class FileUpdateRequest : 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>("FileUpdateRequest").ToLocalChecked());
        localRef->InstanceTemplate()->SetInternalFieldCount(1);


          
            Nan::SetPrototypeMethod(localRef, "getDeferral", GetDeferral);
            Nan::SetPrototypeMethod(localRef, "updateLocalFile", UpdateLocalFile);
          



          
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("status").ToLocalChecked(), StatusGetter, StatusSetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("contentId").ToLocalChecked(), ContentIdGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("file").ToLocalChecked(), FileGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("userInputNeededMessage").ToLocalChecked(), UserInputNeededMessageGetter, UserInputNeededMessageSetter);

        Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
        Nan::SetMethod(constructor, "castFrom", CastFrom);



        Nan::Set(exports, Nan::New<String>("FileUpdateRequest").ToLocalChecked(), constructor);
      }

      virtual ::Platform::Object^ GetObjectInstance() const override {
        return _instance;
      }

    private:

      FileUpdateRequest(::Windows::Storage::Provider::FileUpdateRequest^ 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::Storage::Provider::FileUpdateRequest^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::FileUpdateRequest^>(info[0])) {
        try {
          winRtInstance = (::Windows::Storage::Provider::FileUpdateRequest^) 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());

      FileUpdateRequest *wrapperInstance = new FileUpdateRequest(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::Storage::Provider::FileUpdateRequest^>(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::Storage::Provider::FileUpdateRequest^ winRtInstance;
      try {
        winRtInstance = (::Windows::Storage::Provider::FileUpdateRequest^) NodeRT::Utils::GetObjectInstance(info[0]);
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }

      info.GetReturnValue().Set(WrapFileUpdateRequest(winRtInstance));
    }


    static void GetDeferral(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::FileUpdateRequest^>(info.This())) {
        return;
      }

      FileUpdateRequest *wrapper = FileUpdateRequest::Unwrap<FileUpdateRequest>(info.This());

      if (info.Length() == 0)
      {
        try
        {
          ::Windows::Storage::Provider::FileUpdateRequestDeferral^ result;
          result = wrapper->_instance->GetDeferral();
          info.GetReturnValue().Set(WrapFileUpdateRequestDeferral(result));
          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 UpdateLocalFile(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::FileUpdateRequest^>(info.This())) {
        return;
      }

      FileUpdateRequest *wrapper = FileUpdateRequest::Unwrap<FileUpdateRequest>(info.This());

      if (info.Length() == 1
        && NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::IStorageFile^>(info[0]))
      {
        try
        {
          ::Windows::Storage::IStorageFile^ arg0 = dynamic_cast<::Windows::Storage::IStorageFile^>(NodeRT::Utils::GetObjectInstance(info[0]));
          
          wrapper->_instance->UpdateLocalFile(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 StatusGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::FileUpdateRequest^>(info.This())) {
        return;
      }

      FileUpdateRequest *wrapper = FileUpdateRequest::Unwrap<FileUpdateRequest>(info.This());

      try  {
        ::Windows::Storage::Provider::FileUpdateStatus result = wrapper->_instance->Status;
        info.GetReturnValue().Set(Nan::New<Integer>(static_cast<int>(result)));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void StatusSetter(Local<String> property, Local<Value> value, const Nan::PropertyCallbackInfo<void> &info) {
      HandleScope scope;

      if (!value->IsInt32()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Value to set is of unexpected type")));
        return;
      }

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::FileUpdateRequest^>(info.This())) {
        return;
      }

      FileUpdateRequest *wrapper = FileUpdateRequest::Unwrap<FileUpdateRequest>(info.This());

      try {

        ::Windows::Storage::Provider::FileUpdateStatus winRtValue = static_cast<::Windows::Storage::Provider::FileUpdateStatus>(Nan::To<int32_t>(value).FromMaybe(0));

        wrapper->_instance->Status = winRtValue;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
      }
    }
      
    static void ContentIdGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::FileUpdateRequest^>(info.This())) {
        return;
      }

      FileUpdateRequest *wrapper = FileUpdateRequest::Unwrap<FileUpdateRequest>(info.This());

      try  {
        Platform::String^ result = wrapper->_instance->ContentId;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void FileGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::FileUpdateRequest^>(info.This())) {
        return;
      }

      FileUpdateRequest *wrapper = FileUpdateRequest::Unwrap<FileUpdateRequest>(info.This());

      try  {
        ::Windows::Storage::StorageFile^ result = wrapper->_instance->File;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.Storage", "StorageFile", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void UserInputNeededMessageGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::FileUpdateRequest^>(info.This())) {
        return;
      }

      FileUpdateRequest *wrapper = FileUpdateRequest::Unwrap<FileUpdateRequest>(info.This());

      try  {
        Platform::String^ result = wrapper->_instance->UserInputNeededMessage;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void UserInputNeededMessageSetter(Local<String> property, Local<Value> value, const Nan::PropertyCallbackInfo<void> &info) {
      HandleScope scope;

      if (!value->IsString()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Value to set is of unexpected type")));
        return;
      }

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::FileUpdateRequest^>(info.This())) {
        return;
      }

      FileUpdateRequest *wrapper = FileUpdateRequest::Unwrap<FileUpdateRequest>(info.This());

      try {

        Platform::String^ winRtValue = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(v8::Isolate::GetCurrent(), value)));

        wrapper->_instance->UserInputNeededMessage = winRtValue;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
      }
    }
      


    private:
      ::Windows::Storage::Provider::FileUpdateRequest^ _instance;
      static Persistent<FunctionTemplate> s_constructorTemplate;

      friend v8::Local<v8::Value> WrapFileUpdateRequest(::Windows::Storage::Provider::FileUpdateRequest^ wintRtInstance);
      friend ::Windows::Storage::Provider::FileUpdateRequest^ UnwrapFileUpdateRequest(Local<Value> value);
  };

  Persistent<FunctionTemplate> FileUpdateRequest::s_constructorTemplate;

  v8::Local<v8::Value> WrapFileUpdateRequest(::Windows::Storage::Provider::FileUpdateRequest^ 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>(FileUpdateRequest::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::Storage::Provider::FileUpdateRequest^ UnwrapFileUpdateRequest(Local<Value> value) {
     return FileUpdateRequest::Unwrap<FileUpdateRequest>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitFileUpdateRequest(Local<Object> exports) {
    FileUpdateRequest::Init(exports);
  }

  class FileUpdateRequestDeferral : 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>("FileUpdateRequestDeferral").ToLocalChecked());
        localRef->InstanceTemplate()->SetInternalFieldCount(1);


          
            Nan::SetPrototypeMethod(localRef, "complete", Complete);
          




        Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
        Nan::SetMethod(constructor, "castFrom", CastFrom);



        Nan::Set(exports, Nan::New<String>("FileUpdateRequestDeferral").ToLocalChecked(), constructor);
      }

      virtual ::Platform::Object^ GetObjectInstance() const override {
        return _instance;
      }

    private:

      FileUpdateRequestDeferral(::Windows::Storage::Provider::FileUpdateRequestDeferral^ 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::Storage::Provider::FileUpdateRequestDeferral^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::FileUpdateRequestDeferral^>(info[0])) {
        try {
          winRtInstance = (::Windows::Storage::Provider::FileUpdateRequestDeferral^) 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());

      FileUpdateRequestDeferral *wrapperInstance = new FileUpdateRequestDeferral(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::Storage::Provider::FileUpdateRequestDeferral^>(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::Storage::Provider::FileUpdateRequestDeferral^ winRtInstance;
      try {
        winRtInstance = (::Windows::Storage::Provider::FileUpdateRequestDeferral^) NodeRT::Utils::GetObjectInstance(info[0]);
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }

      info.GetReturnValue().Set(WrapFileUpdateRequestDeferral(winRtInstance));
    }


    static void Complete(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::FileUpdateRequestDeferral^>(info.This())) {
        return;
      }

      FileUpdateRequestDeferral *wrapper = FileUpdateRequestDeferral::Unwrap<FileUpdateRequestDeferral>(info.This());

      if (info.Length() == 0)
      {
        try
        {
          wrapper->_instance->Complete();
          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;
      }
    }





    private:
      ::Windows::Storage::Provider::FileUpdateRequestDeferral^ _instance;
      static Persistent<FunctionTemplate> s_constructorTemplate;

      friend v8::Local<v8::Value> WrapFileUpdateRequestDeferral(::Windows::Storage::Provider::FileUpdateRequestDeferral^ wintRtInstance);
      friend ::Windows::Storage::Provider::FileUpdateRequestDeferral^ UnwrapFileUpdateRequestDeferral(Local<Value> value);
  };

  Persistent<FunctionTemplate> FileUpdateRequestDeferral::s_constructorTemplate;

  v8::Local<v8::Value> WrapFileUpdateRequestDeferral(::Windows::Storage::Provider::FileUpdateRequestDeferral^ 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>(FileUpdateRequestDeferral::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::Storage::Provider::FileUpdateRequestDeferral^ UnwrapFileUpdateRequestDeferral(Local<Value> value) {
     return FileUpdateRequestDeferral::Unwrap<FileUpdateRequestDeferral>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitFileUpdateRequestDeferral(Local<Object> exports) {
    FileUpdateRequestDeferral::Init(exports);
  }

  class CachedFileUpdater : 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>("CachedFileUpdater").ToLocalChecked());
        localRef->InstanceTemplate()->SetInternalFieldCount(1);






        Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
        Nan::SetMethod(constructor, "castFrom", CastFrom);

        Nan::SetMethod(constructor, "setUpdateInformation", SetUpdateInformation);


        Nan::Set(exports, Nan::New<String>("CachedFileUpdater").ToLocalChecked(), constructor);
      }

      virtual ::Platform::Object^ GetObjectInstance() const override {
        return _instance;
      }

    private:

      CachedFileUpdater(::Windows::Storage::Provider::CachedFileUpdater^ 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::Storage::Provider::CachedFileUpdater^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::Provider::CachedFileUpdater^>(info[0])) {
        try {
          winRtInstance = (::Windows::Storage::Provider::CachedFileUpdater^) 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());

      CachedFileUpdater *wrapperInstance = new CachedFileUpdater(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::Storage::Provider::CachedFileUpdater^>(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::Storage::Provider::CachedFileUpdater^ winRtInstance;
      try {
        winRtInstance = (::Windows::Storage::Provider::CachedFileUpdater^) NodeRT::Utils::GetObjectInstance(info[0]);
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }

      info.GetReturnValue().Set(WrapCachedFileUpdater(winRtInstance));
    }





    static void SetUpdateInformation(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (info.Length() == 5
        && NodeRT::Utils::IsWinRtWrapperOf<::Windows::Storage::IStorageFile^>(info[0])
        && info[1]->IsString()
        && info[2]->IsInt32()
        && info[3]->IsInt32()
        && info[4]->IsInt32())
      {
        try
        {
          ::Windows::Storage::IStorageFile^ arg0 = dynamic_cast<::Windows::Storage::IStorageFile^>(NodeRT::Utils::GetObjectInstance(info[0]));
          Platform::String^ arg1 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(v8::Isolate::GetCurrent(), info[1])));
          ::Windows::Storage::Provider::ReadActivationMode arg2 = static_cast<::Windows::Storage::Provider::ReadActivationMode>(Nan::To<int32_t>(info[2]).FromMaybe(0));
          ::Windows::Storage::Provider::WriteActivationMode arg3 = static_cast<::Windows::Storage::Provider::WriteActivationMode>(Nan::To<int32_t>(info[3]).FromMaybe(0));
          ::Windows::Storage::Provider::CachedFileOptions arg4 = static_cast<::Windows::Storage::Provider::CachedFileOptions>(Nan::To<int32_t>(info[4]).FromMaybe(0));
          
          ::Windows::Storage::Provider::CachedFileUpdater::SetUpdateInformation(arg0, arg1, arg2, arg3, arg4);
          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;
      }
    }



    private:
      ::Windows::Storage::Provider::CachedFileUpdater^ _instance;
      static Persistent<FunctionTemplate> s_constructorTemplate;

      friend v8::Local<v8::Value> WrapCachedFileUpdater(::Windows::Storage::Provider::CachedFileUpdater^ wintRtInstance);
      friend ::Windows::Storage::Provider::CachedFileUpdater^ UnwrapCachedFileUpdater(Local<Value> value);
  };

  Persistent<FunctionTemplate> CachedFileUpdater::s_constructorTemplate;

  v8::Local<v8::Value> WrapCachedFileUpdater(::Windows::Storage::Provider::CachedFileUpdater^ 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>(CachedFileUpdater::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::Storage::Provider::CachedFileUpdater^ UnwrapCachedFileUpdater(Local<Value> value) {
     return CachedFileUpdater::Unwrap<CachedFileUpdater>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitCachedFileUpdater(Local<Object> exports) {
    CachedFileUpdater::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::Storage::Provider::InitCachedFileTargetEnum(target);
      NodeRT::Windows::Storage::Provider::InitUIStatusEnum(target);
      NodeRT::Windows::Storage::Provider::InitFileUpdateStatusEnum(target);
      NodeRT::Windows::Storage::Provider::InitCachedFileOptionsEnum(target);
      NodeRT::Windows::Storage::Provider::InitReadActivationModeEnum(target);
      NodeRT::Windows::Storage::Provider::InitWriteActivationModeEnum(target);
      NodeRT::Windows::Storage::Provider::InitCachedFileUpdaterUI(target);
      NodeRT::Windows::Storage::Provider::InitFileUpdateRequestedEventArgs(target);
      NodeRT::Windows::Storage::Provider::InitFileUpdateRequest(target);
      NodeRT::Windows::Storage::Provider::InitFileUpdateRequestDeferral(target);
      NodeRT::Windows::Storage::Provider::InitCachedFileUpdater(target);


  NodeRT::Utils::RegisterNameSpace("Windows.Storage.Provider", target);
}



NODE_MODULE(binding, init)
