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

#include "../include/checkout_options.h"
#include "../include/remote_callbacks.h"
#include "../include/signature.h"

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


// generated from struct_content.cc
GitCloneOptions::GitCloneOptions()
{
  git_clone_options wrappedValue = GIT_CLONE_OPTIONS_INIT;
  this->raw = (git_clone_options*) malloc(sizeof(git_clone_options));
  memcpy(this->raw, &wrappedValue, sizeof(git_clone_options));

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

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

GitCloneOptions::~GitCloneOptions()
{
  // 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 GitCloneOptions::ConstructFields()
{
  Local<Object> checkout_optsTemp = GitCheckoutOptions::New(
                                      &this->raw->checkout_opts,
                                      false
                                    )->ToObject();
  NanAssignPersistent(this->checkout_opts, checkout_optsTemp);

  Local<Object> remote_callbacksTemp = GitRemoteCallbacks::New(
                                         &this->raw->remote_callbacks,
                                         false
                                       )->ToObject();
  NanAssignPersistent(this->remote_callbacks, remote_callbacksTemp);

  Local<Object> signatureTemp = GitSignature::New(
                                  &this->raw->signature,
                                  false
                                )->ToObject();
  NanAssignPersistent(this->signature, signatureTemp);

}

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

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

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

  tpl->InstanceTemplate()->SetAccessor(NanNew<String>("version"), GetVersion, SetVersion);
  tpl->InstanceTemplate()->SetAccessor(NanNew<String>("checkoutOpts"), GetCheckoutOpts, SetCheckoutOpts);
  tpl->InstanceTemplate()->SetAccessor(NanNew<String>("remoteCallbacks"), GetRemoteCallbacks, SetRemoteCallbacks);
  tpl->InstanceTemplate()->SetAccessor(NanNew<String>("bare"), GetBare, SetBare);
  tpl->InstanceTemplate()->SetAccessor(NanNew<String>("ignoreCertErrors"), GetIgnoreCertErrors, SetIgnoreCertErrors);
  tpl->InstanceTemplate()->SetAccessor(NanNew<String>("local"), GetLocal, SetLocal);
  tpl->InstanceTemplate()->SetAccessor(NanNew<String>("remoteName"), GetRemoteName, SetRemoteName);
  tpl->InstanceTemplate()->SetAccessor(NanNew<String>("checkoutBranch"), GetCheckoutBranch, SetCheckoutBranch);
  tpl->InstanceTemplate()->SetAccessor(NanNew<String>("signature"), GetSignature, SetSignature);

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

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

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

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

  NanReturnValue(args.This());
}

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

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

git_clone_options *GitCloneOptions::GetValue()
{
  return this->raw;
}

git_clone_options **GitCloneOptions::GetRefValue()
{
  return &this->raw;
}

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

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

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

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

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

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

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

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

  NanReturnValue(wrapper->checkout_opts);

}

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

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

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

}

NAN_GETTER(GitCloneOptions::GetRemoteCallbacks)
{
  NanScope();

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

  NanReturnValue(wrapper->remote_callbacks);

}

NAN_SETTER(GitCloneOptions::SetRemoteCallbacks)
{
  NanScope();

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

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

}

NAN_GETTER(GitCloneOptions::GetBare)
{
  NanScope();

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

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

NAN_SETTER(GitCloneOptions::SetBare)
{
  NanScope();

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

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

NAN_GETTER(GitCloneOptions::GetIgnoreCertErrors)
{
  NanScope();

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

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

NAN_SETTER(GitCloneOptions::SetIgnoreCertErrors)
{
  NanScope();

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

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

NAN_GETTER(GitCloneOptions::GetLocal)
{
  NanScope();

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

  NanReturnValue(NanNew((int)wrapper->GetValue()->local));

}

NAN_SETTER(GitCloneOptions::SetLocal)
{
  NanScope();

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

  if (value->IsNumber()) {
    wrapper->GetValue()->local = (git_clone_local_t) value->Int32Value();
  }

}

NAN_GETTER(GitCloneOptions::GetRemoteName)
{
  NanScope();

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

  if (wrapper->GetValue()->remote_name) {
    NanReturnValue(NanNew<String>(wrapper->GetValue()->remote_name));
  } else {
    NanReturnUndefined();
  }

}

NAN_SETTER(GitCloneOptions::SetRemoteName)
{
  NanScope();

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

  if (wrapper->GetValue()->remote_name) {
  }

  String::Utf8Value str(value);
  wrapper->GetValue()->remote_name = strdup(*str);

}

NAN_GETTER(GitCloneOptions::GetCheckoutBranch)
{
  NanScope();

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

  if (wrapper->GetValue()->checkout_branch) {
    NanReturnValue(NanNew<String>(wrapper->GetValue()->checkout_branch));
  } else {
    NanReturnUndefined();
  }

}

NAN_SETTER(GitCloneOptions::SetCheckoutBranch)
{
  NanScope();

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

  if (wrapper->GetValue()->checkout_branch) {
  }

  String::Utf8Value str(value);
  wrapper->GetValue()->checkout_branch = strdup(*str);

}

NAN_GETTER(GitCloneOptions::GetSignature)
{
  NanScope();

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

  NanReturnValue(wrapper->signature);

}

NAN_SETTER(GitCloneOptions::SetSignature)
{
  NanScope();

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

  NanDisposePersistent(wrapper->signature);
  wrapper->raw->signature =  ObjectWrap::Unwrap<GitSignature>(value->ToObject())->GetValue();

}


Persistent<Function> GitCloneOptions::constructor_template;
