// Copyright (c) Microsoft Corporation
// All rights reserved. 
//
// Licensed under the Apache License, Version 2.0 (the ""License""); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
// THIS CODE IS PROVIDED ON AN  *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. 
//
// See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.

// TODO: Verify that this is is still needed..
#define NTDDI_VERSION 0x06010000

#include <v8.h>
#include "nan.h"
#include <string>
#include <ppltasks.h>
#include "CollectionsConverter.h"
#include "CollectionsWrap.h"
#include "node-async.h"
#include "NodeRtUtils.h"
#include "OpaqueWrapper.h"
#include "WrapperBase.h"

#using <Windows.WinMD>

// this undefs fixes the issues of compiling Windows.Data.Json, Windows.Storag.FileProperties, and Windows.Stroage.Search
// Some of the node header files brings windows definitions with the same names as some of the WinRT methods
#undef DocumentProperties
#undef GetObject
#undef CreateEvent
#undef FindText
#undef SendMessage

const char* REGISTRATION_TOKEN_MAP_PROPERTY_NAME = "__registrationTokenMap__";

using v8::Array;
using v8::String;
using v8::Handle;
using v8::Value;
using v8::Boolean;
using v8::Integer;
using v8::FunctionTemplate;
using v8::Object;
using v8::Local;
using v8::Function;
using v8::Date;
using v8::Number;
using v8::PropertyAttribute;
using v8::Primitive;
using Nan::HandleScope;
using Nan::Persistent;
using Nan::Undefined;
using Nan::True;
using Nan::False;
using Nan::Null;
using Nan::MaybeLocal;
using Nan::EscapableHandleScope;
using Nan::HandleScope;
using Nan::TryCatch;
using namespace concurrency;

namespace NodeRT { namespace Windows { namespace Globalization { namespace Fonts { 

  v8::Local<v8::Value> WrapLanguageFont(::Windows::Globalization::Fonts::LanguageFont^ wintRtInstance);
  ::Windows::Globalization::Fonts::LanguageFont^ UnwrapLanguageFont(Local<Value> value);
  
  v8::Local<v8::Value> WrapLanguageFontGroup(::Windows::Globalization::Fonts::LanguageFontGroup^ wintRtInstance);
  ::Windows::Globalization::Fonts::LanguageFontGroup^ UnwrapLanguageFontGroup(Local<Value> value);
  



  
  static bool IsFontWeightJsObject(Local<Value> value)
  {
    if (!value->IsObject())
    {
      return false;
    }

    Local<String> symbol;
    Local<Object> obj = Nan::To<Object>(value).ToLocalChecked();

    symbol = Nan::New<String>("weight").ToLocalChecked();
    if (Nan::Has(obj, symbol).FromMaybe(false))
    {
      if (!Nan::Get(obj,symbol).ToLocalChecked()->IsInt32())
      {
          return false;
      }
    }
    
    return true;
  }

  ::Windows::UI::Text::FontWeight FontWeightFromJsObject(Local<Value> value)
  {
    HandleScope scope;
    ::Windows::UI::Text::FontWeight 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;

    symbol = Nan::New<String>("weight").ToLocalChecked();
    if (Nan::Has(obj, symbol).FromMaybe(false))
    {
      returnValue.Weight = static_cast<unsigned short>(Nan::To<int32_t>(Nan::Get(obj,symbol).ToLocalChecked()).FromMaybe(0));
    }
    
    return returnValue;
  }

  Local<Value> FontWeightToJsObject(::Windows::UI::Text::FontWeight value)
  {
    EscapableHandleScope scope;

    Local<Object> obj = Nan::New<Object>();

    Nan::Set(obj, Nan::New<String>("weight").ToLocalChecked(), Nan::New<Integer>(value.Weight));
    
    return scope.Escape(obj);
  }

  
  class LanguageFont : 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>("LanguageFont").ToLocalChecked());
      localRef->InstanceTemplate()->SetInternalFieldCount(1);
      
                              
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("fontFamily").ToLocalChecked(), FontFamilyGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("fontStretch").ToLocalChecked(), FontStretchGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("fontStyle").ToLocalChecked(), FontStyleGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("fontWeight").ToLocalChecked(), FontWeightGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("scaleFactor").ToLocalChecked(), ScaleFactorGetter);
      
      Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
	  Nan::SetMethod(constructor, "castFrom", CastFrom);


