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

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

#include <iostream>
#include "../include/functions/copy.h"
#include "../include/blame_options.h"

  #include "../include/oid.h"
 
using namespace v8;
using namespace node;
using namespace std;


// generated from struct_content.cc
GitBlameOptions::GitBlameOptions() {
  git_blame_options wrappedValue = GIT_BLAME_OPTIONS_INIT;
  this->raw = (git_blame_options*) malloc(sizeof(git_blame_options));
  memcpy(this->raw, &wrappedValue, sizeof(git_blame_options));

  this->ConstructFields();
  this->selfFreeing = true;
}

GitBlameOptions::GitBlameOptions(git_blame_options* raw, bool selfFreeing) {
  this->raw = raw;
  this->ConstructFields();
  this->selfFreeing = selfFreeing;
}

GitBlameOptions::~GitBlameOptions() {
  // This is going to cause memory leaks. We'll have to solve that later
  // TODO: Clean up memory better
  if (this->selfFreeing) {
    free(this->raw);
  }
}

void GitBlameOptions::ConstructFields() {
                   Local<Object> newest_commitTemp = GitOid::New(
            &this->raw->newest_commit,
            false
          )->ToObject();
          NanAssignPersistent(this->newest_commit, newest_commitTemp);

             Local<Object> oldest_commitTemp = GitOid::New(
            &this->raw->oldest_commit,
            false
          )->ToObject();
          NanAssignPersistent(this->oldest_commit, oldest_commitTemp);

          }

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

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

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

      tpl->InstanceTemplate()->SetAccessor(NanNew<String>("version"), GetVersion, SetVersion);
       tpl->InstanceTemplate()->SetAccessor(NanNew<String>("flags"), GetFlags, SetFlags);
       tpl->InstanceTemplate()->SetAccessor(NanNew<String>("minMatchCharacters"), GetMinMatchCharacters, SetMinMatchCharacters);
       tpl->InstanceTemplate()->SetAccessor(NanNew<String>("newestCommit"), GetNewestCommit, SetNewestCommit);
       tpl->InstanceTemplate()->SetAccessor(NanNew<String>("oldestCommit"), GetOldestCommit, SetOldestCommit);
       tpl->InstanceTemplate()->SetAccessor(NanNew<String>("minLine"), GetMinLine, SetMinLine);
       tpl->InstanceTemplate()->SetAccessor(NanNew<String>("maxLine"), GetMaxLine, SetMaxLine);
  
  Local<Function> _constructor_template = tpl->GetFunction();
  NanAssignPersistent(constructor_template, _constructor_template);
  target->Set(NanNew<String>("BlameOptions"), _constructor_template);
}

NAN_METHOD(GitBlameOptions::JSNewFunction) {
  NanScope();
  GitBlameOptions* instance;

  if (args.Length() == 0 || !args[0]->IsExternal()) {
    instance = new GitBlameOptions();
  }
  else {
    instance = new GitBlameOptions(static_cast<git_blame_options*>(Handle<External>::Cast(args[0])->Value()), args[1]->BooleanValue());
  }

  instance->Wrap(args.This());

  NanReturnValue(args.This());
}

Handle<Value> GitBlameOptions::New(void* raw, bool selfFreeing) {
  NanEscapableScope();

  Handle<Value> argv[2] = { NanNew<External>((void *)raw), NanNew<Boolean>(selfFreeing) };
  return NanEscapeScope(NanNew<Function>(GitBlameOptions::constructor_template)->NewInstance(2, argv));
}

git_blame_options *GitBlameOptions::GetValue() {
  return this->raw;
}

