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

#include "../include/repository.h"
#include "../include/commit.h"
#include "../include/revert_options.h"
#include "../include/index.h"
#include "../include/merge_options.h"

#include <iostream>

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


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

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

  NODE_SET_METHOD(object, "revert", Revert);
  NODE_SET_METHOD(object, "commit", Commit);
  NODE_SET_METHOD(object, "initOptions", InitOptions);

  target->Set(NanNew<String>("Revert"), object);
}


/*
 * @param Repository repo
   * @param Commit commit
   * @param RevertOptions given_opts
     * @return Number  result    */
NAN_METHOD(GitRevert::Revert)
{
  NanEscapableScope();

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

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

  if (args.Length() == 2 || !args[2]->IsObject()) {
    return NanThrowError("RevertOptions given_opts 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_commit * from_commit;
  from_commit = ObjectWrap::Unwrap<GitCommit>(args[1]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const git_revert_options * from_given_opts;
  from_given_opts = ObjectWrap::Unwrap<GitRevertOptions>(args[2]->ToObject())->GetValue();
// end convert_from_v8 block
  int result = git_revert(
                 from_repo
                 ,from_commit
                 ,from_given_opts
               );

  Handle<Value> to;
// start convert_to_v8 block
  to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
  * @param Repository repo
   * @param Commit revert_commit
   * @param Commit our_commit
   * @param Number mainline
   * @param MergeOptions merge_options
    * @param Index callback
   */
NAN_METHOD(GitRevert::Commit)
{
  NanScope();

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

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

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

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

  if (args.Length() == 4 || !args[4]->IsObject()) {
    return NanThrowError("MergeOptions merge_options is required.");
  }

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

  CommitBaton* baton = new CommitBaton;

  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
  git_commit * from_revert_commit;
  from_revert_commit = ObjectWrap::Unwrap<GitCommit>(args[1]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->revert_commit = from_revert_commit;
// start convert_from_v8 block
  git_commit * from_our_commit;
  from_our_commit = ObjectWrap::Unwrap<GitCommit>(args[2]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->our_commit = from_our_commit;
// start convert_from_v8 block
  unsigned int from_mainline;
  from_mainline = (unsigned int)   args[3]->ToNumber()->Value();
// end convert_from_v8 block
  baton->mainline = from_mainline;
// start convert_from_v8 block
  const git_merge_options * from_merge_options;
  from_merge_options = ObjectWrap::Unwrap<GitMergeOptions>(args[4]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->merge_options = from_merge_options;

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[5]));
  CommitWorker *worker = new CommitWorker(baton, callback);
  if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("repo", args[0]->ToObject());
  if (!args[1]->IsUndefined() && !args[1]->IsNull())
    worker->SaveToPersistent("revert_commit", args[1]->ToObject());
  if (!args[2]->IsUndefined() && !args[2]->IsNull())
    worker->SaveToPersistent("our_commit", args[2]->ToObject());
  if (!args[3]->IsUndefined() && !args[3]->IsNull())
    worker->SaveToPersistent("mainline", args[3]->ToObject());
  if (!args[4]->IsUndefined() && !args[4]->IsNull())
    worker->SaveToPersistent("merge_options", args[4]->ToObject());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRevert::CommitWorker::Execute()
{
  int result = git_revert_commit(
                 &baton->out,baton->repo,baton->revert_commit,baton->our_commit,baton->mainline,baton->merge_options    );

  baton->error_code = result;

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

}

void GitRevert::CommitWorker::HandleOKCallback()
{
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block

    if (baton->out != NULL) {
      // GitIndex baton->out
      to = GitIndex::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);
  }


  delete baton;
}

/*
 * @param RevertOptions opts
   * @param Number version
     * @return Number  result    */
NAN_METHOD(GitRevert::InitOptions)
{
  NanEscapableScope();

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

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

// start convert_from_v8 block
  git_revert_options * from_opts;
  from_opts = ObjectWrap::Unwrap<GitRevertOptions>(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_revert_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> GitRevert::constructor_template;