      Nan::Set(exports, Nan::New<String>("LanguageFont").ToLocalChecked(), constructor);
    }


    virtual ::Platform::Object^ GetObjectInstance() const override
    {
      return _instance;
    }

  private:
    
    LanguageFont(::Windows::Globalization::Fonts::LanguageFont^ 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::Globalization::Fonts::LanguageFont^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::Fonts::LanguageFont^>(info[0]))
      {
        try 
        {
          winRtInstance = (::Windows::Globalization::Fonts::LanguageFont^) 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());

      LanguageFont *wrapperInstance = new LanguageFont(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::Globalization::Fonts::LanguageFont^>(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::Globalization::Fonts::LanguageFont^ winRtInstance;
		try
		{
			winRtInstance = (::Windows::Globalization::Fonts::LanguageFont^) NodeRT::Utils::GetObjectInstance(info[0]);
		}
		catch (Platform::Exception ^exception)
		{
			NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
			return;
		}

		info.GetReturnValue().Set(WrapLanguageFont(winRtInstance));
    }


  



    static void FontFamilyGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::Fonts::LanguageFont^>(info.This()))
      {
        return;
      }

      LanguageFont *wrapper = LanguageFont::Unwrap<LanguageFont>(info.This());

      try 
      {
        Platform::String^ result = wrapper->_instance->FontFamily;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void FontStretchGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::Fonts::LanguageFont^>(info.This()))
      {
        return;
      }

      LanguageFont *wrapper = LanguageFont::Unwrap<LanguageFont>(info.This());

      try 
      {
        ::Windows::UI::Text::FontStretch result = wrapper->_instance->FontStretch;
        info.GetReturnValue().Set(Nan::New<Integer>(static_cast<int>(result)));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void FontStyleGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::Fonts::LanguageFont^>(info.This()))
      {
        return;
      }

      LanguageFont *wrapper = LanguageFont::Unwrap<LanguageFont>(info.This());

      try 
      {
        ::Windows::UI::Text::FontStyle result = wrapper->_instance->FontStyle;
        info.GetReturnValue().Set(Nan::New<Integer>(static_cast<int>(result)));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void FontWeightGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::Fonts::LanguageFont^>(info.This()))
      {
        return;
      }

      LanguageFont *wrapper = LanguageFont::Unwrap<LanguageFont>(info.This());

      try 
      {
        ::Windows::UI::Text::FontWeight result = wrapper->_instance->FontWeight;
        info.GetReturnValue().Set(FontWeightToJsObject(result));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void ScaleFactorGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::Fonts::LanguageFont^>(info.This()))
      {
        return;
      }

      LanguageFont *wrapper = LanguageFont::Unwrap<LanguageFont>(info.This());

      try 
      {
        double result = wrapper->_instance->ScaleFactor;
        info.GetReturnValue().Set(Nan::New<Number>(static_cast<double>(result)));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    


  private:
    ::Windows::Globalization::Fonts::LanguageFont^ _instance;
    static Persistent<FunctionTemplate> s_constructorTemplate;

    friend v8::Local<v8::Value> WrapLanguageFont(::Windows::Globalization::Fonts::LanguageFont^ wintRtInstance);
    friend ::Windows::Globalization::Fonts::LanguageFont^ UnwrapLanguageFont(Local<Value> value);
  };
  Persistent<FunctionTemplate> LanguageFont::s_constructorTemplate;

  v8::Local<v8::Value> WrapLanguageFont(::Windows::Globalization::Fonts::LanguageFont^ 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>(LanguageFont::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::Globalization::Fonts::LanguageFont^ UnwrapLanguageFont(Local<Value> value)
  {
     return LanguageFont::Unwrap<LanguageFont>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitLanguageFont(Local<Object> exports)
  {
    LanguageFont::Init(exports);
  }

  class LanguageFontGroup : 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>("LanguageFontGroup").ToLocalChecked());
      localRef->InstanceTemplate()->SetInternalFieldCount(1);
      
                              
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("documentAlternate1Font").ToLocalChecked(), DocumentAlternate1FontGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("documentAlternate2Font").ToLocalChecked(), DocumentAlternate2FontGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("documentHeadingFont").ToLocalChecked(), DocumentHeadingFontGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("fixedWidthTextFont").ToLocalChecked(), FixedWidthTextFontGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("modernDocumentFont").ToLocalChecked(), ModernDocumentFontGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("traditionalDocumentFont").ToLocalChecked(), TraditionalDocumentFontGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("uICaptionFont").ToLocalChecked(), UICaptionFontGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("uIHeadingFont").ToLocalChecked(), UIHeadingFontGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("uINotificationHeadingFont").ToLocalChecked(), UINotificationHeadingFontGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("uITextFont").ToLocalChecked(), UITextFontGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("uITitleFont").ToLocalChecked(), UITitleFontGetter);
      
      Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
	  Nan::SetMethod(constructor, "castFrom", CastFrom);


      Nan::Set(exports, Nan::New<String>("LanguageFontGroup").ToLocalChecked(), constructor);
    }


    virtual ::Platform::Object^ GetObjectInstance() const override
    {
      return _instance;
    }

  private:
    
    LanguageFontGroup(::Windows::Globalization::Fonts::LanguageFontGroup^ 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::Globalization::Fonts::LanguageFontGroup^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::Fonts::LanguageFontGroup^>(info[0]))
      {
        try 
        {
          winRtInstance = (::Windows::Globalization::Fonts::LanguageFontGroup^) NodeRT::Utils::GetObjectInstance(info[0]);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 1
        && info[0]->IsString())
      {
        try
        {
          Platform::String^ arg0 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(info[0])));
          
          winRtInstance = ref new ::Windows::Globalization::Fonts::LanguageFontGroup(arg0);
        }
        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());

