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

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

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

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

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

        NODE_SET_METHOD(object, "addMacro", AddMacro);
         NODE_SET_METHOD(object, "cacheFlush", CacheFlush);
         NODE_SET_METHOD(object, "get", Get);
         NODE_SET_METHOD(object, "getMany", GetMany);
  
    target->Set(NanNew<String>("Attr"), object);
  }

  
/*
 * @param Repository repo
   * @param String name
   * @param String values
     * @return Number  result    */
NAN_METHOD(GitAttr::AddMacro) {
  NanEscapableScope();

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

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

  if (args.Length() == 2 || !args[2]->IsString()) {
    return NanThrowError("String values 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
  const char * from_name;

  String::Utf8Value name(args[1]->ToString());
  from_name = (const char *) strdup(*name);
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_values;

  String::Utf8Value values(args[2]->ToString());
  from_values = (const char *) strdup(*values);
// end convert_from_v8 block
  int result = git_attr_add_macro(
from_repo
,from_name
,from_values
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
 * @param Repository repo
     */
NAN_METHOD(GitAttr::CacheFlush) {
  NanEscapableScope();

  if (args.Length() == 0 || !args[0]->IsObject()) {
    return NanThrowError("Repository repo 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
git_attr_cache_flush(
from_repo
  );
  NanReturnUndefined();
}
  
/*
  * @param Repository repo
   * @param Number flags
   * @param String path
   * @param String name
    * @param String callback
   */
NAN_METHOD(GitAttr::Get) {
  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]->IsString()) {
    return NanThrowError("String path is required.");
  }

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

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

  GetBaton* baton = new GetBaton;

  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;
// start convert_from_v8 block
  const char * from_path;

  String::Utf8Value path(args[2]->ToString());
  from_path = (const char *) strdup(*path);
// end convert_from_v8 block
  baton->path = from_path;
// start convert_from_v8 block
  const char * from_name;

  String::Utf8Value name(args[3]->ToString());
  from_name = (const char *) strdup(*name);
// end convert_from_v8 block
  baton->name = from_name;

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[4]));
  GetWorker *worker = new GetWorker(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());
  if (!args[2]->IsUndefined() && !args[2]->IsNull())
    worker->SaveToPersistent("path", args[2]->ToObject());
  if (!args[3]->IsUndefined() && !args[3]->IsNull())
    worker->SaveToPersistent("name", args[3]->ToObject());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitAttr::GetWorker::Execute() {
  int result = git_attr_get(
&baton->value_out,baton->repo,baton->flags,baton->path,baton->name    );

}

void GitAttr::GetWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  if (baton->value_out){
       to = NanNew<String>(baton->value_out);
   }
  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->value_out);
  free((void *)baton->path);
  free((void *)baton->name);

  delete baton;
}
   
/*
 * @param String values_out
   * @param Repository repo
   * @param Number flags
   * @param String path
   * @param Number num_attr
   * @param String names
     * @return Number  result    */
NAN_METHOD(GitAttr::GetMany) {
  NanEscapableScope();

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

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

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

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

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

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

// start convert_from_v8 block
  const char ** from_values_out;

  String::Utf8Value values_out(args[0]->ToString());
  from_values_out = (const char **) strdup(*values_out);
// end convert_from_v8 block
// start convert_from_v8 block
  git_repository * from_repo;
  from_repo = ObjectWrap::Unwrap<GitRepository>(args[1]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  uint32_t from_flags;
  from_flags = (uint32_t)   args[2]->ToNumber()->Value();
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_path;

  String::Utf8Value path(args[3]->ToString());
  from_path = (const char *) strdup(*path);
// end convert_from_v8 block
// start convert_from_v8 block
  size_t from_num_attr;
  from_num_attr = (size_t)   args[4]->ToNumber()->Value();
// end convert_from_v8 block
// start convert_from_v8 block
  const char ** from_names;

  String::Utf8Value names(args[5]->ToString());
  from_names = (const char **) strdup(*names);
// end convert_from_v8 block
  int result = git_attr_get_many(
from_values_out
,from_repo
,from_flags
,from_path
,from_num_attr
,from_names
  );

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