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

#include "../include/time.h"
#include "../include/signature.h"
#include "../include/repository.h"

#include <iostream>

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

GitSignature::GitSignature(git_signature *raw, bool selfFreeing)
{
  this->raw = raw;
  this->selfFreeing = selfFreeing;
}

GitSignature::~GitSignature()
{
  if (this->selfFreeing) {
    git_signature_free(this->raw);
  }
}

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

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

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

  NODE_SET_METHOD(tpl, "default", Default);
  NODE_SET_PROTOTYPE_METHOD(tpl, "dup", Dup);
  NODE_SET_METHOD(tpl, "create", Create);
  NODE_SET_METHOD(tpl, "now", Now);
  NODE_SET_PROTOTYPE_METHOD(tpl, "name", Name);
  NODE_SET_PROTOTYPE_METHOD(tpl, "email", Email);
  NODE_SET_PROTOTYPE_METHOD(tpl, "when", When);

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

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

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

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

  NanReturnValue(args.This());
}

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

git_signature *GitSignature::GetValue()
{
  return this->raw;
}

git_signature **GitSignature::GetRefValue()
{
  return &this->raw;
}


/*
  * @param Repository repo
     * @return Signature out    */
NAN_METHOD(GitSignature::Default)
{
  NanEscapableScope();

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

  git_signature * out = 0;
// start convert_from_v8 block
  git_repository * from_repo;
  from_repo = ObjectWrap::Unwrap<GitRepository>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  int result = git_signature_default(
                 &out
                 ,from_repo
               );
  if (result != GIT_OK) {

    if (giterr_last()) {
      return NanThrowError(giterr_last()->message);
    } else {
      return NanThrowError("Unknown Error");
    }
  }

  Handle<Value> to;
// start convert_to_v8 block

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

// end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
     * @param Signature callback
   */
NAN_METHOD(GitSignature::Dup)
{
  NanScope();

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

  DupBaton* baton = new DupBaton;

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

  baton->sig = ObjectWrap::Unwrap<GitSignature>(args.This())->GetValue();

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

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitSignature::DupWorker::Execute()
{
  int result = git_signature_dup(
                 &baton->dest,baton->sig    );

  baton->error_code = result;

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

}

void GitSignature::DupWorker::HandleOKCallback()
{
  TryCatch try_catch;

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

    if (baton->dest != NULL) {
      // GitSignature baton->dest
      to = GitSignature::New((void *)baton->dest, 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
   * @param String email
   * @param Number time
   * @param Number offset
     * @return Signature out    */
NAN_METHOD(GitSignature::Create)
{
  NanEscapableScope();

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

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

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

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

  git_signature * out = 0;
// 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_email;

  String::Utf8Value email(args[1]->ToString());
  from_email = (const char *) strdup(*email);
// end convert_from_v8 block
// start convert_from_v8 block
  git_time_t from_time;
  from_time = (git_time_t)  (int) args[2]->ToNumber()->Value();
// end convert_from_v8 block
// start convert_from_v8 block
  int from_offset;
  from_offset = (int)   args[3]->ToNumber()->Value();
// end convert_from_v8 block
  int result = git_signature_new(
                 &out
                 ,from_name
                 ,from_email
                 ,from_time
                 ,from_offset
               );
  if (result != GIT_OK) {

    if (giterr_last()) {
      return NanThrowError(giterr_last()->message);
    } else {
      return NanThrowError("Unknown Error");
    }
  }

  Handle<Value> to;
// start convert_to_v8 block

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

// end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
  * @param String name
   * @param String email
     * @return Signature out    */
NAN_METHOD(GitSignature::Now)
{
  NanEscapableScope();

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

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

  git_signature * out = 0;
// 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_email;

  String::Utf8Value email(args[1]->ToString());
  from_email = (const char *) strdup(*email);
// end convert_from_v8 block
  int result = git_signature_now(
                 &out
                 ,from_name
                 ,from_email
               );
  if (result != GIT_OK) {

    if (giterr_last()) {
      return NanThrowError(giterr_last()->message);
    } else {
      return NanThrowError("Unknown Error");
    }
  }

  Handle<Value> to;
// start convert_to_v8 block

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

// end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
NAN_METHOD(GitSignature::Name)
{
  NanScope();
  Handle<Value> to;

  char *  name =
    ObjectWrap::Unwrap<GitSignature>(args.This())->GetValue()->name;

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

  // end convert_to_v8 block
  NanReturnValue(to);
}
NAN_METHOD(GitSignature::Email)
{
  NanScope();
  Handle<Value> to;

  char *  email =
    ObjectWrap::Unwrap<GitSignature>(args.This())->GetValue()->email;

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

  // end convert_to_v8 block
  NanReturnValue(to);
}
NAN_METHOD(GitSignature::When)
{
  NanScope();
  Handle<Value> to;

  git_time * when =
    & ObjectWrap::Unwrap<GitSignature>(args.This())->GetValue()->when;

// start convert_to_v8 block

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

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