// 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/diff.h>
}


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

#include "../include/repository.h"
#include "../include/status_list.h"
#include "../include/diff_perfdata.h"

#include <iostream>

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


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

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

  NODE_SET_METHOD(object, "file", File);
  NODE_SET_METHOD(object, "listEntrycount", ListEntrycount);
  NODE_SET_METHOD(object, "listFree", ListFree);
  NODE_SET_METHOD(object, "listGetPerfdata", ListGetPerfdata);
  NODE_SET_METHOD(object, "shouldIgnore", ShouldIgnore);

  target->Set(NanNew<String>("Status"), object);
}


/*
 * @param Number status_flags
   * @param Repository repo
   * @param String path
     * @return Number  result    */
NAN_METHOD(GitStatus::File)
{
  NanEscapableScope();

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

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

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

// start convert_from_v8 block
  unsigned int * from_status_flags;
  *from_status_flags = (unsigned int)   args[0]->ToNumber()->Value();
// 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
  const char * from_path;

  String::Utf8Value path(args[2]->ToString());
  from_path = (const char *) strdup(*path);
// end convert_from_v8 block
  int result = git_status_file(
                 from_status_flags
                 ,from_repo
                 ,from_path
               );

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

/*
 * @param StatusList statuslist
     * @return Number  result    */
NAN_METHOD(GitStatus::ListEntrycount)
{
  NanEscapableScope();

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

// start convert_from_v8 block
  git_status_list * from_statuslist;
  from_statuslist = ObjectWrap::Unwrap<GitStatusList>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  size_t result = git_status_list_entrycount(
                    from_statuslist
                  );

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

/*
 * @param StatusList statuslist
     */
NAN_METHOD(GitStatus::ListFree)
{
  NanEscapableScope();

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

// start convert_from_v8 block
  git_status_list * from_statuslist;
  from_statuslist = ObjectWrap::Unwrap<GitStatusList>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  git_status_list_free(
    from_statuslist
  );
  NanReturnUndefined();
}

/*
  * @param StatusList status
    * @param DiffPerfdata callback
   */
NAN_METHOD(GitStatus::ListGetPerfdata)
{
  NanScope();

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

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

  ListGetPerfdataBaton* baton = new ListGetPerfdataBaton;

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

// start convert_from_v8 block
  const git_status_list * from_status;
  from_status = ObjectWrap::Unwrap<GitStatusList>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->status = from_status;

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

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitStatus::ListGetPerfdataWorker::Execute()
{
  int result = git_status_list_get_perfdata(
                 baton->out,baton->status    );

  baton->error_code = result;

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

}

void GitStatus::ListGetPerfdataWorker::HandleOKCallback()
{
  TryCatch try_catch;

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

    if (baton->out != NULL) {
      // GitDiffPerfdata baton->out
      to = GitDiffPerfdata::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 Number ignored
   * @param Repository repo
   * @param String path
     * @return Number  result    */
NAN_METHOD(GitStatus::ShouldIgnore)
{
  NanEscapableScope();

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

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

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

// start convert_from_v8 block
  int * from_ignored;
  *from_ignored = (int)   args[0]->ToNumber()->Value();
// 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
  const char * from_path;

  String::Utf8Value path(args[2]->ToString());
  from_path = (const char *) strdup(*path);
// end convert_from_v8 block
  int result = git_status_should_ignore(
                 from_ignored
                 ,from_repo
                 ,from_path
               );

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