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

#include "../include/repository.h"
#include "../include/oid.h"
#include "../include/strarray.h"
#include "../include/object.h"

using namespace v8;
using namespace node;

GitRefs::GitRefs(git_reference *raw) {
  this->raw = raw;
}

GitRefs::~GitRefs() {
  }

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

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

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

              NODE_SET_METHOD(tpl, "lookup", Lookup);
                        NODE_SET_PROTOTYPE_METHOD(tpl, "target", Target);
                        NODE_SET_PROTOTYPE_METHOD(tpl, "type", Type);
            
  
  Local<Function> _constructor_template = tpl->GetFunction();
  NanAssignPersistent(constructor_template, _constructor_template);
  target->Set(NanNew<String>("Refs"), _constructor_template);
}

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

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

  NanReturnValue(args.This());
}

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

git_reference *GitRefs::GetValue() {
  return this->raw;
}

            /**
        * @param Repository repo
            * @param String name
            * @param Reference callback
  */
NAN_METHOD(GitRefs::Lookup) {
  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.");
  }

  LookupBaton* baton = new LookupBaton;
  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]));
  LookupWorker *worker = new LookupWorker(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 GitRefs::LookupWorker::Execute() {
    int result = git_reference_lookup(
                    &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 GitRefs::LookupWorker::HandleOKCallback() {
  TryCatch try_catch;
  if (baton->error_code == GIT_OK) {
        Handle<Value> to;
                      if (baton->out != NULL) {
  to = GitRefs::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 Oid result  */
NAN_METHOD(GitRefs::Target) {
  NanScope();
    
  
        
  const git_oid * result = git_reference_target(
          ObjectWrap::Unwrap<GitRefs>(args.This())->GetValue()
            );

  

  Handle<Value> to;
          if (result != NULL) {
  result = (const git_oid * )git_oid_dup(result);
}
  if (result != NULL) {
  to = GitOid::New((void *)result);
} else {
  to = NanNull();
}
          NanReturnValue(to);
  }
                  /**
              * @return Number result  */
NAN_METHOD(GitRefs::Type) {
  NanScope();
    
  
        
  git_ref_t result = git_reference_type(
          ObjectWrap::Unwrap<GitRefs>(args.This())->GetValue()
            );

  

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

Persistent<Function> GitRefs::constructor_template;
