// This is a generated file, modify: generate/templates/class.cc.
#include <nan.h>
#include <string.h>

extern "C" {
#include <git2.h>
}

#include "../include/functions/copy.h"
#include "../include/repository.h"

#include "../include/functions/copy.h"
#include "../include/repository.h"
#include "../include/repository.h"
#include "../include/odb.h"
#include "../include/config.h"
#include "../include/refdb.h"
#include "../include/index.h"
#include "../include/oid.h"
#include "../include/repository_init_options.h"
#include "../include/repository.h"
#include "../include/odb.h"
#include "../include/repository_init_options.h"
#include "../include/config.h"
#include "../include/refdb.h"
#include "../include/index.h"
#include "../include/oid.h"

using namespace v8;
using namespace node;

GitRepository::GitRepository(git_repository *raw) {
  this->raw = raw;
}

GitRepository::~GitRepository() {
    git_repository_free(this->raw);
  }

void GitRepository::Initialize(Handle<v8::Object> target) {
  NanScope();

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

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

              NODE_SET_METHOD(tpl, "open", Open);
                        NODE_SET_METHOD(tpl, "init", Init);
                        NODE_SET_METHOD(tpl, "initInitOptions", InitInitOptions);
                        NODE_SET_METHOD(tpl, "initExt", InitExt);
                        NODE_SET_PROTOTYPE_METHOD(tpl, "index", Index);
            
  
  Local<Function> _constructor_template = tpl->GetFunction();
  NanAssignPersistent(constructor_template, _constructor_template);
  target->Set(NanNew<String>("Repository"), _constructor_template);
}

NAN_METHOD(GitRepository::New) {
  NanScope();

  if (args.Length() == 0 || !args[0]->IsExternal()) {
    return NanThrowError("git_repository is required.");
  }
  GitRepository* object = new GitRepository(static_cast<git_repository *>(Handle<External>::Cast(args[0])->Value()));
  object->Wrap(args.This());

  NanReturnValue(args.This());
}

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

git_repository *GitRepository::GetValue() {
  return this->raw;
}

            /**
        * @param String path
            * @param Repository callback
  */
NAN_METHOD(GitRepository::Open) {
  NanScope();
              if (args.Length() == 0 || !args[0]->IsString()) {
      return NanThrowError("String path is required.");
    }
      
  if (args.Length() == 1 || !args[1]->IsFunction()) {
    return NanThrowError("Callback is required and must be a Function.");
  }

  OpenBaton* baton = new OpenBaton;
  baton->error_code = GIT_OK;
  baton->error = NULL;
                      const char * from_path;
          String::Utf8Value path(args[0]->ToString());
      from_path = (const char *) strdup(*path);
  
            baton->path = from_path;
                    
  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[1]));
  OpenWorker *worker = new OpenWorker(baton, callback);
                    if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("path", args[0]->ToObject());
            
  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRepository::OpenWorker::Execute() {
    int result = git_repository_open(
                    &baton->out,    
              baton->path    
        );

    baton->error_code = result;

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

  }

void GitRepository::OpenWorker::HandleOKCallback() {
  TryCatch try_catch;
  if (baton->error_code == GIT_OK) {
        Handle<Value> to;
                      if (baton->out != NULL) {
  to = GitRepository::New((void *)baton->out);
} else {
  to = NanNull();
}
                        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->path);
            
  delete baton;
}
                  /**
        * @param String path
            * @param Uint32 is_bare
            * @param Repository callback
  */
NAN_METHOD(GitRepository::Init) {
  NanScope();
              if (args.Length() == 0 || !args[0]->IsString()) {
      return NanThrowError("String path is required.");
    }
                if (args.Length() == 1 || !args[1]->IsNumber()) {
      return NanThrowError("Uint32 is_bare is required.");
    }
      
  if (args.Length() == 2 || !args[2]->IsFunction()) {
    return NanThrowError("Callback is required and must be a Function.");
  }

  InitBaton* baton = new InitBaton;
  baton->error_code = GIT_OK;
  baton->error = NULL;
                      const char * from_path;
          String::Utf8Value path(args[0]->ToString());
      from_path = (const char *) strdup(*path);
  
            baton->path = from_path;
                                  unsigned int from_is_bare;
          from_is_bare = (unsigned int)   args[1]->ToNumber()->Value();
  
            baton->is_bare = from_is_bare;
                    
  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[2]));
  InitWorker *worker = new InitWorker(baton, callback);
                    if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("path", args[0]->ToObject());
                        if (!args[1]->IsUndefined() && !args[1]->IsNull())
    worker->SaveToPersistent("is_bare", args[1]->ToObject());
            
  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRepository::InitWorker::Execute() {
    int result = git_repository_init(
                    &baton->out,    
              baton->path,    
              baton->is_bare    
        );

    baton->error_code = result;

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

  }