git_blame_options **GitBlameOptions::GetRefValue() {
  return &this->raw;
}

    NAN_GETTER(GitBlameOptions::GetVersion) {
      NanScope();

      GitBlameOptions *wrapper = ObjectWrap::Unwrap<GitBlameOptions>(args.This());

        NanReturnValue(NanNew<Number>(wrapper->GetValue()->version));
     }

    NAN_SETTER(GitBlameOptions::SetVersion) {
      NanScope();

      GitBlameOptions *wrapper = ObjectWrap::Unwrap<GitBlameOptions>(args.This());

         if (value->IsNumber()) {
          wrapper->GetValue()->version = (unsigned int) value->Int32Value();
        }
     }

      NAN_GETTER(GitBlameOptions::GetFlags) {
      NanScope();

      GitBlameOptions *wrapper = ObjectWrap::Unwrap<GitBlameOptions>(args.This());

        NanReturnValue(NanNew<Number>(wrapper->GetValue()->flags));
     }

    NAN_SETTER(GitBlameOptions::SetFlags) {
      NanScope();

      GitBlameOptions *wrapper = ObjectWrap::Unwrap<GitBlameOptions>(args.This());

         if (value->IsNumber()) {
          wrapper->GetValue()->flags = (uint32_t) value->Int32Value();
        }
     }

      NAN_GETTER(GitBlameOptions::GetMinMatchCharacters) {
      NanScope();

      GitBlameOptions *wrapper = ObjectWrap::Unwrap<GitBlameOptions>(args.This());

        NanReturnValue(NanNew<Number>(wrapper->GetValue()->min_match_characters));
     }

    NAN_SETTER(GitBlameOptions::SetMinMatchCharacters) {
      NanScope();

      GitBlameOptions *wrapper = ObjectWrap::Unwrap<GitBlameOptions>(args.This());

         if (value->IsNumber()) {
          wrapper->GetValue()->min_match_characters = (uint16_t) value->Int32Value();
        }
     }

      NAN_GETTER(GitBlameOptions::GetNewestCommit) {
      NanScope();

      GitBlameOptions *wrapper = ObjectWrap::Unwrap<GitBlameOptions>(args.This());

        NanReturnValue(wrapper->newest_commit);

     }

    NAN_SETTER(GitBlameOptions::SetNewestCommit) {
      NanScope();

      GitBlameOptions *wrapper = ObjectWrap::Unwrap<GitBlameOptions>(args.This());

        NanDisposePersistent(wrapper->newest_commit);
        wrapper->raw->newest_commit = * ObjectWrap::Unwrap<GitOid>(value->ToObject())->GetValue();

     }

      NAN_GETTER(GitBlameOptions::GetOldestCommit) {
      NanScope();

      GitBlameOptions *wrapper = ObjectWrap::Unwrap<GitBlameOptions>(args.This());

        NanReturnValue(wrapper->oldest_commit);

     }

    NAN_SETTER(GitBlameOptions::SetOldestCommit) {
      NanScope();

      GitBlameOptions *wrapper = ObjectWrap::Unwrap<GitBlameOptions>(args.This());

        NanDisposePersistent(wrapper->oldest_commit);
        wrapper->raw->oldest_commit = * ObjectWrap::Unwrap<GitOid>(value->ToObject())->GetValue();

     }

      NAN_GETTER(GitBlameOptions::GetMinLine) {
      NanScope();

      GitBlameOptions *wrapper = ObjectWrap::Unwrap<GitBlameOptions>(args.This());

        NanReturnValue(NanNew<Number>(wrapper->GetValue()->min_line));
     }

    NAN_SETTER(GitBlameOptions::SetMinLine) {
      NanScope();

      GitBlameOptions *wrapper = ObjectWrap::Unwrap<GitBlameOptions>(args.This());

         if (value->IsNumber()) {
          wrapper->GetValue()->min_line = (uint32_t) value->Int32Value();
        }
     }

      NAN_GETTER(GitBlameOptions::GetMaxLine) {
      NanScope();

      GitBlameOptions *wrapper = ObjectWrap::Unwrap<GitBlameOptions>(args.This());

        NanReturnValue(NanNew<Number>(wrapper->GetValue()->max_line));
     }

    NAN_SETTER(GitBlameOptions::SetMaxLine) {
      NanScope();

      GitBlameOptions *wrapper = ObjectWrap::Unwrap<GitBlameOptions>(args.This());

         if (value->IsNumber()) {
          wrapper->GetValue()->max_line = (uint32_t) value->Int32Value();
        }
     }

   
Persistent<Function> GitBlameOptions::constructor_template;
