// This is a generated file, modify: generate/templates/templates/struct_content.cc

// generated from struct_content.cc
#include <nan.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif // win32

extern "C" {
  #include <git2.h>
 }

#include <iostream>
#include "../include/nodegit.h"
#include "../include/lock_master.h"
#include "../include/functions/copy.h"
#include "../include/fetch_options.h"
#include "nodegit_wrapper.cc"

  #include "../include/str_array_converter.h"
  #include "../include/remote_callbacks.h"
  #include "../include/proxy_options.h"
  #include "../include/strarray.h"
 
using namespace v8;
using namespace node;
using namespace std;

 
ConfigurableGitFetchOptions::ConfigurableGitFetchOptions(nodegit::Context *nodegitContext)
  : nodegit::ConfigurableClassWrapper<GitFetchOptionsTraits>(nodegitContext)
{
     git_fetch_options  wrappedValue = GIT_FETCH_OPTIONS_INIT;
    this->raw = (git_fetch_options*) malloc(sizeof(git_fetch_options ));
    memcpy(this->raw, &wrappedValue, sizeof(git_fetch_options ));
 }

ConfigurableGitFetchOptions::~ConfigurableGitFetchOptions() {
                    if (this->raw->custom_headers.count) {
          for (size_t i = 0; i < this->raw->custom_headers.count; ++i) {
            delete this->raw->custom_headers.strings[i];
          }
          delete[] this->raw->custom_headers.strings;
        }
   }

nodegit::ConfigurableClassWrapper<GitFetchOptionsTraits>::v8ConversionResult ConfigurableGitFetchOptions::fromJavascript(nodegit::Context *nodegitContext, v8::Local<v8::Value> input) {
  if (!input->IsObject()) {
    return {
      "Must pass object for ConfigurableGitFetchOptions"
    };
  }

  Nan::HandleScope scope;
  v8::Local<v8::Object> inputObj = input.As<v8::Object>();
  std::shared_ptr<ConfigurableGitFetchOptions> output(new ConfigurableGitFetchOptions(nodegitContext));

  // unpack the data into the correct fields
         {
          v8::Local<v8::Value> maybeNumber = nodegit::safeGetField(inputObj, "version");
          if (!maybeNumber.IsEmpty() && !maybeNumber->IsUndefined() && !maybeNumber->IsNull()) {
            if (!maybeNumber->IsNumber()) {
              return {
                "Must pass Int32 to version"
              };
            }

            output->raw->version = static_cast<int>(maybeNumber->Int32Value(Nan::GetCurrentContext()).FromJust());
          }
        }
          {
          v8::Local<v8::Value> maybeNestedObject = nodegit::safeGetField(inputObj, "callbacks");
          if (!maybeNestedObject.IsEmpty() && !maybeNestedObject->IsUndefined() && !maybeNestedObject->IsNull()) {
            auto conversionResult = ConfigurableGitRemoteCallbacks::fromJavascript(nodegitContext, maybeNestedObject);
            if (!conversionResult.result) {
              std::string error = "Failed to set callbacks: ";
              error += conversionResult.error;
              return {
                error
              };
            }

            auto child = conversionResult.result;
            output->childCleanupVector.push_back(child);
            output->raw->callbacks = *child->GetValue();
          }
        }
           {
          v8::Local<v8::Value> maybeNumber = nodegit::safeGetField(inputObj, "prune");
          if (!maybeNumber.IsEmpty() && !maybeNumber->IsUndefined() && !maybeNumber->IsNull()) {
            if (!maybeNumber->IsNumber()) {
              return {
                "Must pass Int32 to prune"
              };
            }

            output->raw->prune = static_cast<git_fetch_prune_t>(maybeNumber->Int32Value(Nan::GetCurrentContext()).FromJust());
          }
        }
          {
          v8::Local<v8::Value> maybeNestedObject = nodegit::safeGetField(inputObj, "proxyOpts");
          if (!maybeNestedObject.IsEmpty() && !maybeNestedObject->IsUndefined() && !maybeNestedObject->IsNull()) {
            auto conversionResult = ConfigurableGitProxyOptions::fromJavascript(nodegitContext, maybeNestedObject);
            if (!conversionResult.result) {
              std::string error = "Failed to set proxyOpts: ";
              error += conversionResult.error;
              return {
                error
              };
            }

            auto child = conversionResult.result;
            output->childCleanupVector.push_back(child);
            output->raw->proxy_opts = *child->GetValue();
          }
        }
           {
          v8::Local<v8::Value> maybeNumber = nodegit::safeGetField(inputObj, "updateFetchhead");
          if (!maybeNumber.IsEmpty() && !maybeNumber->IsUndefined() && !maybeNumber->IsNull()) {
            if (!maybeNumber->IsNumber()) {
              return {
                "Must pass Int32 to updateFetchhead"
              };
            }

            output->raw->update_fetchhead = static_cast<int>(maybeNumber->Int32Value(Nan::GetCurrentContext()).FromJust());
          }
        }
           {
          v8::Local<v8::Value> maybeNumber = nodegit::safeGetField(inputObj, "downloadTags");
          if (!maybeNumber.IsEmpty() && !maybeNumber->IsUndefined() && !maybeNumber->IsNull()) {
            if (!maybeNumber->IsNumber()) {
              return {
                "Must pass Int32 to downloadTags"
              };
            }

            output->raw->download_tags = static_cast<git_remote_autotag_option_t>(maybeNumber->Int32Value(Nan::GetCurrentContext()).FromJust());
          }
        }
            output->raw->custom_headers.count = 0;
          output->raw->custom_headers.strings = nullptr;

          {
            v8::Local<v8::Value> maybeStrarray = nodegit::safeGetField(inputObj, "customHeaders");
            if (!maybeStrarray.IsEmpty() && !maybeStrarray->IsUndefined() && !maybeStrarray->IsNull()) {
              if (maybeStrarray->IsArray()) {
                v8::Local<v8::Array> strarrayValue = maybeStrarray.As<v8::Array>();
                // validate the StrArray is indeed a list of strings
                for (uint32_t i = 0; i < strarrayValue->Length(); ++i) {
                  // TODO confirm that sparse array at least boils down to undefined
                  v8::Local<v8::Value> arrayValue = Nan::Get(strarrayValue, i).ToLocalChecked();
                  if (!arrayValue->IsString()) {
                    return {
                      "Must pass String or Array of strings to customHeaders"
                    };
                  }
                }

                StrArrayConverter::ConvertInto(&output->raw->custom_headers, strarrayValue);
              } else if (maybeStrarray->IsString()) {
                v8::Local<v8::String> strarrayValue = maybeStrarray.As<v8::String>();
                StrArrayConverter::ConvertInto(&output->raw->custom_headers, strarrayValue);
              } else {
                return {
                  "Must pass String or Array of strings to customHeaders"
                };
              }
            }
          }
     
  return {
    output
  };
}

               
// force base class template instantiation, to make sure we get all the
// methods, statics, etc.
template class nodegit::ConfigurableClassWrapper<GitFetchOptionsTraits>;
