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

  #include "../include/wrapper.h"
  #include "node_buffer.h"
  #include "../include/odb_object.h"
  #include "../include/oid.h"
 
#include <iostream>

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

  GitOdbObject::GitOdbObject(git_odb_object *raw, bool selfFreeing) {
    this->raw = raw;
    this->selfFreeing = selfFreeing;
  }

  GitOdbObject::~GitOdbObject() {
      if (this->selfFreeing) {
        git_odb_object_free(this->raw);
      }
   }

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

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

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

          NODE_SET_PROTOTYPE_METHOD(tpl, "data", Data);
            NODE_SET_PROTOTYPE_METHOD(tpl, "dup", Dup);
            NODE_SET_PROTOTYPE_METHOD(tpl, "id", Id);
            NODE_SET_PROTOTYPE_METHOD(tpl, "size", Size);
            NODE_SET_PROTOTYPE_METHOD(tpl, "type", Type);
    
    Local<Function> _constructor_template = tpl->GetFunction();
    NanAssignPersistent(constructor_template, _constructor_template);
    target->Set(NanNew<String>("OdbObject"), _constructor_template);
  }

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

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

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

    NanReturnValue(args.This());
  }

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

  git_odb_object *GitOdbObject::GetValue() {
    return this->raw;
  }

  git_odb_object **GitOdbObject::GetRefValue() {
    return &this->raw;
  }

  
/*
     * @return Buffer  result    */
NAN_METHOD(GitOdbObject::Data) {
  NanEscapableScope();

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

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

 // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
  
/*
     * @param OdbObject callback
   */
NAN_METHOD(GitOdbObject::Dup) {
  NanScope();

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

  DupBaton* baton = new DupBaton;

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

  baton->source = ObjectWrap::Unwrap<GitOdbObject>(args.This())->GetValue();

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[0]));
  DupWorker *worker = new DupWorker(baton, callback);
  worker->SaveToPersistent("source", args.This());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitOdbObject::DupWorker::Execute() {
  int result = git_odb_object_dup(
&baton->dest,baton->source    );

  baton->error_code = result;

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

}

void GitOdbObject::DupWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->dest != NULL) {
    // GitOdbObject baton->dest
       to = GitOdbObject::New((void *)baton->dest, 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);
  }


  delete baton;
}
   
/*
     * @return Oid  result    */
NAN_METHOD(GitOdbObject::Id) {
  NanEscapableScope();

  const git_oid * result = git_odb_object_id(
ObjectWrap::Unwrap<GitOdbObject>(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);
}
   
/*
     * @return Number  result    */
NAN_METHOD(GitOdbObject::Size) {
  NanEscapableScope();

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

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

  git_otype result = git_odb_object_type(
ObjectWrap::Unwrap<GitOdbObject>(args.This())->GetValue()
  );

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