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

#include "../include/config.h"

#include <iostream>

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

GitConfig::GitConfig(git_config *raw, bool selfFreeing)
{
  this->raw = raw;
  this->selfFreeing = selfFreeing;
}

GitConfig::~GitConfig()
{
  if (this->selfFreeing) {
    git_config_free(this->raw);
  }
}

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

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

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

  NODE_SET_PROTOTYPE_METHOD(tpl, "setInt64", SetInt64);
  NODE_SET_PROTOTYPE_METHOD(tpl, "setMultivar", SetMultivar);
  NODE_SET_PROTOTYPE_METHOD(tpl, "setString", SetString);
  NODE_SET_PROTOTYPE_METHOD(tpl, "snapshot", Snapshot);

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

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

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

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

  NanReturnValue(args.This());
}

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

git_config *GitConfig::GetValue()
{
  return this->raw;
}

git_config **GitConfig::GetRefValue()
{
  return &this->raw;
}


/*
   * @param String name
   * @param Number value
     * @return Number  result    */
NAN_METHOD(GitConfig::SetInt64)
{
  NanEscapableScope();

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

  if (args.Length() == 1 || !args[1]->IsNumber()) {
    return NanThrowError("Number value 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
  int64_t from_value;
  from_value = (int64_t)   args[1]->ToNumber()->Value();
// end convert_from_v8 block
  int result = git_config_set_int64(
                 ObjectWrap::Unwrap<GitConfig>(args.This())->GetValue()
                 ,from_name
                 ,from_value
               );

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

/*
   * @param String name
   * @param String regexp
   * @param String value
     * @return Number  result    */
NAN_METHOD(GitConfig::SetMultivar)
{
  NanEscapableScope();

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

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

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

  String::Utf8Value regexp(args[1]->ToString());
  from_regexp = (const char *) strdup(*regexp);
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_value;

  String::Utf8Value value(args[2]->ToString());
  from_value = (const char *) strdup(*value);
// end convert_from_v8 block
  int result = git_config_set_multivar(
                 ObjectWrap::Unwrap<GitConfig>(args.This())->GetValue()
                 ,from_name
                 ,from_regexp
                 ,from_value
               );

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

/*
   * @param String name
   * @param String value
     * @return Number  result    */
NAN_METHOD(GitConfig::SetString)
{
  NanEscapableScope();

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

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

  String::Utf8Value value(args[1]->ToString());
  from_value = (const char *) strdup(*value);
// end convert_from_v8 block
  int result = git_config_set_string(
                 ObjectWrap::Unwrap<GitConfig>(args.This())->GetValue()
                 ,from_name
                 ,from_value
               );

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

/*
     * @param Config callback
   */
NAN_METHOD(GitConfig::Snapshot)
{
  NanScope();

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

  SnapshotBaton* baton = new SnapshotBaton;

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

  baton->config = ObjectWrap::Unwrap<GitConfig>(args.This())->GetValue();

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[0]));
  SnapshotWorker *worker = new SnapshotWorker(baton, callback);
  worker->SaveToPersistent("config", args.This());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitConfig::SnapshotWorker::Execute()
{
  int result = git_config_snapshot(
                 &baton->out,baton->config    );

  baton->error_code = result;

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

}

void GitConfig::SnapshotWorker::HandleOKCallback()
{
  TryCatch try_catch;

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

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