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

#include "../include/functions/copy.h"
#include "../include/remote.h"
#include "../include/strarray.h"
#include "../include/repository.h"
#include "../include/transport.h"
#include "../include/remote.h"
#include "../include/repository.h"
#include "../include/strarray.h"
#include "../include/transport.h"

using namespace v8;
using namespace node;

GitRemote::GitRemote(git_remote *raw) {
  this->raw = raw;
}

GitRemote::~GitRemote() {
    git_remote_free(this->raw);
  }

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

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

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

              NODE_SET_METHOD(tpl, "create", Create);
                        NODE_SET_METHOD(tpl, "load", Load);
                        NODE_SET_PROTOTYPE_METHOD(tpl, "name", Name);
                        NODE_SET_PROTOTYPE_METHOD(tpl, "url", Url);
                        NODE_SET_PROTOTYPE_METHOD(tpl, "pushurl", Pushurl);
                        NODE_SET_METHOD(tpl, "list", List);
                        NODE_SET_PROTOTYPE_METHOD(tpl, "rename", Rename);
                        NODE_SET_PROTOTYPE_METHOD(tpl, "delete", Delete);
            
  
  Local<Function> _constructor_template = tpl->GetFunction();
  NanAssignPersistent(constructor_template, _constructor_template);
  target->Set(NanNew<String>("Remote"), _constructor_template);
}

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

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

  NanReturnValue(args.This());
}

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

git_remote *GitRemote::GetValue() {
  return this->raw;
}

            /**
        * @param Repository repo
            * @param String name
            * @param String url
            * @param Remote callback
  */
NAN_METHOD(GitRemote::Create) {
  NanScope();
              if (args.Length() == 0 || !args[0]->IsObject()) {
      return NanThrowError("Repository repo is required.");
    }
                if (args.Length() == 1 || !args[1]->IsString()) {
      return NanThrowError("String name is required.");
    }
                if (args.Length() == 2 || !args[2]->IsString()) {
      return NanThrowError("String url is required.");
    }
      
  if (args.Length() == 3 || !args[3]->IsFunction()) {
    return NanThrowError("Callback is required and must be a Function.");
  }

  CreateBaton* baton = new CreateBaton;
  baton->error_code = GIT_OK;
  baton->error = NULL;
                      git_repository * from_repo;
          from_repo = ObjectWrap::Unwrap<GitRepository>(args[0]->ToObject())->GetValue();
  
            baton->repo = from_repo;
                                  const char * from_name;
          String::Utf8Value name(args[1]->ToString());
      from_name = (const char *) strdup(*name);
  
            baton->name = from_name;
                                  const char * from_url;
          String::Utf8Value url(args[2]->ToString());
      from_url = (const char *) strdup(*url);
  
            baton->url = from_url;
                    
  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[3]));
  CreateWorker *worker = new CreateWorker(baton, callback);
                    if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("repo", args[0]->ToObject());
                        if (!args[1]->IsUndefined() && !args[1]->IsNull())
    worker->SaveToPersistent("name", args[1]->ToObject());
                        if (!args[2]->IsUndefined() && !args[2]->IsNull())
    worker->SaveToPersistent("url", args[2]->ToObject());
            
  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRemote::CreateWorker::Execute() {
    int result = git_remote_create(
                    &baton->out,    
              baton->repo,    
              baton->name,    
              baton->url    
        );

    baton->error_code = result;

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

  }

void GitRemote::CreateWorker::HandleOKCallback() {
  TryCatch try_catch;
  if (baton->error_code == GIT_OK) {
        Handle<Value> to;
                      if (baton->out != NULL) {
  to = GitRemote::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->name);
                        free((void *)baton->url);
            
  delete baton;
}
                  /**
        * @param Repository repo
            * @param String name
            * @param Remote callback
  */
NAN_METHOD(GitRemote::Load) {
  NanScope();
              if (args.Length() == 0 || !args[0]->IsObject()) {
      return NanThrowError("Repository repo is required.");
    }
                if (args.Length() == 1 || !args[1]->IsString()) {
      return NanThrowError("String name is required.");
    }
      
  if (args.Length() == 2 || !args[2]->IsFunction()) {
    return NanThrowError("Callback is required and must be a Function.");
  }

  LoadBaton* baton = new LoadBaton;
  baton->error_code = GIT_OK;
  baton->error = NULL;
                      git_repository * from_repo;
          from_repo = ObjectWrap::Unwrap<GitRepository>(args[0]->ToObject())->GetValue();
  
            baton->repo = from_repo;
                                  const char * from_name;
          String::Utf8Value name(args[1]->ToString());
      from_name = (const char *) strdup(*name);
  
            baton->name = from_name;
                    
  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[2]));
  LoadWorker *worker = new LoadWorker(baton, callback);
                    if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("repo", args[0]->ToObject());
                        if (!args[1]->IsUndefined() && !args[1]->IsNull())
    worker->SaveToPersistent("name", args[1]->ToObject());
            
  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRemote::LoadWorker::Execute() {
    int result = git_remote_load(
                    &baton->out,    
              baton->repo,    
              baton->name    
        );

    baton->error_code = result;

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

  }

