// 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/graph.h"

  #include "../include/repository.h"
  #include "../include/oid.h"
 
#include <iostream>

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

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

    Local<Object> object = NanNew<Object>();

        NODE_SET_METHOD(object, "descendantOf", DescendantOf);
  
    target->Set(NanNew<String>("Graph"), object);
  }

  
/*
 * @param Repository repo
   * @param Oid commit
   * @param Oid ancestor
     * @return Number  result    */
NAN_METHOD(GitGraph::DescendantOf) {
  NanEscapableScope();

  if (args.Length() == 0 || !args[0]->IsObject()) {
    return NanThrowError("Repository repo is required.");
  }

  if (args.Length() == 1
    || (!args[1]->IsObject() && !args[1]->IsString())) {
    return NanThrowError("Oid commit is required.");
  }

  if (args.Length() == 2
    || (!args[2]->IsObject() && !args[2]->IsString())) {
    return NanThrowError("Oid ancestor is required.");
  }

// start convert_from_v8 block
  git_repository * from_repo;
  from_repo = ObjectWrap::Unwrap<GitRepository>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const git_oid * from_commit;
  if (args[1]->IsString()) {
    // Try and parse in a string to a git_oid
    String::Utf8Value oidString(args[1]->ToString());
    git_oid *oidOut = (git_oid *)malloc(sizeof(git_oid));

    if (git_oid_fromstr(oidOut, (const char *) strdup(*oidString)) != GIT_OK) {
      free(oidOut);

      if (giterr_last()) {
        return NanThrowError(giterr_last()->message);
      } else {
        return NanThrowError("Unknown Error");
      }
    }

    from_commit = oidOut;
  }
  else {
    from_commit = ObjectWrap::Unwrap<GitOid>(args[1]->ToObject())->GetValue();
  }
// end convert_from_v8 block
// start convert_from_v8 block
  const git_oid * from_ancestor;
  if (args[2]->IsString()) {
    // Try and parse in a string to a git_oid
    String::Utf8Value oidString(args[2]->ToString());
    git_oid *oidOut = (git_oid *)malloc(sizeof(git_oid));

    if (git_oid_fromstr(oidOut, (const char *) strdup(*oidString)) != GIT_OK) {
      free(oidOut);

      if (giterr_last()) {
        return NanThrowError(giterr_last()->message);
      } else {
        return NanThrowError("Unknown Error");
      }
    }

    from_ancestor = oidOut;
  }
  else {
    from_ancestor = ObjectWrap::Unwrap<GitOid>(args[2]->ToObject())->GetValue();
  }
// end convert_from_v8 block
  int result = git_graph_descendant_of(
from_repo
,from_commit
,from_ancestor
  );
  if (args[1]->IsString()) {
    free(&from_commit);
  }
  if (args[2]->IsString()) {
    free(&from_ancestor);
  }

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