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

  #include "../include/repository.h"
  #include "../include/checkout_options.h"
 
#include <iostream>

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

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

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

        NODE_SET_METHOD(object, "head", Head);
         NODE_SET_METHOD(object, "initOptions", InitOptions);
  
    target->Set(NanNew<String>("Checkout"), object);
  }

 
/*
 * @param Repository repo
   * @param CheckoutOptions opts
     */
NAN_METHOD(GitCheckout::Head) {
  NanScope();

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

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

  HeadBaton* baton = new HeadBaton;

  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 git_checkout_options * from_opts;

  if (args[1]->IsObject()) {
  from_opts = ObjectWrap::Unwrap<GitCheckoutOptions>(args[1]->ToObject())->GetValue();
  }
  else {
    from_opts = 0;
  }

// end convert_from_v8 block
  baton->opts = from_opts;

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

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitCheckout::HeadWorker::Execute() {
  int result = git_checkout_head(
baton->repo,baton->opts    );

  baton->error_code = result;

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

}

void GitCheckout::HeadWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> result = NanUndefined();
    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 CheckoutOptions opts
   * @param Number version
     * @return Number  result    */
NAN_METHOD(GitCheckout::InitOptions) {
  NanEscapableScope();

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

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

// start convert_from_v8 block
  git_checkout_options * from_opts;
  from_opts = ObjectWrap::Unwrap<GitCheckoutOptions>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  unsigned int from_version;
  from_version = (unsigned int)   args[1]->ToNumber()->Value();
// end convert_from_v8 block
  int result = git_checkout_init_options(
from_opts
,from_version
  );

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