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

#include "../include/pathspec_match_list.h"
#include "../include/diff.h"
#include "../include/pathspec.h"
#include "../include/index.h"
#include "../include/diff_delta.h"
#include "../include/tree.h"
#include "../include/repository.h"

#include <iostream>

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

GitPathspec::GitPathspec(git_pathspec *raw, bool selfFreeing)
{
  this->raw = raw;
  this->selfFreeing = selfFreeing;
}

GitPathspec::~GitPathspec()
{
  if (this->selfFreeing) {
    git_pathspec_free(this->raw);
  }
}

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

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

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

  NODE_SET_PROTOTYPE_METHOD(tpl, "matchDiff", MatchDiff);
  NODE_SET_PROTOTYPE_METHOD(tpl, "matchIndex", MatchIndex);
  NODE_SET_METHOD(tpl, "matchListDiffEntry", MatchListDiffEntry);
  NODE_SET_METHOD(tpl, "matchListEntry", MatchListEntry);
  NODE_SET_METHOD(tpl, "matchListEntrycount", MatchListEntrycount);
  NODE_SET_METHOD(tpl, "matchListFailedEntry", MatchListFailedEntry);
  NODE_SET_METHOD(tpl, "matchListFailedEntrycount", MatchListFailedEntrycount);
  NODE_SET_METHOD(tpl, "matchListFree", MatchListFree);
  NODE_SET_PROTOTYPE_METHOD(tpl, "matchTree", MatchTree);
  NODE_SET_PROTOTYPE_METHOD(tpl, "matchWorkdir", MatchWorkdir);
  NODE_SET_PROTOTYPE_METHOD(tpl, "matchesPath", MatchesPath);

  Local<Function> _constructor_template = tpl->GetFunction();
  NanAssignPersistent(constructor_template, _constructor_template);
  target->Set(NanNew<String>("Pathspec"), _constructor_template);
}

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

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

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

  NanReturnValue(args.This());
}

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

git_pathspec *GitPathspec::GetValue()
{
  return this->raw;
}

git_pathspec **GitPathspec::GetRefValue()
{
  return &this->raw;
}


/*
  * @param Diff diff
   * @param Number flags
      * @param PathspecMatchList callback
   */
NAN_METHOD(GitPathspec::MatchDiff)
{
  NanScope();

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

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

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

  MatchDiffBaton* baton = new MatchDiffBaton;

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

// start convert_from_v8 block
  git_diff * from_diff;
  from_diff = ObjectWrap::Unwrap<GitDiff>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->diff = from_diff;
// start convert_from_v8 block
  uint32_t from_flags;
  from_flags = (uint32_t)   args[1]->ToNumber()->Value();
// end convert_from_v8 block
  baton->flags = from_flags;
  baton->ps = ObjectWrap::Unwrap<GitPathspec>(args.This())->GetValue();

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[2]));
  MatchDiffWorker *worker = new MatchDiffWorker(baton, callback);
  if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("diff", args[0]->ToObject());
  if (!args[1]->IsUndefined() && !args[1]->IsNull())
    worker->SaveToPersistent("flags", args[1]->ToObject());
  worker->SaveToPersistent("ps", args.This());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitPathspec::MatchDiffWorker::Execute()
{
  int result = git_pathspec_match_diff(
                 &baton->out,baton->diff,baton->flags,baton->ps    );

  baton->error_code = result;

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

}

