// 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 DateTimeFormatting { 

  v8::Local<v8::Value> WrapDateTimeFormatter(::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ wintRtInstance);
  ::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ UnwrapDateTimeFormatter(Local<Value> value);
  


  static void InitYearFormatEnum(const Local<Object> exports)
  {
    HandleScope scope;
    
	Local<Object> enumObject = Nan::New<Object>();
    Nan::Set(exports, Nan::New<String>("YearFormat").ToLocalChecked(), enumObject);
	Nan::Set(enumObject, Nan::New<String>("none").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Globalization::DateTimeFormatting::YearFormat::None)));
	Nan::Set(enumObject, Nan::New<String>("default").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Globalization::DateTimeFormatting::YearFormat::Default)));
	Nan::Set(enumObject, Nan::New<String>("abbreviated").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Globalization::DateTimeFormatting::YearFormat::Abbreviated)));
	Nan::Set(enumObject, Nan::New<String>("full").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Globalization::DateTimeFormatting::YearFormat::Full)));
  }


  static void InitMonthFormatEnum(const Local<Object> exports)
  {
    HandleScope scope;
    
	Local<Object> enumObject = Nan::New<Object>();
    Nan::Set(exports, Nan::New<String>("MonthFormat").ToLocalChecked(), enumObject);
	Nan::Set(enumObject, Nan::New<String>("none").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Globalization::DateTimeFormatting::MonthFormat::None)));
	Nan::Set(enumObject, Nan::New<String>("default").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Globalization::DateTimeFormatting::MonthFormat::Default)));
	Nan::Set(enumObject, Nan::New<String>("abbreviated").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Globalization::DateTimeFormatting::MonthFormat::Abbreviated)));
	Nan::Set(enumObject, Nan::New<String>("full").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Globalization::DateTimeFormatting::MonthFormat::Full)));
	Nan::Set(enumObject, Nan::New<String>("numeric").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Globalization::DateTimeFormatting::MonthFormat::Numeric)));
  }


  static void InitDayOfWeekFormatEnum(const Local<Object> exports)
  {
    HandleScope scope;
    
	Local<Object> enumObject = Nan::New<Object>();
    Nan::Set(exports, Nan::New<String>("DayOfWeekFormat").ToLocalChecked(), enumObject);
	Nan::Set(enumObject, Nan::New<String>("none").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Globalization::DateTimeFormatting::DayOfWeekFormat::None)));
	Nan::Set(enumObject, Nan::New<String>("default").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Globalization::DateTimeFormatting::DayOfWeekFormat::Default)));
	Nan::Set(enumObject, Nan::New<String>("abbreviated").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Globalization::DateTimeFormatting::DayOfWeekFormat::Abbreviated)));
	Nan::Set(enumObject, Nan::New<String>("full").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Globalization::DateTimeFormatting::DayOfWeekFormat::Full)));
  }


  static void InitDayFormatEnum(const Local<Object> exports)
  {
    HandleScope scope;
    
	Local<Object> enumObject = Nan::New<Object>();
    Nan::Set(exports, Nan::New<String>("DayFormat").ToLocalChecked(), enumObject);
	Nan::Set(enumObject, Nan::New<String>("none").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Globalization::DateTimeFormatting::DayFormat::None)));
	Nan::Set(enumObject, Nan::New<String>("default").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Globalization::DateTimeFormatting::DayFormat::Default)));
  }


  static void InitHourFormatEnum(const Local<Object> exports)
  {
    HandleScope scope;
    
	Local<Object> enumObject = Nan::New<Object>();
    Nan::Set(exports, Nan::New<String>("HourFormat").ToLocalChecked(), enumObject);
	Nan::Set(enumObject, Nan::New<String>("none").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Globalization::DateTimeFormatting::HourFormat::None)));
	Nan::Set(enumObject, Nan::New<String>("default").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Globalization::DateTimeFormatting::HourFormat::Default)));
  }


  static void InitMinuteFormatEnum(const Local<Object> exports)
  {
    HandleScope scope;
    
	Local<Object> enumObject = Nan::New<Object>();
    Nan::Set(exports, Nan::New<String>("MinuteFormat").ToLocalChecked(), enumObject);
	Nan::Set(enumObject, Nan::New<String>("none").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Globalization::DateTimeFormatting::MinuteFormat::None)));
	Nan::Set(enumObject, Nan::New<String>("default").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Globalization::DateTimeFormatting::MinuteFormat::Default)));
  }


  static void InitSecondFormatEnum(const Local<Object> exports)
  {
    HandleScope scope;
    
	Local<Object> enumObject = Nan::New<Object>();
    Nan::Set(exports, Nan::New<String>("SecondFormat").ToLocalChecked(), enumObject);
	Nan::Set(enumObject, Nan::New<String>("none").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Globalization::DateTimeFormatting::SecondFormat::None)));
	Nan::Set(enumObject, Nan::New<String>("default").ToLocalChecked(), Nan::New<Integer>(static_cast<int>(::Windows::Globalization::DateTimeFormatting::SecondFormat::Default)));
  }



  
  class DateTimeFormatter : 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>("DateTimeFormatter").ToLocalChecked());
      localRef->InstanceTemplate()->SetInternalFieldCount(1);
      
            
      Nan::SetPrototypeMethod(localRef, "format", Format);
      
                        
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("numeralSystem").ToLocalChecked(), NumeralSystemGetter, NumeralSystemSetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("clock").ToLocalChecked(), ClockGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("geographicRegion").ToLocalChecked(), GeographicRegionGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("includeDay").ToLocalChecked(), IncludeDayGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("includeDayOfWeek").ToLocalChecked(), IncludeDayOfWeekGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("includeHour").ToLocalChecked(), IncludeHourGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("includeMinute").ToLocalChecked(), IncludeMinuteGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("includeMonth").ToLocalChecked(), IncludeMonthGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("includeSecond").ToLocalChecked(), IncludeSecondGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("includeYear").ToLocalChecked(), IncludeYearGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("languages").ToLocalChecked(), LanguagesGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("calendar").ToLocalChecked(), CalendarGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("patterns").ToLocalChecked(), PatternsGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("resolvedGeographicRegion").ToLocalChecked(), ResolvedGeographicRegionGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("resolvedLanguage").ToLocalChecked(), ResolvedLanguageGetter);
      Nan::SetAccessor(localRef->PrototypeTemplate(), Nan::New<String>("template").ToLocalChecked(), TemplateGetter);
      
      Local<Object> constructor = Nan::To<Object>(Nan::GetFunction(localRef).ToLocalChecked()).ToLocalChecked();
	  Nan::SetMethod(constructor, "castFrom", CastFrom);

      Nan::SetAccessor(constructor, Nan::New<String>("longDate").ToLocalChecked(), LongDateGetter);
      Nan::SetAccessor(constructor, Nan::New<String>("longTime").ToLocalChecked(), LongTimeGetter);
      Nan::SetAccessor(constructor, Nan::New<String>("shortDate").ToLocalChecked(), ShortDateGetter);
      Nan::SetAccessor(constructor, Nan::New<String>("shortTime").ToLocalChecked(), ShortTimeGetter);

      Nan::Set(exports, Nan::New<String>("DateTimeFormatter").ToLocalChecked(), constructor);
    }


    virtual ::Platform::Object^ GetObjectInstance() const override
    {
      return _instance;
    }

  private:
    
    DateTimeFormatter(::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ 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::DateTimeFormatting::DateTimeFormatter^ winRtInstance;


      if (info.Length() == 1 && OpaqueWrapper::IsOpaqueWrapper(info[0]) &&
        NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^>(info[0]))
      {
        try 
        {
          winRtInstance = (::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^) 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::DateTimeFormatting::DateTimeFormatter(arg0);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 2
        && info[0]->IsString()
        && (NodeRT::Utils::IsWinRtWrapperOf<::Windows::Foundation::Collections::IIterable<::Platform::String^>^>(info[1]) || info[1]->IsArray()))
      {
        try
        {
          Platform::String^ arg0 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(info[0])));
          ::Windows::Foundation::Collections::IIterable<::Platform::String^>^ arg1 = 
            [] (v8::Local<v8::Value> value) -> ::Windows::Foundation::Collections::IIterable<::Platform::String^>^
            {
              if (value->IsArray())
              {
                return NodeRT::Collections::JsArrayToWinrtVector<::Platform::String^>(value.As<Array>(), 
                 [](Local<Value> value) -> bool {
                   return (!NodeRT::Utils::IsWinRtWrapper(value));
                 },
                 [](Local<Value> value) -> ::Platform::String^ {
                   return ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(value)));
                 }
                );
              }
              else
              {
                return dynamic_cast<::Windows::Foundation::Collections::IIterable<::Platform::String^>^>(NodeRT::Utils::GetObjectInstance(value));
              }
            } (info[1]);
          
          winRtInstance = ref new ::Windows::Globalization::DateTimeFormatting::DateTimeFormatter(arg0,arg1);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 5
        && info[0]->IsString()
        && (NodeRT::Utils::IsWinRtWrapperOf<::Windows::Foundation::Collections::IIterable<::Platform::String^>^>(info[1]) || info[1]->IsArray())
        && info[2]->IsString()
        && info[3]->IsString()
        && info[4]->IsString())
      {
        try
        {
          Platform::String^ arg0 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(info[0])));
          ::Windows::Foundation::Collections::IIterable<::Platform::String^>^ arg1 = 
            [] (v8::Local<v8::Value> value) -> ::Windows::Foundation::Collections::IIterable<::Platform::String^>^
            {
              if (value->IsArray())
              {
                return NodeRT::Collections::JsArrayToWinrtVector<::Platform::String^>(value.As<Array>(), 
                 [](Local<Value> value) -> bool {
                   return (!NodeRT::Utils::IsWinRtWrapper(value));
                 },
                 [](Local<Value> value) -> ::Platform::String^ {
                   return ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(value)));
                 }
                );
              }
              else
              {
                return dynamic_cast<::Windows::Foundation::Collections::IIterable<::Platform::String^>^>(NodeRT::Utils::GetObjectInstance(value));
              }
            } (info[1]);
          Platform::String^ arg2 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(info[2])));
          Platform::String^ arg3 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(info[3])));
          Platform::String^ arg4 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(info[4])));
          
          winRtInstance = ref new ::Windows::Globalization::DateTimeFormatting::DateTimeFormatter(arg0,arg1,arg2,arg3,arg4);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 4
        && info[0]->IsInt32()
        && info[1]->IsInt32()
        && info[2]->IsInt32()
        && info[3]->IsInt32())
      {
        try
        {
          ::Windows::Globalization::DateTimeFormatting::YearFormat arg0 = static_cast<::Windows::Globalization::DateTimeFormatting::YearFormat>(Nan::To<int32_t>(info[0]).FromMaybe(0));
          ::Windows::Globalization::DateTimeFormatting::MonthFormat arg1 = static_cast<::Windows::Globalization::DateTimeFormatting::MonthFormat>(Nan::To<int32_t>(info[1]).FromMaybe(0));
          ::Windows::Globalization::DateTimeFormatting::DayFormat arg2 = static_cast<::Windows::Globalization::DateTimeFormatting::DayFormat>(Nan::To<int32_t>(info[2]).FromMaybe(0));
          ::Windows::Globalization::DateTimeFormatting::DayOfWeekFormat arg3 = static_cast<::Windows::Globalization::DateTimeFormatting::DayOfWeekFormat>(Nan::To<int32_t>(info[3]).FromMaybe(0));
          
          winRtInstance = ref new ::Windows::Globalization::DateTimeFormatting::DateTimeFormatter(arg0,arg1,arg2,arg3);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 3
        && info[0]->IsInt32()
        && info[1]->IsInt32()
        && info[2]->IsInt32())
      {
        try
        {
          ::Windows::Globalization::DateTimeFormatting::HourFormat arg0 = static_cast<::Windows::Globalization::DateTimeFormatting::HourFormat>(Nan::To<int32_t>(info[0]).FromMaybe(0));
          ::Windows::Globalization::DateTimeFormatting::MinuteFormat arg1 = static_cast<::Windows::Globalization::DateTimeFormatting::MinuteFormat>(Nan::To<int32_t>(info[1]).FromMaybe(0));
          ::Windows::Globalization::DateTimeFormatting::SecondFormat arg2 = static_cast<::Windows::Globalization::DateTimeFormatting::SecondFormat>(Nan::To<int32_t>(info[2]).FromMaybe(0));
          
          winRtInstance = ref new ::Windows::Globalization::DateTimeFormatting::DateTimeFormatter(arg0,arg1,arg2);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 8
        && info[0]->IsInt32()
        && info[1]->IsInt32()
        && info[2]->IsInt32()
        && info[3]->IsInt32()
        && info[4]->IsInt32()
        && info[5]->IsInt32()
        && info[6]->IsInt32()
        && (NodeRT::Utils::IsWinRtWrapperOf<::Windows::Foundation::Collections::IIterable<::Platform::String^>^>(info[7]) || info[7]->IsArray()))
      {
        try
        {
          ::Windows::Globalization::DateTimeFormatting::YearFormat arg0 = static_cast<::Windows::Globalization::DateTimeFormatting::YearFormat>(Nan::To<int32_t>(info[0]).FromMaybe(0));
          ::Windows::Globalization::DateTimeFormatting::MonthFormat arg1 = static_cast<::Windows::Globalization::DateTimeFormatting::MonthFormat>(Nan::To<int32_t>(info[1]).FromMaybe(0));
          ::Windows::Globalization::DateTimeFormatting::DayFormat arg2 = static_cast<::Windows::Globalization::DateTimeFormatting::DayFormat>(Nan::To<int32_t>(info[2]).FromMaybe(0));
          ::Windows::Globalization::DateTimeFormatting::DayOfWeekFormat arg3 = static_cast<::Windows::Globalization::DateTimeFormatting::DayOfWeekFormat>(Nan::To<int32_t>(info[3]).FromMaybe(0));
          ::Windows::Globalization::DateTimeFormatting::HourFormat arg4 = static_cast<::Windows::Globalization::DateTimeFormatting::HourFormat>(Nan::To<int32_t>(info[4]).FromMaybe(0));
          ::Windows::Globalization::DateTimeFormatting::MinuteFormat arg5 = static_cast<::Windows::Globalization::DateTimeFormatting::MinuteFormat>(Nan::To<int32_t>(info[5]).FromMaybe(0));
          ::Windows::Globalization::DateTimeFormatting::SecondFormat arg6 = static_cast<::Windows::Globalization::DateTimeFormatting::SecondFormat>(Nan::To<int32_t>(info[6]).FromMaybe(0));
          ::Windows::Foundation::Collections::IIterable<::Platform::String^>^ arg7 = 
            [] (v8::Local<v8::Value> value) -> ::Windows::Foundation::Collections::IIterable<::Platform::String^>^
            {
              if (value->IsArray())
              {
                return NodeRT::Collections::JsArrayToWinrtVector<::Platform::String^>(value.As<Array>(), 
                 [](Local<Value> value) -> bool {
                   return (!NodeRT::Utils::IsWinRtWrapper(value));
                 },
                 [](Local<Value> value) -> ::Platform::String^ {
                   return ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(value)));
                 }
                );
              }
              else
              {
                return dynamic_cast<::Windows::Foundation::Collections::IIterable<::Platform::String^>^>(NodeRT::Utils::GetObjectInstance(value));
              }
            } (info[7]);
          
          winRtInstance = ref new ::Windows::Globalization::DateTimeFormatting::DateTimeFormatter(arg0,arg1,arg2,arg3,arg4,arg5,arg6,arg7);
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 11
        && info[0]->IsInt32()
        && info[1]->IsInt32()
        && info[2]->IsInt32()
        && info[3]->IsInt32()
        && info[4]->IsInt32()
        && info[5]->IsInt32()
        && info[6]->IsInt32()
        && (NodeRT::Utils::IsWinRtWrapperOf<::Windows::Foundation::Collections::IIterable<::Platform::String^>^>(info[7]) || info[7]->IsArray())
        && info[8]->IsString()
        && info[9]->IsString()
        && info[10]->IsString())
      {
        try
        {
          ::Windows::Globalization::DateTimeFormatting::YearFormat arg0 = static_cast<::Windows::Globalization::DateTimeFormatting::YearFormat>(Nan::To<int32_t>(info[0]).FromMaybe(0));
          ::Windows::Globalization::DateTimeFormatting::MonthFormat arg1 = static_cast<::Windows::Globalization::DateTimeFormatting::MonthFormat>(Nan::To<int32_t>(info[1]).FromMaybe(0));
          ::Windows::Globalization::DateTimeFormatting::DayFormat arg2 = static_cast<::Windows::Globalization::DateTimeFormatting::DayFormat>(Nan::To<int32_t>(info[2]).FromMaybe(0));
          ::Windows::Globalization::DateTimeFormatting::DayOfWeekFormat arg3 = static_cast<::Windows::Globalization::DateTimeFormatting::DayOfWeekFormat>(Nan::To<int32_t>(info[3]).FromMaybe(0));
          ::Windows::Globalization::DateTimeFormatting::HourFormat arg4 = static_cast<::Windows::Globalization::DateTimeFormatting::HourFormat>(Nan::To<int32_t>(info[4]).FromMaybe(0));
          ::Windows::Globalization::DateTimeFormatting::MinuteFormat arg5 = static_cast<::Windows::Globalization::DateTimeFormatting::MinuteFormat>(Nan::To<int32_t>(info[5]).FromMaybe(0));
          ::Windows::Globalization::DateTimeFormatting::SecondFormat arg6 = static_cast<::Windows::Globalization::DateTimeFormatting::SecondFormat>(Nan::To<int32_t>(info[6]).FromMaybe(0));
          ::Windows::Foundation::Collections::IIterable<::Platform::String^>^ arg7 = 
            [] (v8::Local<v8::Value> value) -> ::Windows::Foundation::Collections::IIterable<::Platform::String^>^
            {
              if (value->IsArray())
              {
                return NodeRT::Collections::JsArrayToWinrtVector<::Platform::String^>(value.As<Array>(), 
                 [](Local<Value> value) -> bool {
                   return (!NodeRT::Utils::IsWinRtWrapper(value));
                 },
                 [](Local<Value> value) -> ::Platform::String^ {
                   return ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(value)));
                 }
                );
              }
              else
              {
                return dynamic_cast<::Windows::Foundation::Collections::IIterable<::Platform::String^>^>(NodeRT::Utils::GetObjectInstance(value));
              }
            } (info[7]);
          Platform::String^ arg8 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(info[8])));
          Platform::String^ arg9 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(info[9])));
          Platform::String^ arg10 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(info[10])));
          
          winRtInstance = ref new ::Windows::Globalization::DateTimeFormatting::DateTimeFormatter(arg0,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10);
        }
        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());

      DateTimeFormatter *wrapperInstance = new DateTimeFormatter(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::DateTimeFormatting::DateTimeFormatter^>(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::DateTimeFormatting::DateTimeFormatter^ winRtInstance;
		try
		{
			winRtInstance = (::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^) NodeRT::Utils::GetObjectInstance(info[0]);
		}
		catch (Platform::Exception ^exception)
		{
			NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
			return;
		}

		info.GetReturnValue().Set(WrapDateTimeFormatter(winRtInstance));
    }


  
    static void Format(Nan::NAN_METHOD_ARGS_TYPE info)
    {
      HandleScope scope;

      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^>(info.This()))
      {
        return;
      }

      DateTimeFormatter *wrapper = DateTimeFormatter::Unwrap<DateTimeFormatter>(info.This());

      if (info.Length() == 1
        && info[0]->IsDate())
      {
        try
        {
          ::Windows::Foundation::DateTime arg0 = NodeRT::Utils::DateTimeFromJSDate(info[0]);
          
          Platform::String^ result;
          result = wrapper->_instance->Format(arg0);
          info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
          return;
        }
        catch (Platform::Exception ^exception)
        {
          NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
          return;
        }
      }
      else if (info.Length() == 2
        && info[0]->IsDate()
        && info[1]->IsString())
      {
        try
        {
          ::Windows::Foundation::DateTime arg0 = NodeRT::Utils::DateTimeFromJSDate(info[0]);
          Platform::String^ arg1 = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(info[1])));
          
          Platform::String^ result;
          result = wrapper->_instance->Format(arg0, arg1);
          info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
          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 NumeralSystemGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^>(info.This()))
      {
        return;
      }

      DateTimeFormatter *wrapper = DateTimeFormatter::Unwrap<DateTimeFormatter>(info.This());

      try 
      {
        Platform::String^ result = wrapper->_instance->NumeralSystem;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void NumeralSystemSetter(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::Globalization::DateTimeFormatting::DateTimeFormatter^>(info.This()))
      {
        return;
      }

      DateTimeFormatter *wrapper = DateTimeFormatter::Unwrap<DateTimeFormatter>(info.This());

      try 
      {
        
        Platform::String^ winRtValue = ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(value)));

        wrapper->_instance->NumeralSystem = winRtValue;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
      }
    }
    
    static void ClockGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^>(info.This()))
      {
        return;
      }

      DateTimeFormatter *wrapper = DateTimeFormatter::Unwrap<DateTimeFormatter>(info.This());

      try 
      {
        Platform::String^ result = wrapper->_instance->Clock;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void GeographicRegionGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^>(info.This()))
      {
        return;
      }

      DateTimeFormatter *wrapper = DateTimeFormatter::Unwrap<DateTimeFormatter>(info.This());

      try 
      {
        Platform::String^ result = wrapper->_instance->GeographicRegion;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void IncludeDayGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^>(info.This()))
      {
        return;
      }

      DateTimeFormatter *wrapper = DateTimeFormatter::Unwrap<DateTimeFormatter>(info.This());

      try 
      {
        ::Windows::Globalization::DateTimeFormatting::DayFormat result = wrapper->_instance->IncludeDay;
        info.GetReturnValue().Set(Nan::New<Integer>(static_cast<int>(result)));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void IncludeDayOfWeekGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^>(info.This()))
      {
        return;
      }

      DateTimeFormatter *wrapper = DateTimeFormatter::Unwrap<DateTimeFormatter>(info.This());

      try 
      {
        ::Windows::Globalization::DateTimeFormatting::DayOfWeekFormat result = wrapper->_instance->IncludeDayOfWeek;
        info.GetReturnValue().Set(Nan::New<Integer>(static_cast<int>(result)));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void IncludeHourGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^>(info.This()))
      {
        return;
      }

      DateTimeFormatter *wrapper = DateTimeFormatter::Unwrap<DateTimeFormatter>(info.This());

      try 
      {
        ::Windows::Globalization::DateTimeFormatting::HourFormat result = wrapper->_instance->IncludeHour;
        info.GetReturnValue().Set(Nan::New<Integer>(static_cast<int>(result)));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void IncludeMinuteGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^>(info.This()))
      {
        return;
      }

      DateTimeFormatter *wrapper = DateTimeFormatter::Unwrap<DateTimeFormatter>(info.This());

      try 
      {
        ::Windows::Globalization::DateTimeFormatting::MinuteFormat result = wrapper->_instance->IncludeMinute;
        info.GetReturnValue().Set(Nan::New<Integer>(static_cast<int>(result)));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void IncludeMonthGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^>(info.This()))
      {
        return;
      }

      DateTimeFormatter *wrapper = DateTimeFormatter::Unwrap<DateTimeFormatter>(info.This());

      try 
      {
        ::Windows::Globalization::DateTimeFormatting::MonthFormat result = wrapper->_instance->IncludeMonth;
        info.GetReturnValue().Set(Nan::New<Integer>(static_cast<int>(result)));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void IncludeSecondGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^>(info.This()))
      {
        return;
      }

      DateTimeFormatter *wrapper = DateTimeFormatter::Unwrap<DateTimeFormatter>(info.This());

      try 
      {
        ::Windows::Globalization::DateTimeFormatting::SecondFormat result = wrapper->_instance->IncludeSecond;
        info.GetReturnValue().Set(Nan::New<Integer>(static_cast<int>(result)));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void IncludeYearGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^>(info.This()))
      {
        return;
      }

      DateTimeFormatter *wrapper = DateTimeFormatter::Unwrap<DateTimeFormatter>(info.This());

      try 
      {
        ::Windows::Globalization::DateTimeFormatting::YearFormat result = wrapper->_instance->IncludeYear;
        info.GetReturnValue().Set(Nan::New<Integer>(static_cast<int>(result)));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void LanguagesGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^>(info.This()))
      {
        return;
      }

      DateTimeFormatter *wrapper = DateTimeFormatter::Unwrap<DateTimeFormatter>(info.This());

      try 
      {
        ::Windows::Foundation::Collections::IVectorView<::Platform::String^>^ result = wrapper->_instance->Languages;
        info.GetReturnValue().Set(NodeRT::Collections::VectorViewWrapper<::Platform::String^>::CreateVectorViewWrapper(result, 
            [](::Platform::String^ val) -> Local<Value> {
              return NodeRT::Utils::NewString(val->Data());
            },
            [](Local<Value> value) -> bool {
              return value->IsString();
            },
            [](Local<Value> value) -> ::Platform::String^ {
              return ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(value)));
            }
          ));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void CalendarGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^>(info.This()))
      {
        return;
      }

      DateTimeFormatter *wrapper = DateTimeFormatter::Unwrap<DateTimeFormatter>(info.This());

      try 
      {
        Platform::String^ result = wrapper->_instance->Calendar;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void PatternsGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^>(info.This()))
      {
        return;
      }

      DateTimeFormatter *wrapper = DateTimeFormatter::Unwrap<DateTimeFormatter>(info.This());

      try 
      {
        ::Windows::Foundation::Collections::IVectorView<::Platform::String^>^ result = wrapper->_instance->Patterns;
        info.GetReturnValue().Set(NodeRT::Collections::VectorViewWrapper<::Platform::String^>::CreateVectorViewWrapper(result, 
            [](::Platform::String^ val) -> Local<Value> {
              return NodeRT::Utils::NewString(val->Data());
            },
            [](Local<Value> value) -> bool {
              return value->IsString();
            },
            [](Local<Value> value) -> ::Platform::String^ {
              return ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value(value)));
            }
          ));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void ResolvedGeographicRegionGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^>(info.This()))
      {
        return;
      }

      DateTimeFormatter *wrapper = DateTimeFormatter::Unwrap<DateTimeFormatter>(info.This());

      try 
      {
        Platform::String^ result = wrapper->_instance->ResolvedGeographicRegion;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void ResolvedLanguageGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^>(info.This()))
      {
        return;
      }

      DateTimeFormatter *wrapper = DateTimeFormatter::Unwrap<DateTimeFormatter>(info.This());

      try 
      {
        Platform::String^ result = wrapper->_instance->ResolvedLanguage;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void TemplateGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;
      
      if (!NodeRT::Utils::IsWinRtWrapperOf<::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^>(info.This()))
      {
        return;
      }

      DateTimeFormatter *wrapper = DateTimeFormatter::Unwrap<DateTimeFormatter>(info.This());

      try 
      {
        Platform::String^ result = wrapper->_instance->Template;
        info.GetReturnValue().Set(NodeRT::Utils::NewString(result->Data()));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    

    static void LongDateGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;

      try 
      {
        ::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ result = ::Windows::Globalization::DateTimeFormatting::DateTimeFormatter::LongDate;
        info.GetReturnValue().Set(WrapDateTimeFormatter(result));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void LongTimeGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;

      try 
      {
        ::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ result = ::Windows::Globalization::DateTimeFormatting::DateTimeFormatter::LongTime;
        info.GetReturnValue().Set(WrapDateTimeFormatter(result));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void ShortDateGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;

      try 
      {
        ::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ result = ::Windows::Globalization::DateTimeFormatting::DateTimeFormatter::ShortDate;
        info.GetReturnValue().Set(WrapDateTimeFormatter(result));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    
    static void ShortTimeGetter(Local<String> property, const Nan::PropertyCallbackInfo<v8::Value> &info)
    {
      HandleScope scope;

      try 
      {
        ::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ result = ::Windows::Globalization::DateTimeFormatting::DateTimeFormatter::ShortTime;
        info.GetReturnValue().Set(WrapDateTimeFormatter(result));
        return;
      }
      catch (Platform::Exception ^exception)
      {
        NodeRT::Utils::ThrowWinRtExceptionInJs(exception);
        return;
      }
    }
    

  private:
    ::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ _instance;
    static Persistent<FunctionTemplate> s_constructorTemplate;

    friend v8::Local<v8::Value> WrapDateTimeFormatter(::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ wintRtInstance);
    friend ::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ UnwrapDateTimeFormatter(Local<Value> value);
  };
  Persistent<FunctionTemplate> DateTimeFormatter::s_constructorTemplate;

  v8::Local<v8::Value> WrapDateTimeFormatter(::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ 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>(DateTimeFormatter::s_constructorTemplate);
    return scope.Escape(Nan::NewInstance(Nan::GetFunction(localRef).ToLocalChecked(),_countof(args), args).ToLocalChecked());
  }

  ::Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ UnwrapDateTimeFormatter(Local<Value> value)
  {
     return DateTimeFormatter::Unwrap<DateTimeFormatter>(Nan::To<Object>(value).ToLocalChecked())->_instance;
  }

  void InitDateTimeFormatter(Local<Object> exports)
  {
    DateTimeFormatter::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::DateTimeFormatting::InitYearFormatEnum(target);
  NodeRT::Windows::Globalization::DateTimeFormatting::InitMonthFormatEnum(target);
  NodeRT::Windows::Globalization::DateTimeFormatting::InitDayOfWeekFormatEnum(target);
  NodeRT::Windows::Globalization::DateTimeFormatting::InitDayFormatEnum(target);
  NodeRT::Windows::Globalization::DateTimeFormatting::InitHourFormatEnum(target);
  NodeRT::Windows::Globalization::DateTimeFormatting::InitMinuteFormatEnum(target);
  NodeRT::Windows::Globalization::DateTimeFormatting::InitSecondFormatEnum(target);
  NodeRT::Windows::Globalization::DateTimeFormatting::InitDateTimeFormatter(target);

  NodeRT::Utils::RegisterNameSpace("Windows.Globalization.DateTimeFormatting", target);
}


NODE_MODULE(binding, init)