// This is a generated file, modify: generate/templates/class_content.cc.
#include <nan.h>
#include <string.h>

extern "C" {
#include <git2.h>
}


#include "../include/functions/copy.h"
#include "../include/macros.h"
#include "../include/transport.h"

#include "../include/transport.h"

#include <iostream>

using namespace std;
using namespace v8;
using namespace node;

GitTransport::GitTransport(git_transport *raw, bool selfFreeing)
{
  this->raw = raw;
  this->selfFreeing = selfFreeing;
}

GitTransport::~GitTransport()
{
}

void GitTransport::InitializeComponent(Handle<v8::Object> target)
{
  NanScope();

  Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(JSNewFunction);

  tpl->InstanceTemplate()->SetInternalFieldCount(1);
  tpl->SetClassName(NanNew<String>("Transport"));

  NODE_SET_PROTOTYPE_METHOD(tpl, "init", Init);
  NODE_SET_METHOD(tpl, "unregister", Unregister);

  Local<Function> _constructor_template = tpl->GetFunction();
  NanAssignPersistent(constructor_template, _constructor_template);
  target->Set(NanNew<String>("Transport"), _constructor_template);
}

NAN_METHOD(GitTransport::JSNewFunction)
{
  NanScope();

  if (args.Length() == 0 || !args[0]->IsExternal()) {
    return NanThrowError("A new GitTransport cannot be instantiated.");
  }

  GitTransport* object = new GitTransport(static_cast<git_transport *>(Handle<External>::Cast(args[0])->Value()), args[1]->BooleanValue());
  object->Wrap(args.This());

  NanReturnValue(args.This());
}

Handle<Value> GitTransport::New(void *raw, bool selfFreeing)
{
  NanEscapableScope();
  Handle<Value> argv[2] = { NanNew<External>((void *)raw), NanNew<Boolean>(selfFreeing) };
  return NanEscapeScope(NanNew<Function>(GitTransport::constructor_template)->NewInstance(2, argv));
}

git_transport *GitTransport::GetValue()
{
  return this->raw;
}

git_transport **GitTransport::GetRefValue()
{
  return &this->raw;
}


/*
   * @param Number version
     * @return Number  result    */
NAN_METHOD(GitTransport::Init)
{
  NanEscapableScope();

  if (args.Length() == 0 || !args[0]->IsNumber()) {
    return NanThrowError("Number version is required.");
  }

// start convert_from_v8 block
  unsigned int from_version;
  from_version = (unsigned int)   args[0]->ToNumber()->Value();
// end convert_from_v8 block
  int result = git_transport_init(
                 ObjectWrap::Unwrap<GitTransport>(args.This())->GetValue()
                 ,from_version
               );

  Handle<Value> to;
// start convert_to_v8 block
  to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
 * @param String prefix
   * @param Number priority
     * @return Number  result    */
NAN_METHOD(GitTransport::Unregister)
{
  NanEscapableScope();

  if (args.Length() == 0 || !args[0]->IsString()) {
    return NanThrowError("String prefix is required.");
  }

  if (args.Length() == 1 || !args[1]->IsNumber()) {
    return NanThrowError("Number priority is required.");
  }

// start convert_from_v8 block
  const char * from_prefix;

  String::Utf8Value prefix(args[0]->ToString());
  from_prefix = (const char *) strdup(*prefix);
// end convert_from_v8 block
// start convert_from_v8 block
  unsigned int from_priority;
  from_priority = (unsigned int)   args[1]->ToNumber()->Value();
// end convert_from_v8 block
  int result = git_transport_unregister(
                 from_prefix
                 ,from_priority
               );

  Handle<Value> to;
// start convert_to_v8 block
  to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
Persistent<Function> GitTransport::constructor_template;
