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

  #include "../include/indexer.h"
  #include "../include/transfer_progress.h"
  #include "../include/oid.h"
 
#include <iostream>

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

  GitIndexer::GitIndexer(git_indexer *raw, bool selfFreeing) {
    this->raw = raw;
    this->selfFreeing = selfFreeing;
  }

  GitIndexer::~GitIndexer() {
      if (this->selfFreeing) {
        git_indexer_free(this->raw);
      }
   }

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

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

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

          NODE_SET_PROTOTYPE_METHOD(tpl, "commit", Commit);
            NODE_SET_PROTOTYPE_METHOD(tpl, "hash", Hash);
    
    Local<Function> _constructor_template = tpl->GetFunction();
    NanAssignPersistent(constructor_template, _constructor_template);
    target->Set(NanNew<String>("Indexer"), _constructor_template);
  }

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

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

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

    NanReturnValue(args.This());
  }

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

  git_indexer *GitIndexer::GetValue() {
    return this->raw;
  }

  git_indexer **GitIndexer::GetRefValue() {
    return &this->raw;
  }

  
/*
   * @param TransferProgress stats
     * @return Number  result    */
NAN_METHOD(GitIndexer::Commit) {
  NanEscapableScope();

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

// start convert_from_v8 block
  git_transfer_progress * from_stats;
  from_stats = ObjectWrap::Unwrap<GitTransferProgress>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  int result = git_indexer_commit(
ObjectWrap::Unwrap<GitIndexer>(args.This())->GetValue()
,from_stats
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
     * @return Oid  result    */
NAN_METHOD(GitIndexer::Hash) {
  NanEscapableScope();

  const git_oid * result = git_indexer_hash(
ObjectWrap::Unwrap<GitIndexer>(args.This())->GetValue()
  );
  // null checks on pointers
  if (!result) {
    NodeGitPsueodoNanReturnEscapingValue(NanUndefined());
  }

  Handle<Value> to;
// start convert_to_v8 block
  
  if (result != NULL) {
    // GitOid result
       to = GitOid::New((void *)result, false);
   }
  else {
    to = NanNull();
  }

 // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
      Persistent<Function> GitIndexer::constructor_template;
 