// This is a generated file, modify: generate/templates/class_content.cc.
#include <nan.h>
#include <string.h>

extern "C" {
  #include <git2.h>
    #include <git2/sys/filter.h>
 }


#include "../include/functions/copy.h"
#include "../include/macros.h"
#include "../include/filter.h"

  #include "../include/filter_list.h"
  #include "../include/repository.h"
  #include "../include/filter.h"
 
#include <iostream>

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

  GitFilter::GitFilter(git_filter *raw, bool selfFreeing) {
    this->raw = raw;
    this->selfFreeing = selfFreeing;
  }

  GitFilter::~GitFilter() {
   }

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

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

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

           NODE_SET_METHOD(tpl, "listLength", ListLength);
             NODE_SET_METHOD(tpl, "listNew", ListNew);
            NODE_SET_PROTOTYPE_METHOD(tpl, "lookup", Lookup);
            NODE_SET_PROTOTYPE_METHOD(tpl, "register", Register);
             NODE_SET_METHOD(tpl, "unregister", Unregister);
           NODE_SET_PROTOTYPE_METHOD(tpl, "version", Version);
         NODE_SET_PROTOTYPE_METHOD(tpl, "attributes", Attributes);
  
    Local<Function> _constructor_template = tpl->GetFunction();
    NanAssignPersistent(constructor_template, _constructor_template);
    target->Set(NanNew<String>("Filter"), _constructor_template);
  }

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

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

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

    NanReturnValue(args.This());
  }

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

  git_filter *GitFilter::GetValue() {
    return this->raw;
  }

  git_filter **GitFilter::GetRefValue() {
    return &this->raw;
  }

  
/*
 * @param FilterList fl
     * @return Number  result    */
NAN_METHOD(GitFilter::ListLength) {
  NanEscapableScope();

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

// start convert_from_v8 block
  const git_filter_list * from_fl;
  from_fl = ObjectWrap::Unwrap<GitFilterList>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  size_t result = git_filter_list_length(
from_fl
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
  
/*
  * @param Repository repo
   * @param Number mode
   * @param Number options
    * @param FilterList callback
   */
NAN_METHOD(GitFilter::ListNew) {
  NanScope();

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

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

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

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

  ListNewBaton* baton = new ListNewBaton;

  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_filter_mode_t from_mode;
  from_mode = (git_filter_mode_t)  (int) args[1]->ToNumber()->Value();
// end convert_from_v8 block
  baton->mode = from_mode;
// start convert_from_v8 block
  uint32_t from_options;
  from_options = (uint32_t)   args[2]->ToNumber()->Value();
// end convert_from_v8 block
  baton->options = from_options;

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

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitFilter::ListNewWorker::Execute() {
  int result = git_filter_list_new(
&baton->out,baton->repo,baton->mode,baton->options    );

  baton->error_code = result;

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

}

void GitFilter::ListNewWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->out != NULL) {
    // GitFilterList baton->out
       to = GitFilterList::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 String name
     * @return Filter  result    */
NAN_METHOD(GitFilter::Lookup) {
  NanEscapableScope();

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

// start convert_from_v8 block
  const char * from_name;

  String::Utf8Value name(args[0]->ToString());
  from_name = (const char *) strdup(*name);
// end convert_from_v8 block
  git_filter * result = git_filter_lookup(
from_name
  );
  // null checks on pointers
  if (!result) {
    NodeGitPsueodoNanReturnEscapingValue(NanUndefined());
  }

  Handle<Value> to;
// start convert_to_v8 block
  
  if (result != NULL) {
    // GitFilter result
       to = GitFilter::New((void *)result, false);
   }
  else {
    to = NanNull();
  }

 // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
 * @param String name
     * @param Number priority
     * @return Number  result    */
NAN_METHOD(GitFilter::Register) {
  NanEscapableScope();

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

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

// start convert_from_v8 block
  const char * from_name;

  String::Utf8Value name(args[0]->ToString());
  from_name = (const char *) strdup(*name);
// end convert_from_v8 block
// start convert_from_v8 block
  int from_priority;
  from_priority = (int)   args[1]->ToNumber()->Value();
// end convert_from_v8 block
  int result = git_filter_register(
from_name
,ObjectWrap::Unwrap<GitFilter>(args.This())->GetValue()
,from_priority
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
 * @param String name
     * @return Number  result    */
NAN_METHOD(GitFilter::Unregister) {
  NanEscapableScope();

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

// start convert_from_v8 block
  const char * from_name;

  String::Utf8Value name(args[0]->ToString());
  from_name = (const char *) strdup(*name);
// end convert_from_v8 block
  int result = git_filter_unregister(
from_name
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
       NAN_METHOD(GitFilter::Version) {
      NanScope();
      Handle<Value> to;

      unsigned int  version =
 ObjectWrap::Unwrap<GitFilter>(args.This())->GetValue()->version;

// start convert_to_v8 block
     to = NanNew<Number>(version);
  // end convert_to_v8 block
      NanReturnValue(to);
    }
     NAN_METHOD(GitFilter::Attributes) {
      NanScope();
      Handle<Value> to;

      const char *  attributes =
 ObjectWrap::Unwrap<GitFilter>(args.This())->GetValue()->attributes;

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

  // end convert_to_v8 block
      NanReturnValue(to);
    }
    Persistent<Function> GitFilter::constructor_template;
 