// 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/rebase_options.h"
#include "nodegit_wrapper.cc"

  #include "../include/checkout_options.h"
  #include "../include/merge_options.h"
  #include "../include/buf.h"
 
using namespace v8;
using namespace node;
using namespace std;

 
ConfigurableGitRebaseOptions::ConfigurableGitRebaseOptions(nodegit::Context *nodegitContext)
  : nodegit::ConfigurableClassWrapper<GitRebaseOptionsTraits>(nodegitContext)
{
     git_rebase_options  wrappedValue = GIT_REBASE_OPTIONS_INIT;
    this->raw = (git_rebase_options*) malloc(sizeof(git_rebase_options ));
    memcpy(this->raw, &wrappedValue, sizeof(git_rebase_options ));
 }

ConfigurableGitRebaseOptions::~ConfigurableGitRebaseOptions() {
            delete this->raw->rewrite_notes_ref;
             }

nodegit::ConfigurableClassWrapper<GitRebaseOptionsTraits>::v8ConversionResult ConfigurableGitRebaseOptions::fromJavascript(nodegit::Context *nodegitContext, v8::Local<v8::Value> input) {
  if (!input->IsObject()) {
    return {
      "Must pass object for ConfigurableGitRebaseOptions"
    };
  }

  Nan::HandleScope scope;
  v8::Local<v8::Object> inputObj = input.As<v8::Object>();
  std::shared_ptr<ConfigurableGitRebaseOptions> output(new ConfigurableGitRebaseOptions(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<unsigned int>(maybeNumber->Int32Value(Nan::GetCurrentContext()).FromJust());
          }
        }
           {
          v8::Local<v8::Value> maybeNumber = nodegit::safeGetField(inputObj, "quiet");
          if (!maybeNumber.IsEmpty() && !maybeNumber->IsUndefined() && !maybeNumber->IsNull()) {
            if (!maybeNumber->IsNumber()) {
              return {
                "Must pass Int32 to quiet"
              };
            }

            output->raw->quiet = static_cast<int>(maybeNumber->Int32Value(Nan::GetCurrentContext()).FromJust());
          }
        }
          output->raw->rewrite_notes_ref = nullptr;
        {
          v8::Local<v8::Value> maybeString = nodegit::safeGetField(inputObj, "rewriteNotesRef");
          if (!maybeString.IsEmpty() && !maybeString->IsUndefined() && !maybeString->IsNull()) {
            if (!maybeString->IsString()) {
              return {
                "Must pass string to rewriteNotesRef"
              };
            }

            Nan::Utf8String utf8String(maybeString.As<v8::String>());
            output->raw->rewrite_notes_ref = strdup(*utf8String);
          }
        }
          {
          v8::Local<v8::Value> maybeNestedObject = nodegit::safeGetField(inputObj, "checkoutOptions");
          if (!maybeNestedObject.IsEmpty() && !maybeNestedObject->IsUndefined() && !maybeNestedObject->IsNull()) {
            auto conversionResult = ConfigurableGitCheckoutOptions::fromJavascript(nodegitContext, maybeNestedObject);
            if (!conversionResult.result) {
              std::string error = "Failed to set checkoutOptions: ";
              error += conversionResult.error;
              return {
                error
              };
            }

            auto child = conversionResult.result;
            output->childCleanupVector.push_back(child);
            output->raw->checkout_options = *child->GetValue();
          }
        }
          {
          v8::Local<v8::Value> maybeNestedObject = nodegit::safeGetField(inputObj, "mergeOptions");
          if (!maybeNestedObject.IsEmpty() && !maybeNestedObject->IsUndefined() && !maybeNestedObject->IsNull()) {
            auto conversionResult = ConfigurableGitMergeOptions::fromJavascript(nodegitContext, maybeNestedObject);
            if (!conversionResult.result) {
              std::string error = "Failed to set mergeOptions: ";
              error += conversionResult.error;
              return {
                error
              };
            }

            auto child = conversionResult.result;
            output->childCleanupVector.push_back(child);
            output->raw->merge_options = *child->GetValue();
          }
        }
          {
          v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(inputObj, "signingCb");
          if (!maybeCallback.IsEmpty() && !maybeCallback->IsUndefined() && !maybeCallback->IsNull()) {
            if (!maybeCallback->IsFunction() && !maybeCallback->IsObject()) {
              return {
                "Must pass Function or CallbackSpecifier to signingCb"
              };
            }

            std::unique_ptr<Nan::Callback> callback;
            uint32_t throttle =  0 ;
            bool waitForResult = true;

            if (maybeCallback->IsFunction()) {
              callback.reset(new Nan::Callback(maybeCallback.As<v8::Function>()));
            } else {
              v8::Local<v8::Object> callbackSpecifier = maybeCallback.As<v8::Object>();
              v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(callbackSpecifier, "callback");
              if (maybeCallback.IsEmpty() || !maybeCallback->IsFunction()) {
                return {
                  "Must pass callback to CallbackSpecifier"
                };
              }

              callback.reset(new Nan::Callback(maybeCallback.As<v8::Function>()));

              v8::Local<v8::Value> maybeThrottle = nodegit::safeGetField(callbackSpecifier, "throttle");
              if (!maybeThrottle.IsEmpty() && !maybeThrottle->IsUndefined() && !maybeThrottle->IsNull()) {
                if (!maybeThrottle->IsNumber()) {
                  return {
                    "Must pass zero or positive number as throttle to CallbackSpecifier"
                  };
                }

                throttle = maybeThrottle->Uint32Value(Nan::GetCurrentContext()).FromJust();
              }

              v8::Local<v8::Value> maybeWaitForResult = nodegit::safeGetField(callbackSpecifier, "waitForResult");
              if (!maybeWaitForResult.IsEmpty() && !maybeWaitForResult->IsUndefined() && !maybeWaitForResult->IsNull()) {
                if (!maybeWaitForResult->IsBoolean()) {
                  return {
                    "Must pass a boolean as waitForResult to callbackSpecifier"
                  };
                }

                waitForResult = Nan::To<bool>(maybeWaitForResult).FromJust();
              }
            }

            output->signingCb.SetCallback(std::move(callback), throttle, waitForResult);
            output->raw->signing_cb = (git_commit_signing_cb)signingCb_cppCallback;
          }
        }
          output->raw->payload = (void *)output.get();
           {
          v8::Local<v8::Value> maybeNumber = nodegit::safeGetField(inputObj, "inmemory");
          if (!maybeNumber.IsEmpty() && !maybeNumber->IsUndefined() && !maybeNumber->IsNull()) {
            if (!maybeNumber->IsNumber()) {
              return {
                "Must pass Int32 to inmemory"
              };
            }

            output->raw->inmemory = static_cast<int>(maybeNumber->Int32Value(Nan::GetCurrentContext()).FromJust());
          }
        }
    
  return {
    output
  };
}

                ConfigurableGitRebaseOptions* ConfigurableGitRebaseOptions::signingCb_getInstanceFromBaton(SigningCbBaton* baton) {
           return static_cast<ConfigurableGitRebaseOptions*>(baton->
                 payload
  );
       }

      int ConfigurableGitRebaseOptions::signingCb_cppCallback (
          git_buf * signature,           git_buf * signature_field,           const char * commit_content,           void * payload        ) {
        SigningCbBaton *baton =
          new SigningCbBaton(-30);

          baton->signature = signature;
          baton->signature_field = signature_field;
          baton->commit_content = commit_content;
          baton->payload = payload;
 
        ConfigurableGitRebaseOptions* instance = signingCb_getInstanceFromBaton(baton);

           int result;

          if (instance->nodegitContext != nodegit::ThreadPool::GetCurrentContext()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->signingCb.WillBeThrottled()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->signingCb.ShouldWaitForResult()) {
            result = baton->ExecuteAsync(signingCb_async, signingCb_cancelAsync);
            delete baton;
          } else {
            result = baton->defaultResult;
            baton->ExecuteAsync(signingCb_async, signingCb_cancelAsync, nodegit::deleteBaton);
          }
          return result;
       }

      void ConfigurableGitRebaseOptions::signingCb_cancelAsync(void *untypedBaton) {
        SigningCbBaton* baton = static_cast<SigningCbBaton*>(untypedBaton);
          baton->result = -1;
         baton->Done();
      }

      void ConfigurableGitRebaseOptions::signingCb_async(void *untypedBaton) {
        Nan::HandleScope scope;

        SigningCbBaton* baton = static_cast<SigningCbBaton*>(untypedBaton);
        ConfigurableGitRebaseOptions* instance = signingCb_getInstanceFromBaton(baton);

        if (instance->signingCb.GetCallback()->IsEmpty()) {
            baton->result = baton->defaultResult; // no results acquired
           baton->Done();
          return;
        }

           v8::Local<Value> argv[3] = {
               GitBuf::New(baton->signature, false)
 ,               GitBuf::New(baton->signature_field, false)
 ,               baton->commit_content == NULL
                ? Nan::EmptyString()
                : Nan::New( baton->commit_content).ToLocalChecked()
            };
 
        Nan::TryCatch tryCatch;

        Nan::MaybeLocal<v8::Value> maybeResult = (*(instance->signingCb.GetCallback()))(
          baton->GetAsyncResource(),
          3,
          argv
        );
        v8::Local<v8::Value> result;
        if (!maybeResult.IsEmpty()) {
          result = maybeResult.ToLocalChecked();
        }

        if (PromiseCompletion::ForwardIfPromise(result, baton, ConfigurableGitRebaseOptions::signingCb_promiseCompleted)) {
          return;
        }

             if (result.IsEmpty() || result->IsNativeError()) {
              baton->result = -1;
            }
            else if (!result->IsNull() && !result->IsUndefined()) {
               if (result->IsNumber()) {
                baton->result = Nan::To<int>(result).FromJust();
              }
              else {
                baton->result = baton->defaultResult;
              }
             }
            else {
              baton->result = baton->defaultResult;
            }
           baton->Done();
       }

      void ConfigurableGitRebaseOptions::signingCb_promiseCompleted(bool isFulfilled, nodegit::AsyncBaton *_baton, v8::Local<v8::Value> result) {
        Nan::HandleScope scope;

        SigningCbBaton* baton = static_cast<SigningCbBaton*>(_baton);
           if (isFulfilled) {
              if (result.IsEmpty() || result->IsNativeError()) {
                baton->result = -1;
              }
              else if (!result->IsNull() && !result->IsUndefined()) {
                 if (result->IsNumber()) {
                  baton->result = Nan::To<int>(result).FromJust();
                }
                else{
                  baton->result = baton->defaultResult;
                }
               }
              else {
                baton->result = baton->defaultResult;
              }
           }
          else {
            // promise was rejected
               ConfigurableGitRebaseOptions* instance = static_cast<ConfigurableGitRebaseOptions*>(baton->   payload  );
             baton->SetCallbackError(result);
            baton->result = -1;
          }
          baton->Done();
       }
       
// force base class template instantiation, to make sure we get all the
// methods, statics, etc.
template class nodegit::ConfigurableClassWrapper<GitRebaseOptionsTraits>;
