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

  #include "../include/object.h"
  #include "../include/reference.h"
  #include "../include/repository.h"
 
#include <iostream>

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

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

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

        NODE_SET_METHOD(object, "ext", Ext);
         NODE_SET_METHOD(object, "single", Single);
  
    target->Set(NanNew<String>("Revparse"), object);
  }

  
/*
 * @param Object object_out
   * @param Reference reference_out
   * @param Repository repo
   * @param String spec
     * @return Number  result    */
NAN_METHOD(GitRevparse::Ext) {
  NanEscapableScope();

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

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

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

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

// start convert_from_v8 block
  git_object ** from_object_out;
  from_object_out = ObjectWrap::Unwrap<GitObject>(args[0]->ToObject())->GetRefValue();
// end convert_from_v8 block
// start convert_from_v8 block
  git_reference ** from_reference_out;
  from_reference_out = ObjectWrap::Unwrap<GitRefs>(args[1]->ToObject())->GetRefValue();
// end convert_from_v8 block
// start convert_from_v8 block
  git_repository * from_repo;
  from_repo = ObjectWrap::Unwrap<GitRepository>(args[2]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_spec;

  String::Utf8Value spec(args[3]->ToString());
  from_spec = (const char *) strdup(*spec);
// end convert_from_v8 block
  int result = git_revparse_ext(
from_object_out
,from_reference_out
,from_repo
,from_spec
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
  
/*
  * @param Repository repo
   * @param String spec
    * @param Object callback
   */
NAN_METHOD(GitRevparse::Single) {
  NanScope();

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

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

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

  SingleBaton* baton = new SingleBaton;

  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_spec;

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

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

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRevparse::SingleWorker::Execute() {
  int result = git_revparse_single(
&baton->out,baton->repo,baton->spec    );

  baton->error_code = result;

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

}

void GitRevparse::SingleWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->out != NULL) {
    // GitObject baton->out
       to = GitObject::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->spec);

  delete baton;
}
      Persistent<Function> GitRevparse::constructor_template;
 