void GitRepository::InitWorker::HandleOKCallback() {
  TryCatch try_catch;
  if (baton->error_code == GIT_OK) {
        Handle<Value> to;
                      if (baton->out != NULL) {
  to = GitRepository::New((void *)baton->out);
} else {
  to = NanNull();
}
                        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->path);
                  
  delete baton;
}
                  /**
      * @param RepositoryInitOptions opts
            * @param Uint32 version
            * @return Int32 result  */
NAN_METHOD(GitRepository::InitInitOptions) {
  NanScope();
            if (args.Length() == 0 || !args[0]->IsObject()) {
      return NanThrowError("RepositoryInitOptions opts is required.");
    }
                if (args.Length() == 1 || !args[1]->IsNumber()) {
      return NanThrowError("Uint32 version is required.");
    }
      
  
                      git_repository_init_options * from_opts;
          from_opts = ObjectWrap::Unwrap<GitRepositoryInitOptions>(args[0]->ToObject())->GetValue();
  
                                  unsigned int from_version;
          from_version = (unsigned int)   args[1]->ToNumber()->Value();
  
              
  int result = git_repository_init_init_options(
          from_opts
        ,          from_version
            );

    

  Handle<Value> to;
          to = NanNew<Number>(result);
            NanReturnValue(to);
  }
                  /**
        * @param String repo_path
            * @param RepositoryInitOptions opts
            * @param Repository callback
  */
NAN_METHOD(GitRepository::InitExt) {
  NanScope();
              if (args.Length() == 0 || !args[0]->IsString()) {
      return NanThrowError("String repo_path is required.");
    }
                if (args.Length() == 1 || !args[1]->IsObject()) {
      return NanThrowError("RepositoryInitOptions opts is required.");
    }
      
  if (args.Length() == 2 || !args[2]->IsFunction()) {
    return NanThrowError("Callback is required and must be a Function.");
  }

  InitExtBaton* baton = new InitExtBaton;
  baton->error_code = GIT_OK;
  baton->error = NULL;
                      const char * from_repo_path;
          String::Utf8Value repo_path(args[0]->ToString());
      from_repo_path = (const char *) strdup(*repo_path);
  
            baton->repo_path = from_repo_path;
                                  git_repository_init_options * from_opts;
          from_opts = ObjectWrap::Unwrap<GitRepositoryInitOptions>(args[1]->ToObject())->GetValue();
  
            baton->opts = from_opts;
                    
  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[2]));
  InitExtWorker *worker = new InitExtWorker(baton, callback);
                    if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("repo_path", args[0]->ToObject());
                        if (!args[1]->IsUndefined() && !args[1]->IsNull())
    worker->SaveToPersistent("opts", args[1]->ToObject());
            
  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRepository::InitExtWorker::Execute() {
    int result = git_repository_init_ext(
                    &baton->out,    
              baton->repo_path,    
              baton->opts    
        );

    baton->error_code = result;

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

  }

void GitRepository::InitExtWorker::HandleOKCallback() {
  TryCatch try_catch;
  if (baton->error_code == GIT_OK) {
        Handle<Value> to;
                      if (baton->out != NULL) {
  to = GitRepository::New((void *)baton->out);
} else {
  to = NanNull();
}
                        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->repo_path);
                  
  delete baton;
}
                  /**
                * @param Index callback
  */
NAN_METHOD(GitRepository::Index) {
  NanScope();
      
  if (args.Length() == 0 || !args[0]->IsFunction()) {
    return NanThrowError("Callback is required and must be a Function.");
  }

  IndexBaton* baton = new IndexBaton;
  baton->error_code = GIT_OK;
  baton->error = NULL;
                    baton->repo = ObjectWrap::Unwrap<GitRepository>(args.This())->GetValue();
            
  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[0]));
  IndexWorker *worker = new IndexWorker(baton, callback);
                    worker->SaveToPersistent("repo", args.This());
            
  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRepository::IndexWorker::Execute() {
    int result = git_repository_index(
                    &baton->out,    
              baton->repo    
        );

    baton->error_code = result;

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

  }

void GitRepository::IndexWorker::HandleOKCallback() {
  TryCatch try_catch;
  if (baton->error_code == GIT_OK) {
        Handle<Value> to;
                      if (baton->out != NULL) {
  to = GitIndex::New((void *)baton->out);
} else {
  to = NanNull();
}
                        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> GitRepository::constructor_template;
