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

  #include "../include/buf.h"
 
#include <iostream>

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

  GitBuf::GitBuf(git_buf *raw, bool selfFreeing) {
    this->raw = raw;
    this->selfFreeing = selfFreeing;
  }

  GitBuf::~GitBuf() {
      if (this->selfFreeing) {
        git_buf_free(this->raw);
      }
   }

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

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

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

          NODE_SET_PROTOTYPE_METHOD(tpl, "grow", Grow);
            NODE_SET_PROTOTYPE_METHOD(tpl, "set", Set);
           NODE_SET_PROTOTYPE_METHOD(tpl, "ptr", Ptr);
         NODE_SET_PROTOTYPE_METHOD(tpl, "asize", Asize);
         NODE_SET_PROTOTYPE_METHOD(tpl, "size", Size);
  
    Local<Function> _constructor_template = tpl->GetFunction();
    NanAssignPersistent(constructor_template, _constructor_template);
    target->Set(NanNew<String>("Buf"), _constructor_template);
  }

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

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

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

    NanReturnValue(args.This());
  }

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

  git_buf *GitBuf::GetValue() {
    return this->raw;
  }

  git_buf **GitBuf::GetRefValue() {
    return &this->raw;
  }

 
/*
  * @param Number target_size
    * @param Buf callback
   */
NAN_METHOD(GitBuf::Grow) {
  NanScope();

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

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

  GrowBaton* baton = new GrowBaton;

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

  baton->buffer = (git_buf *)malloc(sizeof(git_buf ));
// start convert_from_v8 block
  size_t from_target_size;
  from_target_size = (size_t)   args[0]->ToNumber()->Value();
// end convert_from_v8 block
  baton->target_size = from_target_size;

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

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitBuf::GrowWorker::Execute() {
  int result = git_buf_grow(
baton->buffer,baton->target_size    );

  baton->error_code = result;

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

}

void GitBuf::GrowWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->buffer != NULL) {
    // GitBuf baton->buffer
       to = GitBuf::New((void *)baton->buffer, 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;
}
  
/*
  * @param Buffer data
   * @param Number datalen
    * @param Buf callback
   */
NAN_METHOD(GitBuf::Set) {
  NanScope();

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

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

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

  SetBaton* baton = new SetBaton;

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

  baton->buffer = (git_buf *)malloc(sizeof(git_buf ));
// start convert_from_v8 block
  const void * from_data;

  from_data = Buffer::Data(args[0]->ToObject());
// end convert_from_v8 block
  baton->data = from_data;
// start convert_from_v8 block
  size_t from_datalen;
  from_datalen = (size_t)   args[1]->ToNumber()->Value();
// end convert_from_v8 block
  baton->datalen = from_datalen;

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

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitBuf::SetWorker::Execute() {
  int result = git_buf_set(
baton->buffer,baton->data,baton->datalen    );

  baton->error_code = result;

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

}

void GitBuf::SetWorker::HandleOKCallback() {
  TryCatch try_catch;

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

      char *  ptr =
 ObjectWrap::Unwrap<GitBuf>(args.This())->GetValue()->ptr;

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

  // end convert_to_v8 block
      NanReturnValue(to);
    }
     NAN_METHOD(GitBuf::Asize) {
      NanScope();
      Handle<Value> to;

      size_t  asize =
 ObjectWrap::Unwrap<GitBuf>(args.This())->GetValue()->asize;

// start convert_to_v8 block
     to = NanNew<Number>(asize);
  // end convert_to_v8 block
      NanReturnValue(to);
    }
     NAN_METHOD(GitBuf::Size) {
      NanScope();
      Handle<Value> to;

      size_t  size =
 ObjectWrap::Unwrap<GitBuf>(args.This())->GetValue()->size;

// start convert_to_v8 block
     to = NanNew<Number>(size);
  // end convert_to_v8 block
      NanReturnValue(to);
    }
    Persistent<Function> GitBuf::constructor_template;
 