      LanguageFontGroup *wrapperInstance = new LanguageFontGroup(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::Globalization::Fonts::LanguageFontGroup^>(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::Globalization::Fonts::LanguageFontGroup^ winRtInstance;
		try
		{
			winRtInstance = (::Windows::Globalization::Fonts::LanguageFontGroup^) NodeRT::Utils::GetObjectInstance(info[0]);
		}
		catch (Platform::Exception ^exception)
		{
			NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
			return;
		}

		info.GetReturnValue().Set(WrapLanguageFontGroup(winRtInstance));
    }


  



    static void DocumentAlternate1FontGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::Fonts::LanguageFontGroup^>(info.This()))
      {
        return;
      }

      LanguageFontGroup *wrapper = LanguageFontGroup::Unwrap<LanguageFontGroup>(info.This());

      try 
      {
        ::Windows::Globalization::Fonts::LanguageFont^ result = wrapper->_instance->DocumentAlternate1Font;
        info.GetReturnValue().Set(WrapLanguageFont(result));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void DocumentAlternate2FontGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::Fonts::LanguageFontGroup^>(info.This()))
      {
        return;
      }

      LanguageFontGroup *wrapper = LanguageFontGroup::Unwrap<LanguageFontGroup>(info.This());

      try 
      {
        ::Windows::Globalization::Fonts::LanguageFont^ result = wrapper->_instance->DocumentAlternate2Font;
        info.GetReturnValue().Set(WrapLanguageFont(result));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void DocumentHeadingFontGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::Fonts::LanguageFontGroup^>(info.This()))
      {
        return;
      }

      LanguageFontGroup *wrapper = LanguageFontGroup::Unwrap<LanguageFontGroup>(info.This());

      try 
      {
        ::Windows::Globalization::Fonts::LanguageFont^ result = wrapper->_instance->DocumentHeadingFont;
        info.GetReturnValue().Set(WrapLanguageFont(result));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void FixedWidthTextFontGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::Fonts::LanguageFontGroup^>(info.This()))
      {
        return;
      }

      LanguageFontGroup *wrapper = LanguageFontGroup::Unwrap<LanguageFontGroup>(info.This());

      try 
      {
        ::Windows::Globalization::Fonts::LanguageFont^ result = wrapper->_instance->FixedWidthTextFont;
        info.GetReturnValue().Set(WrapLanguageFont(result));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void ModernDocumentFontGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::Fonts::LanguageFontGroup^>(info.This()))
      {
        return;
      }

