// 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/stash_apply_options.h"
#include "nodegit_wrapper.cc"

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

 
ConfigurableGitStashApplyOptions::ConfigurableGitStashApplyOptions(nodegit::Context *nodegitContext)
  : nodegit::ConfigurableClassWrapper<GitStashApplyOptionsTraits>(nodegitContext)
{
     git_stash_apply_options  wrappedValue = GIT_STASH_APPLY_OPTIONS_INIT;
    this->raw = (git_stash_apply_options*) malloc(sizeof(git_stash_apply_options ));
    memcpy(this->raw, &wrappedValue, sizeof(git_stash_apply_options ));
 }

ConfigurableGitStashApplyOptions::~ConfigurableGitStashApplyOptions() {
           }

nodegit::ConfigurableClassWrapper<GitStashApplyOptionsTraits>::v8ConversionResult ConfigurableGitStashApplyOptions::fromJavascript(nodegit::Context *nodegitContext, v8::Local<v8::Value> input) {
  if (!input->IsObject()) {
    return {
      "Must pass object for ConfigurableGitStashApplyOptions"
    };
  }

  Nan::HandleScope scope;
  v8::Local<v8::Object> inputObj = input.As<v8::Object>();
  std::shared_ptr<ConfigurableGitStashApplyOptions> output(new ConfigurableGitStashApplyOptions(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, "flags");
          if (!maybeNumber.IsEmpty() && !maybeNumber->IsUndefined() && !maybeNumber->IsNull()) {
            if (!maybeNumber->IsNumber()) {
              return {
                "Must pass Int32 to flags"
              };
            }

            output->raw->flags = static_cast<uint32_t>(maybeNumber->Int32Value(Nan::GetCurrentContext()).FromJust());
          }
        }
          {
          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> maybeCallback = nodegit::safeGetField(inputObj, "progressCb");
          if (!maybeCallback.IsEmpty() && !maybeCallback->IsUndefined() && !maybeCallback->IsNull()) {
            if (!maybeCallback->IsFunction() && !maybeCallback->IsObject()) {
              return {
                "Must pass Function or CallbackSpecifier to progressCb"
              };
            }

            std::unique_ptr<Nan::Callback> callback;
            uint32_t throttle = 100 ;
            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->progressCb.SetCallback(std::move(callback), throttle, waitForResult);
            output->raw->progress_cb = (git_stash_apply_progress_cb)progressCb_cppCallback;
          }
        }
          output->raw->progress_payload = (void *)output.get();
    
  return {
    output
  };
}

            ConfigurableGitStashApplyOptions* ConfigurableGitStashApplyOptions::progressCb_getInstanceFromBaton(ProgressCbBaton* baton) {
           return static_cast<ConfigurableGitStashApplyOptions*>(baton->
               payload
  );
       }

      int ConfigurableGitStashApplyOptions::progressCb_cppCallback (
          git_stash_apply_progress_t progress,           void * payload        ) {
        ProgressCbBaton *baton =
          new ProgressCbBaton(0);

          baton->progress = progress;
          baton->payload = payload;
 
        ConfigurableGitStashApplyOptions* instance = progressCb_getInstanceFromBaton(baton);

           int result;

          if (instance->nodegitContext != nodegit::ThreadPool::GetCurrentContext()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->progressCb.WillBeThrottled()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->progressCb.ShouldWaitForResult()) {
            result = baton->ExecuteAsync(progressCb_async, progressCb_cancelAsync);
            delete baton;
          } else {
            result = baton->defaultResult;
            baton->ExecuteAsync(progressCb_async, progressCb_cancelAsync, nodegit::deleteBaton);
          }
          return result;
       }

      void ConfigurableGitStashApplyOptions::progressCb_cancelAsync(void *untypedBaton) {
        ProgressCbBaton* baton = static_cast<ProgressCbBaton*>(untypedBaton);
          baton->result = -1;
         baton->Done();
      }

      void ConfigurableGitStashApplyOptions::progressCb_async(void *untypedBaton) {
        Nan::HandleScope scope;

        ProgressCbBaton* baton = static_cast<ProgressCbBaton*>(untypedBaton);
        ConfigurableGitStashApplyOptions* instance = progressCb_getInstanceFromBaton(baton);

        if (instance->progressCb.GetCallback()->IsEmpty()) {
            baton->result = baton->defaultResult; // no results acquired
           baton->Done();
          return;
        }

           v8::Local<Value> argv[1] = {
               Nan::New((int)baton->progress)
            };
 
        Nan::TryCatch tryCatch;

        Nan::MaybeLocal<v8::Value> maybeResult = (*(instance->progressCb.GetCallback()))(
          baton->GetAsyncResource(),
          1,
          argv
        );
        v8::Local<v8::Value> result;
        if (!maybeResult.IsEmpty()) {
          result = maybeResult.ToLocalChecked();
        }

        if (PromiseCompletion::ForwardIfPromise(result, baton, ConfigurableGitStashApplyOptions::progressCb_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 ConfigurableGitStashApplyOptions::progressCb_promiseCompleted(bool isFulfilled, nodegit::AsyncBaton *_baton, v8::Local<v8::Value> result) {
        Nan::HandleScope scope;

        ProgressCbBaton* baton = static_cast<ProgressCbBaton*>(_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
               ConfigurableGitStashApplyOptions* instance = static_cast<ConfigurableGitStashApplyOptions*>(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<GitStashApplyOptionsTraits>;
