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

  #include "../include/reflog.h"
  #include "../include/oid.h"
  #include "../include/signature.h"
  #include "../include/repository.h"
  #include "../include/reflog_entry.h"
 
#include <iostream>

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

  GitReflog::GitReflog(git_reflog *raw, bool selfFreeing) {
    this->raw = raw;
    this->selfFreeing = selfFreeing;
  }

  GitReflog::~GitReflog() {
      if (this->selfFreeing) {
        git_reflog_free(this->raw);
      }
   }

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

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

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

          NODE_SET_PROTOTYPE_METHOD(tpl, "append", Append);
             NODE_SET_METHOD(tpl, "delete", Delete);
            NODE_SET_PROTOTYPE_METHOD(tpl, "drop", Drop);
            NODE_SET_PROTOTYPE_METHOD(tpl, "entryByIndex", EntryByindex);
             NODE_SET_METHOD(tpl, "entryCommitter", EntryCommitter);
             NODE_SET_METHOD(tpl, "entryIdNew", EntryIdNew);
             NODE_SET_METHOD(tpl, "entryIdOld", EntryIdOld);
             NODE_SET_METHOD(tpl, "entryMessage", EntryMessage);
            NODE_SET_PROTOTYPE_METHOD(tpl, "entrycount", Entrycount);
             NODE_SET_METHOD(tpl, "read", Read);
             NODE_SET_METHOD(tpl, "rename", Rename);
            NODE_SET_PROTOTYPE_METHOD(tpl, "write", Write);
    
    Local<Function> _constructor_template = tpl->GetFunction();
    NanAssignPersistent(constructor_template, _constructor_template);
    target->Set(NanNew<String>("Reflog"), _constructor_template);
  }

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

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

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

    NanReturnValue(args.This());
  }

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

  git_reflog *GitReflog::GetValue() {
    return this->raw;
  }

  git_reflog **GitReflog::GetRefValue() {
    return &this->raw;
  }

  
/*
   * @param Oid id
   * @param Signature committer
   * @param String msg
     * @return Number  result    */
