// 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 ApplicationModel { namespace Search { namespace Core { 
  v8::Local<v8::Value> WrapSearchSuggestion(::Windows::ApplicationModel::Search::Core::SearchSuggestion^ wintRtInstance);
  ::Windows::ApplicationModel::Search::Core::SearchSuggestion^ UnwrapSearchSuggestion(Local<Value> value);
  
  v8::Local<v8::Value> WrapSearchSuggestionManager(::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^ wintRtInstance);
  ::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^ UnwrapSearchSuggestionManager(Local<Value> value);
  
  v8::Local<v8::Value> WrapSearchSuggestionsRequestedEventArgs(::Windows::ApplicationModel::Search::Core::SearchSuggestionsRequestedEventArgs^ wintRtInstance);
  ::Windows::ApplicationModel::Search::Core::SearchSuggestionsRequestedEventArgs^ UnwrapSearchSuggestionsRequestedEventArgs(Local<Value> value);
  
  v8::Local<v8::Value> WrapRequestingFocusOnKeyboardInputEventArgs(::Windows::ApplicationModel::Search::Core::RequestingFocusOnKeyboardInputEventArgs^ wintRtInstance);
  ::Windows::ApplicationModel::Search::Core::RequestingFocusOnKeyboardInputEventArgs^ UnwrapRequestingFocusOnKeyboardInputEventArgs(Local<Value> value);
  



  static void InitSearchSuggestionKindEnum(const Local<Object> exports) {
    HandleScope scope;

    Local<Object> enumObject = Nan::New<Object>();

    Nan::Set(exports, Nan::New<String>("SearchSuggestionKind").ToLocalChecked(), enumObject);
    Nan::Set(enumObject, Nan::New<String>("query").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::ApplicationModel::Search::Core::SearchSuggestionKind::Query)));
    Nan::Set(enumObject, Nan::New<String>("result").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::ApplicationModel::Search::Core::SearchSuggestionKind::Result)));
    Nan::Set(enumObject, Nan::New<String>("separator").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::ApplicationModel::Search::Core::SearchSuggestionKind::Separator)));
  }

  static bool IsSearchCoreContractJsObject(Local<Value> value) {
    if (!value->IsObject()) {
      return false;
    }

    Local<String> symbol;
    Local<Object> obj = Nan::To<Object>(value).ToLocalChecked();

    return true;
  }

  ::Windows::ApplicationModel::Search::Core::SearchCoreContract SearchCoreContractFromJsObject(Local<Value> value) {
    HandleScope scope;
    ::Windows::ApplicationModel::Search::Core::SearchCoreContract returnValue;

    if (!value->IsObject()) {
      Nan::ThrowError(Nan::TypeError(NodeRT::Utils::NewString(L"Unexpected type, expected an object")));
      return returnValue;
    }

    Local<Object> obj = Nan::To<Object>(value).ToLocalChecked();
    Local<String> symbol;

    return returnValue;
  }

  Local<Value> SearchCoreContractToJsObject(::Windows::ApplicationModel::Search::Core::SearchCoreContract value) {
    EscapableHandleScope scope;

    Local<Object> obj = Nan::New<Object>();


    return scope.Escape(obj);
  }


  class SearchSuggestion : 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>("SearchSuggestion").ToLocalChecked());
        localRef->InstanceTemplate()->SetInternalFieldCount(1);





          
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("detailText").ToLocalChecked(), DetailTextGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("image").ToLocalChecked(), ImageGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("imageAlternateText").ToLocalChecked(), ImageAlternateTextGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("kind").ToLocalChecked(), KindGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("tag").ToLocalChecked(), TagGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("text").ToLocalChecked(), TextGetter);

        Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
        Nan::SetMethod(constructor, "castFrom", CastFrom);



        Nan::Set(exports, Nan::New<String>("SearchSuggestion").ToLocalChecked(), constructor);
      }

      virtual ::Platform::Object^ GetObjectInstance() const override {
        return _instance;
      }

    private:

      SearchSuggestion(::Windows::ApplicationModel::Search::Core::SearchSuggestion^ 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::Search::Core::SearchSuggestion^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestion^>(info[0])) {
        try {
          winRtInstance = (::Windows::ApplicationModel::Search::Core::SearchSuggestion^) 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());

      SearchSuggestion *wrapperInstance = new SearchSuggestion(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::Search::Core::SearchSuggestion^>(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::Search::Core::SearchSuggestion^ winRtInstance;
      try {
        winRtInstance = (::Windows::ApplicationModel::Search::Core::SearchSuggestion^) NodeRT::Utils::GetObjectInstance(info[0]);
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }

      info.GetReturnValue().Set(WrapSearchSuggestion(winRtInstance));
    }





    static void DetailTextGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestion^>(info.This())) {
        return;
      }

      SearchSuggestion *wrapper = SearchSuggestion::Unwrap<SearchSuggestion>(info.This());

      try  {
        Platform::String^ result = wrapper->_instance->DetailText;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void ImageGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestion^>(info.This())) {
        return;
      }

      SearchSuggestion *wrapper = SearchSuggestion::Unwrap<SearchSuggestion>(info.This());

      try  {
        ::Windows::Storage::Streams::IRandomAccessStreamReference^ result = wrapper->_instance->Image;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.Storage.Streams", "IRandomAccessStreamReference", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void ImageAlternateTextGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestion^>(info.This())) {
        return;
      }

      SearchSuggestion *wrapper = SearchSuggestion::Unwrap<SearchSuggestion>(info.This());

      try  {
        Platform::String^ result = wrapper->_instance->ImageAlternateText;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void KindGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestion^>(info.This())) {
        return;
      }

      SearchSuggestion *wrapper = SearchSuggestion::Unwrap<SearchSuggestion>(info.This());

      try  {
        ::Windows::ApplicationModel::Search::Core::SearchSuggestionKind result = wrapper->_instance->Kind;
        info.GetReturnValue().Set(Nan::New<Integer>(static_cast<int>(result)));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void TagGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestion^>(info.This())) {
        return;
      }

      SearchSuggestion *wrapper = SearchSuggestion::Unwrap<SearchSuggestion>(info.This());

      try  {
        Platform::String^ result = wrapper->_instance->Tag;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void TextGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestion^>(info.This())) {
        return;
      }

      SearchSuggestion *wrapper = SearchSuggestion::Unwrap<SearchSuggestion>(info.This());

      try  {
        Platform::String^ result = wrapper->_instance->Text;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      


    private:
      ::Windows::ApplicationModel::Search::Core::SearchSuggestion^ _instance;
      static Persistent<FunctionTemplate> s_constructorTemplate;

      friend v8::Local<v8::Value> WrapSearchSuggestion(::Windows::ApplicationModel::Search::Core::SearchSuggestion^ wintRtInstance);
      friend ::Windows::ApplicationModel::Search::Core::SearchSuggestion^ UnwrapSearchSuggestion(Local<Value> value);
  };

  Persistent<FunctionTemplate> SearchSuggestion::s_constructorTemplate;

  v8::Local<v8::Value> WrapSearchSuggestion(::Windows::ApplicationModel::Search::Core::SearchSuggestion^ 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>(SearchSuggestion::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::ApplicationModel::Search::Core::SearchSuggestion^ UnwrapSearchSuggestion(Local<Value> value) {
     return SearchSuggestion::Unwrap<SearchSuggestion>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitSearchSuggestion(Local<Object> exports) {
    SearchSuggestion::Init(exports);
  }

  class SearchSuggestionManager : 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>("SearchSuggestionManager").ToLocalChecked());
        localRef->InstanceTemplate()->SetInternalFieldCount(1);


          
            Nan::SetPrototypeMethod(localRef, "setLocalContentSuggestionSettings", SetLocalContentSuggestionSettings);
            Nan::SetPrototypeMethod(localRef, "setQuery", SetQuery);
            Nan::SetPrototypeMethod(localRef, "addToHistory", AddToHistory);
            Nan::SetPrototypeMethod(localRef, "clearHistory", ClearHistory);
          


          
          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>("searchHistoryEnabled").ToLocalChecked(), SearchHistoryEnabledGetter, SearchHistoryEnabledSetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("searchHistoryContext").ToLocalChecked(), SearchHistoryContextGetter, SearchHistoryContextSetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("suggestions").ToLocalChecked(), SuggestionsGetter);

        Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
        Nan::SetMethod(constructor, "castFrom", CastFrom);



        Nan::Set(exports, Nan::New<String>("SearchSuggestionManager").ToLocalChecked(), constructor);
      }

      virtual ::Platform::Object^ GetObjectInstance() const override {
        return _instance;
      }

    private:

      SearchSuggestionManager(::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^ 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::Search::Core::SearchSuggestionManager^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^>(info[0])) {
        try {
          winRtInstance = (::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^) NodeRT::Utils::GetObjectInstance(info[0]);
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 0)
      {
        try {
          winRtInstance = ref new ::Windows::ApplicationModel::Search::Core::SearchSuggestionManager();
        } 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());

      SearchSuggestionManager *wrapperInstance = new SearchSuggestionManager(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::Search::Core::SearchSuggestionManager^>(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::Search::Core::SearchSuggestionManager^ winRtInstance;
      try {
        winRtInstance = (::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^) NodeRT::Utils::GetObjectInstance(info[0]);
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }

      info.GetReturnValue().Set(WrapSearchSuggestionManager(winRtInstance));
    }


    static void SetLocalContentSuggestionSettings(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^>(info.This())) {
        return;
      }

      SearchSuggestionManager *wrapper = SearchSuggestionManager::Unwrap<SearchSuggestionManager>(info.This());

      if (info.Length() == 1
        && NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::LocalContentSuggestionSettings^>(info[0]))
      {
        try
        {
          ::Windows::ApplicationModel::Search::LocalContentSuggestionSettings^ arg0 = dynamic_cast<::Windows::ApplicationModel::Search::LocalContentSuggestionSettings^>(NodeRT::Utils::GetObjectInstance(info[0]));
          
          wrapper->_instance->SetLocalContentSuggestionSettings(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 SetQuery(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^>(info.This())) {
        return;
      }

      SearchSuggestionManager *wrapper = SearchSuggestionManager::Unwrap<SearchSuggestionManager>(info.This());

      if (info.Length() == 1
        && info[0]->IsString())
      {
        try
        {
          Platform::String^ arg0 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(v8::Isolate::GetCurrent(), info[0])));
          
          wrapper->_instance->SetQuery(arg0);
          return;
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 2
        && info[0]->IsString()
        && info[1]->IsString())
      {
        try
        {
          Platform::String^ arg0 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(v8::Isolate::GetCurrent(), info[0])));
          Platform::String^ arg1 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(v8::Isolate::GetCurrent(), info[1])));
          
          wrapper->_instance->SetQuery(arg0, arg1);
          return;
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 3
        && info[0]->IsString()
        && info[1]->IsString()
        && NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::SearchQueryLinguisticDetails^>(info[2]))
      {
        try
        {
          Platform::String^ arg0 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(v8::Isolate::GetCurrent(), info[0])));
          Platform::String^ arg1 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(v8::Isolate::GetCurrent(), info[1])));
          ::Windows::ApplicationModel::Search::SearchQueryLinguisticDetails^ arg2 = dynamic_cast<::Windows::ApplicationModel::Search::SearchQueryLinguisticDetails^>(NodeRT::Utils::GetObjectInstance(info[2]));
          
          wrapper->_instance->SetQuery(arg0, arg1, arg2);
          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 AddToHistory(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^>(info.This())) {
        return;
      }

      SearchSuggestionManager *wrapper = SearchSuggestionManager::Unwrap<SearchSuggestionManager>(info.This());

      if (info.Length() == 1
        && info[0]->IsString())
      {
        try
        {
          Platform::String^ arg0 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(v8::Isolate::GetCurrent(), info[0])));
          
          wrapper->_instance->AddToHistory(arg0);
          return;
        } catch (Platform::Exception ^exception) {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 2
        && info[0]->IsString()
        && info[1]->IsString())
      {
        try
        {
          Platform::String^ arg0 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(v8::Isolate::GetCurrent(), info[0])));
          Platform::String^ arg1 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(v8::Isolate::GetCurrent(), info[1])));
          
          wrapper->_instance->AddToHistory(arg0, arg1);
          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 ClearHistory(Nan::NAN_METHOD_ARGS_TYPE info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^>(info.This())) {
        return;
      }

      SearchSuggestionManager *wrapper = SearchSuggestionManager::Unwrap<SearchSuggestionManager>(info.This());

      if (info.Length() == 0)
      {
        try
        {
          wrapper->_instance->ClearHistory();
          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 SearchHistoryEnabledGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^>(info.This())) {
        return;
      }

      SearchSuggestionManager *wrapper = SearchSuggestionManager::Unwrap<SearchSuggestionManager>(info.This());

      try  {
        bool result = wrapper->_instance->SearchHistoryEnabled;
        info.GetReturnValue().Set(Nan::New<Boolean>(result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void SearchHistoryEnabledSetter(Local<String> property, Local<Value> value, const Nan::PropertyCallbackInfo<void> &info) {
      HandleScope scope;

      if (!value->IsBoolean()) {
        Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(L"Value to set is of unexpected type")));
        return;
      }

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^>(info.This())) {
        return;
      }

      SearchSuggestionManager *wrapper = SearchSuggestionManager::Unwrap<SearchSuggestionManager>(info.This());

      try {

        bool winRtValue = Nan::To<bool>(value).FromMaybe(false);

        wrapper->_instance->SearchHistoryEnabled = winRtValue;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
      }
    }
      
    static void SearchHistoryContextGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^>(info.This())) {
        return;
      }

      SearchSuggestionManager *wrapper = SearchSuggestionManager::Unwrap<SearchSuggestionManager>(info.This());

      try  {
        Platform::String^ result = wrapper->_instance->SearchHistoryContext;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void SearchHistoryContextSetter(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::ApplicationModel::Search::Core::SearchSuggestionManager^>(info.This())) {
        return;
      }

      SearchSuggestionManager *wrapper = SearchSuggestionManager::Unwrap<SearchSuggestionManager>(info.This());

      try {

        Platform::String^ winRtValue = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(v8::Isolate::GetCurrent(), value)));

        wrapper->_instance->SearchHistoryContext = winRtValue;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
      }
    }
      
    static void SuggestionsGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^>(info.This())) {
        return;
      }

      SearchSuggestionManager *wrapper = SearchSuggestionManager::Unwrap<SearchSuggestionManager>(info.This());

      try  {
        ::Windows::Foundation::Collections::IObservableVector<::Windows::ApplicationModel::Search::Core::SearchSuggestion^>^ result = wrapper->_instance->Suggestions;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.Foundation.Collections", "IObservableVector`1", 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"requestingFocusOnKeyboardInput", str))
      {
        if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^>(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;
        }
        SearchSuggestionManager *wrapper = SearchSuggestionManager::Unwrap<SearchSuggestionManager>(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->RequestingFocusOnKeyboardInput::add(
            ref new ::Windows::Foundation::TypedEventHandler<::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^, ::Windows::ApplicationModel::Search::Core::RequestingFocusOnKeyboardInputEventArgs^>(
            [callbackObjPtr](::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^ arg0, ::Windows::ApplicationModel::Search::Core::RequestingFocusOnKeyboardInputEventArgs^ arg1) {
              NodeUtils::Async::RunOnMain([callbackObjPtr , arg0, arg1]() {
                HandleScope scope;


                Local<Value> wrappedArg0;
                Local<Value> wrappedArg1;

                {
                  TryCatch tryCatch;


                  wrappedArg0 = WrapSearchSuggestionManager(arg0);
                  wrappedArg1 = WrapRequestingFocusOnKeyboardInputEventArgs(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"suggestionsRequested", str))
      {
        if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^>(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;
        }
        SearchSuggestionManager *wrapper = SearchSuggestionManager::Unwrap<SearchSuggestionManager>(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->SuggestionsRequested::add(
            ref new ::Windows::Foundation::TypedEventHandler<::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^, ::Windows::ApplicationModel::Search::Core::SearchSuggestionsRequestedEventArgs^>(
            [callbackObjPtr](::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^ arg0, ::Windows::ApplicationModel::Search::Core::SearchSuggestionsRequestedEventArgs^ arg1) {
              NodeUtils::Async::RunOnMain([callbackObjPtr , arg0, arg1]() {
                HandleScope scope;


                Local<Value> wrappedArg0;
                Local<Value> wrappedArg1;

                {
                  TryCatch tryCatch;


                  wrappedArg0 = WrapSearchSuggestionManager(arg0);
                  wrappedArg1 = WrapSearchSuggestionsRequestedEventArgs(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"requestingFocusOnKeyboardInput", str)) &&(!NodeRT::Utils::CaseInsenstiveEquals(L"suggestionsRequested", 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"requestingFocusOnKeyboardInput", str)) {
          if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^>(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;
          }
          SearchSuggestionManager *wrapper = SearchSuggestionManager::Unwrap<SearchSuggestionManager>(info.This());
          wrapper->_instance->RequestingFocusOnKeyboardInput::remove(registrationToken);
        }
        else if (NodeRT::Utils::CaseInsenstiveEquals(L"suggestionsRequested", str))
        {
          if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^>(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;
          }
          SearchSuggestionManager *wrapper = SearchSuggestionManager::Unwrap<SearchSuggestionManager>(info.This());
          wrapper->_instance->SuggestionsRequested::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::ApplicationModel::Search::Core::SearchSuggestionManager^ _instance;
      static Persistent<FunctionTemplate> s_constructorTemplate;

      friend v8::Local<v8::Value> WrapSearchSuggestionManager(::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^ wintRtInstance);
      friend ::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^ UnwrapSearchSuggestionManager(Local<Value> value);
  };

  Persistent<FunctionTemplate> SearchSuggestionManager::s_constructorTemplate;

  v8::Local<v8::Value> WrapSearchSuggestionManager(::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^ 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>(SearchSuggestionManager::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::ApplicationModel::Search::Core::SearchSuggestionManager^ UnwrapSearchSuggestionManager(Local<Value> value) {
     return SearchSuggestionManager::Unwrap<SearchSuggestionManager>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitSearchSuggestionManager(Local<Object> exports) {
    SearchSuggestionManager::Init(exports);
  }

  class SearchSuggestionsRequestedEventArgs : 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>("SearchSuggestionsRequestedEventArgs").ToLocalChecked());
        localRef->InstanceTemplate()->SetInternalFieldCount(1);





          
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("language").ToLocalChecked(), LanguageGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("linguisticDetails").ToLocalChecked(), LinguisticDetailsGetter);
            Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("queryText").ToLocalChecked(), QueryTextGetter);
            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>("SearchSuggestionsRequestedEventArgs").ToLocalChecked(), constructor);
      }

      virtual ::Platform::Object^ GetObjectInstance() const override {
        return _instance;
      }

    private:

      SearchSuggestionsRequestedEventArgs(::Windows::ApplicationModel::Search::Core::SearchSuggestionsRequestedEventArgs^ 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::Search::Core::SearchSuggestionsRequestedEventArgs^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestionsRequestedEventArgs^>(info[0])) {
        try {
          winRtInstance = (::Windows::ApplicationModel::Search::Core::SearchSuggestionsRequestedEventArgs^) 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());

      SearchSuggestionsRequestedEventArgs *wrapperInstance = new SearchSuggestionsRequestedEventArgs(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::Search::Core::SearchSuggestionsRequestedEventArgs^>(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::Search::Core::SearchSuggestionsRequestedEventArgs^ winRtInstance;
      try {
        winRtInstance = (::Windows::ApplicationModel::Search::Core::SearchSuggestionsRequestedEventArgs^) NodeRT::Utils::GetObjectInstance(info[0]);
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }

      info.GetReturnValue().Set(WrapSearchSuggestionsRequestedEventArgs(winRtInstance));
    }





    static void LanguageGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestionsRequestedEventArgs^>(info.This())) {
        return;
      }

      SearchSuggestionsRequestedEventArgs *wrapper = SearchSuggestionsRequestedEventArgs::Unwrap<SearchSuggestionsRequestedEventArgs>(info.This());

      try  {
        Platform::String^ result = wrapper->_instance->Language;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void LinguisticDetailsGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestionsRequestedEventArgs^>(info.This())) {
        return;
      }

      SearchSuggestionsRequestedEventArgs *wrapper = SearchSuggestionsRequestedEventArgs::Unwrap<SearchSuggestionsRequestedEventArgs>(info.This());

      try  {
        ::Windows::ApplicationModel::Search::SearchQueryLinguisticDetails^ result = wrapper->_instance->LinguisticDetails;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.ApplicationModel.Search", "SearchQueryLinguisticDetails", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void QueryTextGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestionsRequestedEventArgs^>(info.This())) {
        return;
      }

      SearchSuggestionsRequestedEventArgs *wrapper = SearchSuggestionsRequestedEventArgs::Unwrap<SearchSuggestionsRequestedEventArgs>(info.This());

      try  {
        Platform::String^ result = wrapper->_instance->QueryText;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      
    static void RequestGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info) {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::SearchSuggestionsRequestedEventArgs^>(info.This())) {
        return;
      }

      SearchSuggestionsRequestedEventArgs *wrapper = SearchSuggestionsRequestedEventArgs::Unwrap<SearchSuggestionsRequestedEventArgs>(info.This());

      try  {
        ::Windows::ApplicationModel::Search::SearchSuggestionsRequest^ result = wrapper->_instance->Request;
        info.GetReturnValue().Set(NodeRT::Utils::CreateExternalWinRTObject("Windows.ApplicationModel.Search", "SearchSuggestionsRequest", result));
        return;
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
      


    private:
      ::Windows::ApplicationModel::Search::Core::SearchSuggestionsRequestedEventArgs^ _instance;
      static Persistent<FunctionTemplate> s_constructorTemplate;

      friend v8::Local<v8::Value> WrapSearchSuggestionsRequestedEventArgs(::Windows::ApplicationModel::Search::Core::SearchSuggestionsRequestedEventArgs^ wintRtInstance);
      friend ::Windows::ApplicationModel::Search::Core::SearchSuggestionsRequestedEventArgs^ UnwrapSearchSuggestionsRequestedEventArgs(Local<Value> value);
  };

  Persistent<FunctionTemplate> SearchSuggestionsRequestedEventArgs::s_constructorTemplate;

  v8::Local<v8::Value> WrapSearchSuggestionsRequestedEventArgs(::Windows::ApplicationModel::Search::Core::SearchSuggestionsRequestedEventArgs^ 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>(SearchSuggestionsRequestedEventArgs::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::ApplicationModel::Search::Core::SearchSuggestionsRequestedEventArgs^ UnwrapSearchSuggestionsRequestedEventArgs(Local<Value> value) {
     return SearchSuggestionsRequestedEventArgs::Unwrap<SearchSuggestionsRequestedEventArgs>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitSearchSuggestionsRequestedEventArgs(Local<Object> exports) {
    SearchSuggestionsRequestedEventArgs::Init(exports);
  }

  class RequestingFocusOnKeyboardInputEventArgs : 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>("RequestingFocusOnKeyboardInputEventArgs").ToLocalChecked());
        localRef->InstanceTemplate()->SetInternalFieldCount(1);






        Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
        Nan::SetMethod(constructor, "castFrom", CastFrom);



        Nan::Set(exports, Nan::New<String>("RequestingFocusOnKeyboardInputEventArgs").ToLocalChecked(), constructor);
      }

      virtual ::Platform::Object^ GetObjectInstance() const override {
        return _instance;
      }

    private:

      RequestingFocusOnKeyboardInputEventArgs(::Windows::ApplicationModel::Search::Core::RequestingFocusOnKeyboardInputEventArgs^ 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::Search::Core::RequestingFocusOnKeyboardInputEventArgs^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::ApplicationModel::Search::Core::RequestingFocusOnKeyboardInputEventArgs^>(info[0])) {
        try {
          winRtInstance = (::Windows::ApplicationModel::Search::Core::RequestingFocusOnKeyboardInputEventArgs^) 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());

      RequestingFocusOnKeyboardInputEventArgs *wrapperInstance = new RequestingFocusOnKeyboardInputEventArgs(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::Search::Core::RequestingFocusOnKeyboardInputEventArgs^>(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::Search::Core::RequestingFocusOnKeyboardInputEventArgs^ winRtInstance;
      try {
        winRtInstance = (::Windows::ApplicationModel::Search::Core::RequestingFocusOnKeyboardInputEventArgs^) NodeRT::Utils::GetObjectInstance(info[0]);
      } catch (Platform::Exception ^exception) {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }

      info.GetReturnValue().Set(WrapRequestingFocusOnKeyboardInputEventArgs(winRtInstance));
    }







    private:
      ::Windows::ApplicationModel::Search::Core::RequestingFocusOnKeyboardInputEventArgs^ _instance;
      static Persistent<FunctionTemplate> s_constructorTemplate;

      friend v8::Local<v8::Value> WrapRequestingFocusOnKeyboardInputEventArgs(::Windows::ApplicationModel::Search::Core::RequestingFocusOnKeyboardInputEventArgs^ wintRtInstance);
      friend ::Windows::ApplicationModel::Search::Core::RequestingFocusOnKeyboardInputEventArgs^ UnwrapRequestingFocusOnKeyboardInputEventArgs(Local<Value> value);
  };

  Persistent<FunctionTemplate> RequestingFocusOnKeyboardInputEventArgs::s_constructorTemplate;

  v8::Local<v8::Value> WrapRequestingFocusOnKeyboardInputEventArgs(::Windows::ApplicationModel::Search::Core::RequestingFocusOnKeyboardInputEventArgs^ 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>(RequestingFocusOnKeyboardInputEventArgs::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::ApplicationModel::Search::Core::RequestingFocusOnKeyboardInputEventArgs^ UnwrapRequestingFocusOnKeyboardInputEventArgs(Local<Value> value) {
     return RequestingFocusOnKeyboardInputEventArgs::Unwrap<RequestingFocusOnKeyboardInputEventArgs>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitRequestingFocusOnKeyboardInputEventArgs(Local<Object> exports) {
    RequestingFocusOnKeyboardInputEventArgs::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::Search::Core::InitSearchSuggestionKindEnum(target);
      NodeRT::Windows::ApplicationModel::Search::Core::InitSearchSuggestion(target);
      NodeRT::Windows::ApplicationModel::Search::Core::InitSearchSuggestionManager(target);
      NodeRT::Windows::ApplicationModel::Search::Core::InitSearchSuggestionsRequestedEventArgs(target);
      NodeRT::Windows::ApplicationModel::Search::Core::InitRequestingFocusOnKeyboardInputEventArgs(target);


  NodeRT::Utils::RegisterNameSpace("Windows.ApplicationModel.Search.Core", target);
}



NODE_MODULE(binding, init)
