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

#include "../include/push.h"
#include "../include/push_options.h"
#include "../include/remote.h"
#include "../include/signature.h"

#include <iostream>

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

GitPush::GitPush(git_push *raw, bool selfFreeing)
{
  this->raw = raw;
  this->selfFreeing = selfFreeing;
}

GitPush::~GitPush()
{
  if (this->selfFreeing) {
    git_push_free(this->raw);
  }
}

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

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

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

  NODE_SET_PROTOTYPE_METHOD(tpl, "addRefspec", AddRefspec);
  NODE_SET_PROTOTYPE_METHOD(tpl, "finish", Finish);
  NODE_SET_METHOD(tpl, "initOptions", InitOptions);
  NODE_SET_METHOD(tpl, "create", Create);
  NODE_SET_PROTOTYPE_METHOD(tpl, "setOptions", SetOptions);
  NODE_SET_PROTOTYPE_METHOD(tpl, "unpackOk", UnpackOk);
  NODE_SET_PROTOTYPE_METHOD(tpl, "updateTips", UpdateTips);

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

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

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

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

  NanReturnValue(args.This());
}

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

git_push *GitPush::GetValue()
{
  return this->raw;
}

git_push **GitPush::GetRefValue()
{
  return &this->raw;
}


/*
   * @param String refspec
     * @return Number  result    */
NAN_METHOD(GitPush::AddRefspec)
{
  NanEscapableScope();

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

// start convert_from_v8 block
  const char * from_refspec;

  String::Utf8Value refspec(args[0]->ToString());
  from_refspec = (const char *) strdup(*refspec);
// end convert_from_v8 block
  int result = git_push_add_refspec(
                 ObjectWrap::Unwrap<GitPush>(args.This())->GetValue()
                 ,from_refspec
               );

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

/*
     */
NAN_METHOD(GitPush::Finish)
{
  NanScope();

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

  FinishBaton* baton = new FinishBaton;

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

  baton->push = ObjectWrap::Unwrap<GitPush>(args.This())->GetValue();

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

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitPush::FinishWorker::Execute()
{
  int result = git_push_finish(
                 baton->push    );

  baton->error_code = result;

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

}

void GitPush::FinishWorker::HandleOKCallback()
{
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> result = NanUndefined();
    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 PushOptions opts
   * @param Number version
     * @return Number  result    */
NAN_METHOD(GitPush::InitOptions)
{
  NanEscapableScope();

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

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

// start convert_from_v8 block
  git_push_options * from_opts;
  from_opts = ObjectWrap::Unwrap<GitPushOptions>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  unsigned int from_version;
  from_version = (unsigned int)   args[1]->ToNumber()->Value();
// end convert_from_v8 block
  int result = git_push_init_options(
                 from_opts
                 ,from_version
               );

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

/*
  * @param Remote remote
    * @param Push callback
   */
NAN_METHOD(GitPush::Create)
{
  NanScope();

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

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

  CreateBaton* baton = new CreateBaton;

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

// start convert_from_v8 block
  git_remote * from_remote;
  from_remote = ObjectWrap::Unwrap<GitRemote>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->remote = from_remote;

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[1]));
  CreateWorker *worker = new CreateWorker(baton, callback);
  if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("remote", args[0]->ToObject());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitPush::CreateWorker::Execute()
{
  int result = git_push_new(
                 &baton->out,baton->remote    );

  baton->error_code = result;

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

}

void GitPush::CreateWorker::HandleOKCallback()
{
  TryCatch try_catch;

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

    if (baton->out != NULL) {
      // GitPush baton->out
      to = GitPush::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 PushOptions opts
     * @return Number  result    */
NAN_METHOD(GitPush::SetOptions)
{
  NanEscapableScope();

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

// start convert_from_v8 block
  const git_push_options * from_opts;
  from_opts = ObjectWrap::Unwrap<GitPushOptions>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  int result = git_push_set_options(
                 ObjectWrap::Unwrap<GitPush>(args.This())->GetValue()
                 ,from_opts
               );

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

/*
     * @return Number  result    */
NAN_METHOD(GitPush::UnpackOk)
{
  NanEscapableScope();

  int result = git_push_unpack_ok(
                 ObjectWrap::Unwrap<GitPush>(args.This())->GetValue()
               );

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

/*
   * @param Signature signature
   * @param String reflog_message
     * @return Number  result    */
NAN_METHOD(GitPush::UpdateTips)
{
  NanEscapableScope();

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

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

// start convert_from_v8 block
  const git_signature * from_signature;
  from_signature = ObjectWrap::Unwrap<GitSignature>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_reflog_message;

  String::Utf8Value reflog_message(args[1]->ToString());
  from_reflog_message = (const char *) strdup(*reflog_message);
// end convert_from_v8 block
  int result = git_push_update_tips(
                 ObjectWrap::Unwrap<GitPush>(args.This())->GetValue()
                 ,from_signature
                 ,from_reflog_message
               );

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