NAN_METHOD(GitReflog::Append) {
  NanEscapableScope();

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

  if (args.Length() == 1 || !args[1]->IsObject()) {
    return NanThrowError("Signature committer is required.");
  }

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

// start convert_from_v8 block
  const git_oid * from_id;
  if (args[0]->IsString()) {
    // Try and parse in a string to a git_oid
    String::Utf8Value oidString(args[0]->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_id = oidOut;
  }
  else {
    from_id = ObjectWrap::Unwrap<GitOid>(args[0]->ToObject())->GetValue();
  }
// end convert_from_v8 block
// start convert_from_v8 block
  const git_signature * from_committer;
  from_committer = ObjectWrap::Unwrap<GitSignature>(args[1]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_msg;

  String::Utf8Value msg(args[2]->ToString());
  from_msg = (const char *) strdup(*msg);
// end convert_from_v8 block
  int result = git_reflog_append(
ObjectWrap::Unwrap<GitReflog>(args.This())->GetValue()
,from_id
,from_committer
,from_msg
  );
  if (args[0]->IsString()) {
    free(&from_id);
  }

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
 * @param Repository repo
   * @param String name
     * @return Number  result    */
NAN_METHOD(GitReflog::Delete) {
  NanEscapableScope();

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

  if (args.Length() == 1 || !args[1]->IsString()) {
    return NanThrowError("String name 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 char * from_name;

  String::Utf8Value name(args[1]->ToString());
  from_name = (const char *) strdup(*name);
// end convert_from_v8 block
  int result = git_reflog_delete(
from_repo
,from_name
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
   * @param Number idx
   * @param Number rewrite_previous_entry
     * @return Number  result    */
NAN_METHOD(GitReflog::Drop) {
  NanEscapableScope();

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

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

// start convert_from_v8 block
  size_t from_idx;
  from_idx = (size_t)   args[0]->ToNumber()->Value();
// end convert_from_v8 block
// start convert_from_v8 block
  int from_rewrite_previous_entry;
  from_rewrite_previous_entry = (int)   args[1]->ToNumber()->Value();
// end convert_from_v8 block
  int result = git_reflog_drop(
ObjectWrap::Unwrap<GitReflog>(args.This())->GetValue()
,from_idx
,from_rewrite_previous_entry
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
   * @param Number idx
     * @return ReflogEntry  result    */
NAN_METHOD(GitReflog::EntryByindex) {
  NanEscapableScope();

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

// start convert_from_v8 block
  size_t from_idx;
  from_idx = (size_t)   args[0]->ToNumber()->Value();
// end convert_from_v8 block
  const git_reflog_entry * result = git_reflog_entry_byindex(
ObjectWrap::Unwrap<GitReflog>(args.This())->GetValue()
,from_idx
  );
  // null checks on pointers
  if (!result) {
    NodeGitPsueodoNanReturnEscapingValue(NanUndefined());
  }

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

 // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
 * @param ReflogEntry entry
     * @return Signature  result    */
NAN_METHOD(GitReflog::EntryCommitter) {
  NanEscapableScope();

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

// start convert_from_v8 block
  const git_reflog_entry * from_entry;
  from_entry = ObjectWrap::Unwrap<GitReflogEntry>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  const git_signature * result = git_reflog_entry_committer(
from_entry
  );
  // null checks on pointers
  if (!result) {
    NodeGitPsueodoNanReturnEscapingValue(NanUndefined());
  }

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

 // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
 * @param ReflogEntry entry
     * @return Oid  result    */
NAN_METHOD(GitReflog::EntryIdNew) {
  NanEscapableScope();

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

// start convert_from_v8 block
  const git_reflog_entry * from_entry;
  from_entry = ObjectWrap::Unwrap<GitReflogEntry>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  const git_oid * result = git_reflog_entry_id_new(
from_entry
  );
  // 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);
}
   
/*
 * @param ReflogEntry entry
     * @return Oid  result    */
NAN_METHOD(GitReflog::EntryIdOld) {
  NanEscapableScope();

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

// start convert_from_v8 block
  const git_reflog_entry * from_entry;
  from_entry = ObjectWrap::Unwrap<GitReflogEntry>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  const git_oid * result = git_reflog_entry_id_old(
from_entry
  );
  // 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);
}
   
/*
 * @param ReflogEntry entry
     * @return String  result    */
NAN_METHOD(GitReflog::EntryMessage) {
  NanEscapableScope();

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

// start convert_from_v8 block
  const git_reflog_entry * from_entry;
  from_entry = ObjectWrap::Unwrap<GitReflogEntry>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  const char * result = git_reflog_entry_message(
from_entry
  );
  // null checks on pointers
  if (!result) {
    NodeGitPsueodoNanReturnEscapingValue(NanUndefined());
  }

  Handle<Value> to;
// start convert_to_v8 block
  if (result){
       to = NanNew<String>(result);
   }
  else {
    to = NanNull();
  }

  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
     * @return Number  result    */
NAN_METHOD(GitReflog::Entrycount) {
  NanEscapableScope();

  size_t result = git_reflog_entrycount(
ObjectWrap::Unwrap<GitReflog>(args.This())->GetValue()
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
  
/*
  * @param Repository repo
   * @param String name
    * @param Reflog callback
   */
NAN_METHOD(GitReflog::Read) {
  NanScope();

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

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

  if (args.Length() == 2 || !args[2]->IsFunction()) {
    return NanThrowError("Callback is required and must be a Function.");
  }

  ReadBaton* baton = new ReadBaton;

  baton->error_code = GIT_OK;
  baton->error = NULL;

// start convert_from_v8 block
  git_repository * from_repo;
  from_repo = ObjectWrap::Unwrap<GitRepository>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->repo = from_repo;
// start convert_from_v8 block
  const char * from_name;

  String::Utf8Value name(args[1]->ToString());
  from_name = (const char *) strdup(*name);
// end convert_from_v8 block
  baton->name = from_name;

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[2]));
  ReadWorker *worker = new ReadWorker(baton, callback);
  if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("repo", args[0]->ToObject());
  if (!args[1]->IsUndefined() && !args[1]->IsNull())
    worker->SaveToPersistent("name", args[1]->ToObject());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitReflog::ReadWorker::Execute() {
  int result = git_reflog_read(
&baton->out,baton->repo,baton->name    );

  baton->error_code = result;

  if (result != GIT_OK && giterr_last() != NULL) {
    baton->error = git_error_dup(giterr_last());
  }

}

void GitReflog::ReadWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->out != NULL) {
    // GitReflog baton->out
       to = GitReflog::New((void *)baton->out, false);
   }
  else {
    to = NanNull();
  }

 // end convert_to_v8 block
    Handle<Value> result = to;
    Handle<Value> argv[2] = {
      NanNull(),
      result
    };
    callback->Call(2, argv);
  } else {
    if (baton->error) {
      Handle<Value> argv[1] = {
        NanError(baton->error->message)
      };
      callback->Call(1, argv);
      if (baton->error->message)
        free((void *)baton->error->message);
      free((void *)baton->error);
    } else {
      callback->Call(0, NULL);
    }

  }

  if (try_catch.HasCaught()) {
    node::FatalException(try_catch);
  }

  free((void *)baton->name);

  delete baton;
}
   
/*
 * @param Repository repo
   * @param String old_name
   * @param String name
     * @return Number  result    */
NAN_METHOD(GitReflog::Rename) {
  NanEscapableScope();

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

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

  if (args.Length() == 2 || !args[2]->IsString()) {
    return NanThrowError("String name 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 char * from_old_name;

  String::Utf8Value old_name(args[1]->ToString());
  from_old_name = (const char *) strdup(*old_name);
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_name;

  String::Utf8Value name(args[2]->ToString());
  from_name = (const char *) strdup(*name);
// end convert_from_v8 block
  int result = git_reflog_rename(
from_repo
,from_old_name
,from_name
  );

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

  int result = git_reflog_write(
ObjectWrap::Unwrap<GitReflog>(args.This())->GetValue()
  );

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