      LanguageFontGroup *wrapper = LanguageFontGroup::Unwrap<LanguageFontGroup>(info.This());

      try 
      {
        ::Windows::Globalization::Fonts::LanguageFont^ result = wrapper->_instance->ModernDocumentFont;
        info.GetReturnValue().Set(WrapLanguageFont(result));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void TraditionalDocumentFontGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::Fonts::LanguageFontGroup^>(info.This()))
      {
        return;
      }

      LanguageFontGroup *wrapper = LanguageFontGroup::Unwrap<LanguageFontGroup>(info.This());

      try 
      {
        ::Windows::Globalization::Fonts::LanguageFont^ result = wrapper->_instance->TraditionalDocumentFont;
        info.GetReturnValue().Set(WrapLanguageFont(result));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void UICaptionFontGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::Fonts::LanguageFontGroup^>(info.This()))
      {
        return;
      }

      LanguageFontGroup *wrapper = LanguageFontGroup::Unwrap<LanguageFontGroup>(info.This());

      try 
      {
        ::Windows::Globalization::Fonts::LanguageFont^ result = wrapper->_instance->UICaptionFont;
        info.GetReturnValue().Set(WrapLanguageFont(result));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void UIHeadingFontGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::Fonts::LanguageFontGroup^>(info.This()))
      {
        return;
      }

      LanguageFontGroup *wrapper = LanguageFontGroup::Unwrap<LanguageFontGroup>(info.This());

      try 
      {
        ::Windows::Globalization::Fonts::LanguageFont^ result = wrapper->_instance->UIHeadingFont;
        info.GetReturnValue().Set(WrapLanguageFont(result));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void UINotificationHeadingFontGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::Fonts::LanguageFontGroup^>(info.This()))
      {
        return;
      }

      LanguageFontGroup *wrapper = LanguageFontGroup::Unwrap<LanguageFontGroup>(info.This());

      try 
      {
        ::Windows::Globalization::Fonts::LanguageFont^ result = wrapper->_instance->UINotificationHeadingFont;
        info.GetReturnValue().Set(WrapLanguageFont(result));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void UITextFontGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::Fonts::LanguageFontGroup^>(info.This()))
      {
        return;
      }

      LanguageFontGroup *wrapper = LanguageFontGroup::Unwrap<LanguageFontGroup>(info.This());

      try 
      {
        ::Windows::Globalization::Fonts::LanguageFont^ result = wrapper->_instance->UITextFont;
        info.GetReturnValue().Set(WrapLanguageFont(result));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void UITitleFontGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::Fonts::LanguageFontGroup^>(info.This()))
      {
        return;
      }

      LanguageFontGroup *wrapper = LanguageFontGroup::Unwrap<LanguageFontGroup>(info.This());

      try 
      {
        ::Windows::Globalization::Fonts::LanguageFont^ result = wrapper->_instance->UITitleFont;
        info.GetReturnValue().Set(WrapLanguageFont(result));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    


  private:
    ::Windows::Globalization::Fonts::LanguageFontGroup^ _instance;
    static Persistent<FunctionTemplate> s_constructorTemplate;

    friend v8::Local<v8::Value> WrapLanguageFontGroup(::Windows::Globalization::Fonts::LanguageFontGroup^ wintRtInstance);
    friend ::Windows::Globalization::Fonts::LanguageFontGroup^ UnwrapLanguageFontGroup(Local<Value> value);
  };
  Persistent<FunctionTemplate> LanguageFontGroup::s_constructorTemplate;

  v8::Local<v8::Value> WrapLanguageFontGroup(::Windows::Globalization::Fonts::LanguageFontGroup^ 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>(LanguageFontGroup::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::Globalization::Fonts::LanguageFontGroup^ UnwrapLanguageFontGroup(Local<Value> value)
  {
     return LanguageFontGroup::Unwrap<LanguageFontGroup>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitLanguageFontGroup(Local<Object> exports)
  {
    LanguageFontGroup::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::Globalization::Fonts::InitLanguageFont(target);
  NodeRT::Windows::Globalization::Fonts::InitLanguageFontGroup(target);

  NodeRT::Utils::RegisterNameSpace("Windows.Globalization.Fonts", target);
}


NODE_MODULE(binding, init)