void GitRemote::LoadWorker::HandleOKCallback() {
  TryCatch try_catch;
  if (baton->error_code == GIT_OK) {
        Handle<Value> to;
                      if (baton->out != NULL) {
  to = GitRemote::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->name);
            
  delete baton;
}
                  /**
              * @return String result  */
NAN_METHOD(GitRemote::Name) {
  NanScope();
    
  
        
  const char * result = git_remote_name(
          ObjectWrap::Unwrap<GitRemote>(args.This())->GetValue()
            );

  

  Handle<Value> to;
          to = NanNew<String>(result);
  
            NanReturnValue(to);
  }
                  /**
              * @return String result  */
NAN_METHOD(GitRemote::Url) {
  NanScope();
    
  
        
  const char * result = git_remote_url(
          ObjectWrap::Unwrap<GitRemote>(args.This())->GetValue()
            );

  

  Handle<Value> to;
          to = NanNew<String>(result);
  
            NanReturnValue(to);
  }
                  /**
              * @return String result  */
NAN_METHOD(GitRemote::Pushurl) {
  NanScope();
    
  
        
  const char * result = git_remote_pushurl(
          ObjectWrap::Unwrap<GitRemote>(args.This())->GetValue()
            );

  

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

  ListBaton* baton = new ListBaton;
  baton->error_code = GIT_OK;
  baton->error = NULL;
        baton->out = (git_strarray *)malloc(sizeof(git_strarray ));
                    git_repository * from_repo;
          from_repo = ObjectWrap::Unwrap<GitRepository>(args[0]->ToObject())->GetValue();
  
            baton->repo = from_repo;
                    
  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[1]));
  ListWorker *worker = new ListWorker(baton, callback);
                    if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("repo", args[0]->ToObject());
            
  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRemote::ListWorker::Execute() {
    int result = git_remote_list(
                    baton->out,    
              baton->repo    
        );

    baton->error_code = result;

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

  }

void GitRemote::ListWorker::HandleOKCallback() {
  TryCatch try_catch;
  if (baton->error_code == GIT_OK) {
        Handle<Value> to;
                      if (baton->out != NULL) {
  to = GitStrarray::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);
    }

              free(baton->out);
                      }

  if (try_catch.HasCaught()) {
    node::FatalException(try_catch);
  }

              
  delete baton;
}
                  /**
      * @param Strarray problems
                    * @param String new_name
            * @return Int32 result  */
NAN_METHOD(GitRemote::Rename) {
  NanScope();
            if (args.Length() == 0 || !args[0]->IsObject()) {
      return NanThrowError("Strarray problems is required.");
    }
                  if (args.Length() == 1 || !args[1]->IsString()) {
      return NanThrowError("String new_name is required.");
    }
      
  
                      git_strarray * from_problems;
          from_problems = ObjectWrap::Unwrap<GitStrarray>(args[0]->ToObject())->GetValue();
  
                                        const char * from_new_name;
          String::Utf8Value new_name(args[1]->ToString());
      from_new_name = (const char *) strdup(*new_name);
  
              
  int result = git_remote_rename(
          from_problems
        ,          ObjectWrap::Unwrap<GitRemote>(args.This())->GetValue()
        ,          from_new_name
            );

            free((void *)from_new_name);
      

  Handle<Value> to;
          to = NanNew<Number>(result);
            NanReturnValue(to);
  }
                  /**
              * @return Int32 result  */
NAN_METHOD(GitRemote::Delete) {
  NanScope();
    
  
        
  int result = git_remote_delete(
          ObjectWrap::Unwrap<GitRemote>(args.This())->GetValue()
            );

  

  Handle<Value> to;
          to = NanNew<Number>(result);
            NanReturnValue(to);
  }
      

Persistent<Function> GitRemote::constructor_template;
