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

  #include "../include/repository.h"
  #include "../include/clone_options.h"
  #include "../include/remote.h"
  #include "../include/checkout_options.h"
  #include "../include/signature.h"
 
#include <iostream>

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

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

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

        NODE_SET_METHOD(object, "clone", Clone);
         NODE_SET_METHOD(object, "initOptions", InitOptions);
         NODE_SET_METHOD(object, "into", Into);
         NODE_SET_METHOD(object, "localInto", LocalInto);
  
    target->Set(NanNew<String>("Clone"), object);
  }

 
/*
  * @param String url
   * @param String local_path
   * @param CloneOptions options
    * @param Repository callback
   */
NAN_METHOD(GitClone::Clone) {
  NanScope();

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

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

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

  CloneBaton* baton = new CloneBaton;

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

// start convert_from_v8 block
  const char * from_url;

  String::Utf8Value url(args[0]->ToString());
  from_url = (const char *) strdup(*url);
// end convert_from_v8 block
  baton->url = from_url;
// start convert_from_v8 block
  const char * from_local_path;

  String::Utf8Value local_path(args[1]->ToString());
  from_local_path = (const char *) strdup(*local_path);
// end convert_from_v8 block
  baton->local_path = from_local_path;
// start convert_from_v8 block
  const git_clone_options * from_options;

  if (args[2]->IsObject()) {
  from_options = ObjectWrap::Unwrap<GitCloneOptions>(args[2]->ToObject())->GetValue();
  }
  else {
    from_options = 0;
  }

// end convert_from_v8 block
  baton->options = from_options;

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

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitClone::CloneWorker::Execute() {
  int result = git_clone(
&baton->out,baton->url,baton->local_path,baton->options    );

  baton->error_code = result;

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

}

void GitClone::CloneWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->out != NULL) {
    // GitRepository baton->out
       to = GitRepository::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->url);
  free((void *)baton->local_path);

  delete baton;
}
   
/*
 * @param CloneOptions opts
   * @param Number version
     * @return Number  result    */
NAN_METHOD(GitClone::InitOptions) {
  NanEscapableScope();

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

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

// start convert_from_v8 block
  git_clone_options * from_opts;
  from_opts = ObjectWrap::Unwrap<GitCloneOptions>(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_clone_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);
}
   
/*
 * @param Repository repo
   * @param Remote remote
   * @param CheckoutOptions co_opts
   * @param String branch
   * @param Signature signature
     * @return Number  result    */
NAN_METHOD(GitClone::Into) {
  NanEscapableScope();

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

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

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

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

  if (args.Length() == 4 || !args[4]->IsObject()) {
    return NanThrowError("Signature signature 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
  git_remote * from_remote;
  from_remote = ObjectWrap::Unwrap<GitRemote>(args[1]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const git_checkout_options * from_co_opts;
  from_co_opts = ObjectWrap::Unwrap<GitCheckoutOptions>(args[2]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_branch;

  String::Utf8Value branch(args[3]->ToString());
  from_branch = (const char *) strdup(*branch);
// end convert_from_v8 block
// start convert_from_v8 block
  const git_signature * from_signature;
  from_signature = ObjectWrap::Unwrap<GitSignature>(args[4]->ToObject())->GetValue();
// end convert_from_v8 block
  int result = git_clone_into(
from_repo
,from_remote
,from_co_opts
,from_branch
,from_signature
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
 * @param Repository repo
   * @param Remote remote
   * @param CheckoutOptions co_opts
   * @param String branch
   * @param Number link
   * @param Signature signature
     * @return Number  result    */
NAN_METHOD(GitClone::LocalInto) {
  NanEscapableScope();

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

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

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

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

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

  if (args.Length() == 5 || !args[5]->IsObject()) {
    return NanThrowError("Signature signature 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
  git_remote * from_remote;
  from_remote = ObjectWrap::Unwrap<GitRemote>(args[1]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const git_checkout_options * from_co_opts;
  from_co_opts = ObjectWrap::Unwrap<GitCheckoutOptions>(args[2]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_branch;

  String::Utf8Value branch(args[3]->ToString());
  from_branch = (const char *) strdup(*branch);
// end convert_from_v8 block
// start convert_from_v8 block
  int from_link;
  from_link = (int)   args[4]->ToNumber()->Value();
// end convert_from_v8 block
// start convert_from_v8 block
  const git_signature * from_signature;
  from_signature = ObjectWrap::Unwrap<GitSignature>(args[5]->ToObject())->GetValue();
// end convert_from_v8 block
  int result = git_clone_local_into(
from_repo
,from_remote
,from_co_opts
,from_branch
,from_link
,from_signature
  );

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