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

#include "../include/delta.h"
#include "../include/diff_file.h"
#include "../include/diff_line.h"

using namespace v8;
using namespace node;

GitPatch::GitPatch(git_patch *raw) {
  this->raw = raw;
}

GitPatch::~GitPatch() {
  }

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

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

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

              NODE_SET_PROTOTYPE_METHOD(tpl, "getDelta", GetDelta);
                        NODE_SET_METHOD(tpl, "fromDiff", FromDiff);
                        NODE_SET_PROTOTYPE_METHOD(tpl, "numHunks", NumHunks);
                        NODE_SET_PROTOTYPE_METHOD(tpl, "numLinesInHunk", NumLinesInHunk);
                        NODE_SET_PROTOTYPE_METHOD(tpl, "getLineInHunk", GetLineInHunk);
            
  
  Local<Function> _constructor_template = tpl->GetFunction();
  NanAssignPersistent(constructor_template, _constructor_template);
  target->Set(NanNew<String>("Patch"), _constructor_template);
}

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

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

  NanReturnValue(args.This());
}

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

git_patch *GitPatch::GetValue() {
  return this->raw;
}

            /**
              * @return Delta result  */
NAN_METHOD(GitPatch::GetDelta) {
  NanScope();
    
  
        
  const git_diff_delta * result = git_patch_get_delta(
          ObjectWrap::Unwrap<GitPatch>(args.This())->GetValue()
            );

  

  Handle<Value> to;
          if (result != NULL) {
  result = (const git_diff_delta * )git_diff_delta_dup(result);
}
  if (result != NULL) {
  to = GitDelta::New((void *)result);
} else {
  to = NanNull();
}
          NanReturnValue(to);
  }
                  /**
        * @param Diff diff
            * @param Number idx
            * @return Patch out  */
NAN_METHOD(GitPatch::FromDiff) {
  NanScope();
              if (args.Length() == 0 || !args[0]->IsObject()) {
      return NanThrowError("Diff diff is required.");
    }
                if (args.Length() == 1 || !args[1]->IsUint32()) {
      return NanThrowError("Number idx is required.");
    }
      
        git_patch * out = 0;
      
                                      git_diff * from_diff;
          from_diff = ObjectWrap::Unwrap<GitDiff>(args[0]->ToObject())->GetValue();
  
                                  size_t from_idx;
          from_idx = (size_t)   args[1]->ToUint32()->Value();
  
              
  int result = git_patch_from_diff(
            &        out
        ,          from_diff
        ,          from_idx
            );

      
  if (result != GIT_OK) {
                    
    if (giterr_last()) {
      return NanThrowError(giterr_last()->message);
    } else {
      return NanThrowError("Unknown Error");
    }
  }

  Handle<Value> to;
          if (out != NULL) {
  to = GitPatch::New((void *)out);
} else {
  to = NanNull();
}
          NanReturnValue(to);
  }
                  /**
              * @return Number result  */
NAN_METHOD(GitPatch::NumHunks) {
  NanScope();
    
  
        
  size_t result = git_patch_num_hunks(
          ObjectWrap::Unwrap<GitPatch>(args.This())->GetValue()
            );

  

  Handle<Value> to;
          to = NanNew<Uint32>((uint32_t)result);
            NanReturnValue(to);
  }
                  /**
              * @param Number hunk_idx
            * @return Int32 result  */
NAN_METHOD(GitPatch::NumLinesInHunk) {
  NanScope();
              if (args.Length() == 0 || !args[0]->IsUint32()) {
      return NanThrowError("Number hunk_idx is required.");
    }
      
  
                            size_t from_hunk_idx;
          from_hunk_idx = (size_t)   args[0]->ToUint32()->Value();
  
              
  int result = git_patch_num_lines_in_hunk(
          ObjectWrap::Unwrap<GitPatch>(args.This())->GetValue()
        ,          from_hunk_idx
            );

    

  Handle<Value> to;
          to = NanNew<Number>(result);
            NanReturnValue(to);
  }
                  /**
                * @param Number hunk_idx
            * @param Number line_of_hunk
            * @return DiffLine out  */
NAN_METHOD(GitPatch::GetLineInHunk) {
  NanScope();
                if (args.Length() == 0 || !args[0]->IsUint32()) {
      return NanThrowError("Number hunk_idx is required.");
    }
                if (args.Length() == 1 || !args[1]->IsUint32()) {
      return NanThrowError("Number line_of_hunk is required.");
    }
      
        const git_diff_line * out = 0;
      
                                            size_t from_hunk_idx;
          from_hunk_idx = (size_t)   args[0]->ToUint32()->Value();
  
                                  size_t from_line_of_hunk;
          from_line_of_hunk = (size_t)   args[1]->ToUint32()->Value();
  
              
  int result = git_patch_get_line_in_hunk(
            &        out
        ,          ObjectWrap::Unwrap<GitPatch>(args.This())->GetValue()
        ,          from_hunk_idx
        ,          from_line_of_hunk
            );

        

  Handle<Value> to;
          if (out != NULL) {
  to = GitDiffLine::New((void *)out);
} else {
  to = NanNull();
}
          NanReturnValue(to);
  }
      

Persistent<Function> GitPatch::constructor_template;
