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

#include "../include/merge_options.h"
#include "../include/checkout_options.h"

using namespace v8;
using namespace node;
using namespace std;


// generated from struct_content.cc
GitRevertOptions::GitRevertOptions()
{
  git_revert_options wrappedValue = GIT_REVERT_OPTIONS_INIT;
  this->raw = (git_revert_options*) malloc(sizeof(git_revert_options));
  memcpy(this->raw, &wrappedValue, sizeof(git_revert_options));

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

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

GitRevertOptions::~GitRevertOptions()
{
  // 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 GitRevertOptions::ConstructFields()
{
  Local<Object> merge_optsTemp = GitMergeOptions::New(
                                   &this->raw->merge_opts,
                                   false
                                 )->ToObject();
  NanAssignPersistent(this->merge_opts, merge_optsTemp);

  Local<Object> checkout_optsTemp = GitCheckoutOptions::New(
                                      &this->raw->checkout_opts,
                                      false
                                    )->ToObject();
  NanAssignPersistent(this->checkout_opts, checkout_optsTemp);

}

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

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

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

  tpl->InstanceTemplate()->SetAccessor(NanNew<String>("version"), GetVersion, SetVersion);
  tpl->InstanceTemplate()->SetAccessor(NanNew<String>("mainline"), GetMainline, SetMainline);
  tpl->InstanceTemplate()->SetAccessor(NanNew<String>("mergeOpts"), GetMergeOpts, SetMergeOpts);
  tpl->InstanceTemplate()->SetAccessor(NanNew<String>("checkoutOpts"), GetCheckoutOpts, SetCheckoutOpts);

  Local<Function> _constructor_template = tpl->GetFunction();
  NanAssignPersistent(constructor_template, _constructor_template);
  target->Set(NanNew<String>("RevertOptions"), _constructor_template);
}

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

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

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

  NanReturnValue(args.This());
}

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

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

git_revert_options *GitRevertOptions::GetValue()
{
  return this->raw;
}

git_revert_options **GitRevertOptions::GetRefValue()
{
  return &this->raw;
}

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

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

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

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

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

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

NAN_GETTER(GitRevertOptions::GetMainline)
{
  NanScope();

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

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

NAN_SETTER(GitRevertOptions::SetMainline)
{
  NanScope();

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

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

NAN_GETTER(GitRevertOptions::GetMergeOpts)
{
  NanScope();

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

  NanReturnValue(wrapper->merge_opts);

}

NAN_SETTER(GitRevertOptions::SetMergeOpts)
{
  NanScope();

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

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

}

NAN_GETTER(GitRevertOptions::GetCheckoutOpts)
{
  NanScope();

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

  NanReturnValue(wrapper->checkout_opts);

}

NAN_SETTER(GitRevertOptions::SetCheckoutOpts)
{
  NanScope();

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

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

}


Persistent<Function> GitRevertOptions::constructor_template;