void GitPathspec::MatchDiffWorker::HandleOKCallback()
{
  TryCatch try_catch;

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

    if (baton->out != NULL) {
      // GitPathspecMatchList baton->out
      to = GitPathspecMatchList::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 Index index
   * @param Number flags
      * @param PathspecMatchList callback
   */
NAN_METHOD(GitPathspec::MatchIndex)
{
  NanScope();

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

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

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

  MatchIndexBaton* baton = new MatchIndexBaton;

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

// start convert_from_v8 block
  git_index * from_index;
  from_index = ObjectWrap::Unwrap<GitIndex>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->index = from_index;
// start convert_from_v8 block
  uint32_t from_flags;
  from_flags = (uint32_t)   args[1]->ToNumber()->Value();
// end convert_from_v8 block
  baton->flags = from_flags;
  baton->ps = ObjectWrap::Unwrap<GitPathspec>(args.This())->GetValue();

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[2]));
  MatchIndexWorker *worker = new MatchIndexWorker(baton, callback);
  if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("index", args[0]->ToObject());
  if (!args[1]->IsUndefined() && !args[1]->IsNull())
    worker->SaveToPersistent("flags", args[1]->ToObject());
  worker->SaveToPersistent("ps", args.This());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitPathspec::MatchIndexWorker::Execute()
{
  int result = git_pathspec_match_index(
                 &baton->out,baton->index,baton->flags,baton->ps    );

  baton->error_code = result;

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

}

void GitPathspec::MatchIndexWorker::HandleOKCallback()
{
  TryCatch try_catch;

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

    if (baton->out != NULL) {
      // GitPathspecMatchList baton->out
      to = GitPathspecMatchList::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 PathspecMatchList m
   * @param Number pos
     * @return DiffDelta  result    */
NAN_METHOD(GitPathspec::MatchListDiffEntry)
{
  NanEscapableScope();

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

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

// start convert_from_v8 block
  const git_pathspec_match_list * from_m;
  from_m = ObjectWrap::Unwrap<GitPathspecMatchList>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  size_t from_pos;
  from_pos = (size_t)   args[1]->ToNumber()->Value();
// end convert_from_v8 block
  const git_diff_delta * result = git_pathspec_match_list_diff_entry(
                                    from_m
                                    ,from_pos
                                  );
  // null checks on pointers
  if (!result) {
    NodeGitPsueodoNanReturnEscapingValue(NanUndefined());
  }

  Handle<Value> to;
// start convert_to_v8 block

  if (result != NULL) {
    // GitDiffDelta result
    to = GitDiffDelta::New((void *)result, false);
  } else {
    to = NanNull();
  }

// end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
 * @param PathspecMatchList m
   * @param Number pos
     * @return String  result    */
NAN_METHOD(GitPathspec::MatchListEntry)
{
  NanEscapableScope();

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

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

// start convert_from_v8 block
  const git_pathspec_match_list * from_m;
  from_m = ObjectWrap::Unwrap<GitPathspecMatchList>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  size_t from_pos;
  from_pos = (size_t)   args[1]->ToNumber()->Value();
// end convert_from_v8 block
  const char * result = git_pathspec_match_list_entry(
                          from_m
                          ,from_pos
                        );
  // null checks on pointers
  if (!result) {
    NodeGitPsueodoNanReturnEscapingValue(NanUndefined());
  }

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

  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
 * @param PathspecMatchList m
     * @return Number  result    */
NAN_METHOD(GitPathspec::MatchListEntrycount)
{
  NanEscapableScope();

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

// start convert_from_v8 block
  const git_pathspec_match_list * from_m;
  from_m = ObjectWrap::Unwrap<GitPathspecMatchList>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  size_t result = git_pathspec_match_list_entrycount(
                    from_m
                  );

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

/*
 * @param PathspecMatchList m
   * @param Number pos
     * @return String  result    */
NAN_METHOD(GitPathspec::MatchListFailedEntry)
{
  NanEscapableScope();

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

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

// start convert_from_v8 block
  const git_pathspec_match_list * from_m;
  from_m = ObjectWrap::Unwrap<GitPathspecMatchList>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  size_t from_pos;
  from_pos = (size_t)   args[1]->ToNumber()->Value();
// end convert_from_v8 block
  const char * result = git_pathspec_match_list_failed_entry(
                          from_m
                          ,from_pos
                        );
  // null checks on pointers
  if (!result) {
    NodeGitPsueodoNanReturnEscapingValue(NanUndefined());
  }

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

  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
 * @param PathspecMatchList m
     * @return Number  result    */
NAN_METHOD(GitPathspec::MatchListFailedEntrycount)
{
  NanEscapableScope();

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

// start convert_from_v8 block
  const git_pathspec_match_list * from_m;
  from_m = ObjectWrap::Unwrap<GitPathspecMatchList>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  size_t result = git_pathspec_match_list_failed_entrycount(
                    from_m
                  );

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

/*
 * @param PathspecMatchList m
     */
NAN_METHOD(GitPathspec::MatchListFree)
{
  NanEscapableScope();

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

// start convert_from_v8 block
  git_pathspec_match_list * from_m;
  from_m = ObjectWrap::Unwrap<GitPathspecMatchList>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  git_pathspec_match_list_free(
    from_m
  );
  NanReturnUndefined();
}

/*
  * @param Tree tree
   * @param Number flags
      * @param PathspecMatchList callback
   */
NAN_METHOD(GitPathspec::MatchTree)
{
  NanScope();

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

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

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

  MatchTreeBaton* baton = new MatchTreeBaton;

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

// start convert_from_v8 block
  git_tree * from_tree;
  from_tree = ObjectWrap::Unwrap<GitTree>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->tree = from_tree;
// start convert_from_v8 block
  uint32_t from_flags;
  from_flags = (uint32_t)   args[1]->ToNumber()->Value();
// end convert_from_v8 block
  baton->flags = from_flags;
  baton->ps = ObjectWrap::Unwrap<GitPathspec>(args.This())->GetValue();

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[2]));
  MatchTreeWorker *worker = new MatchTreeWorker(baton, callback);
  if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("tree", args[0]->ToObject());
  if (!args[1]->IsUndefined() && !args[1]->IsNull())
    worker->SaveToPersistent("flags", args[1]->ToObject());
  worker->SaveToPersistent("ps", args.This());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitPathspec::MatchTreeWorker::Execute()
{
  int result = git_pathspec_match_tree(
                 &baton->out,baton->tree,baton->flags,baton->ps    );

  baton->error_code = result;

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

}

void GitPathspec::MatchTreeWorker::HandleOKCallback()
{
  TryCatch try_catch;

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

    if (baton->out != NULL) {
      // GitPathspecMatchList baton->out
      to = GitPathspecMatchList::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 Repository repo
   * @param Number flags
      * @param PathspecMatchList callback
   */
NAN_METHOD(GitPathspec::MatchWorkdir)
{
  NanScope();

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

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

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

  MatchWorkdirBaton* baton = new MatchWorkdirBaton;

  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
  uint32_t from_flags;
  from_flags = (uint32_t)   args[1]->ToNumber()->Value();
// end convert_from_v8 block
  baton->flags = from_flags;
  baton->ps = ObjectWrap::Unwrap<GitPathspec>(args.This())->GetValue();

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

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitPathspec::MatchWorkdirWorker::Execute()
{
  int result = git_pathspec_match_workdir(
                 &baton->out,baton->repo,baton->flags,baton->ps    );

  baton->error_code = result;

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

}

void GitPathspec::MatchWorkdirWorker::HandleOKCallback()
{
  TryCatch try_catch;

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

    if (baton->out != NULL) {
      // GitPathspecMatchList baton->out
      to = GitPathspecMatchList::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 Number flags
   * @param String path
     * @return Number  result    */
NAN_METHOD(GitPathspec::MatchesPath)
{
  NanEscapableScope();

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

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

// start convert_from_v8 block
  uint32_t from_flags;
  from_flags = (uint32_t)   args[0]->ToNumber()->Value();
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_path;

  String::Utf8Value path(args[1]->ToString());
  from_path = (const char *) strdup(*path);
// end convert_from_v8 block
  int result = git_pathspec_matches_path(
                 ObjectWrap::Unwrap<GitPathspec>(args.This())->GetValue()
                 ,from_flags
                 ,from_path
               );

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