// 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/proxy_options.h"
#include "nodegit_wrapper.cc"

  #include "../include/credential.h"
  #include "../include/cert.h"
 
using namespace v8;
using namespace node;
using namespace std;

 
ConfigurableGitProxyOptions::ConfigurableGitProxyOptions(nodegit::Context *nodegitContext)
  : nodegit::ConfigurableClassWrapper<GitProxyOptionsTraits>(nodegitContext)
{
     git_proxy_options  wrappedValue = GIT_PROXY_OPTIONS_INIT;
    this->raw = (git_proxy_options*) malloc(sizeof(git_proxy_options ));
    memcpy(this->raw, &wrappedValue, sizeof(git_proxy_options ));
 }

ConfigurableGitProxyOptions::~ConfigurableGitProxyOptions() {
            delete this->raw->url;
         }

nodegit::ConfigurableClassWrapper<GitProxyOptionsTraits>::v8ConversionResult ConfigurableGitProxyOptions::fromJavascript(nodegit::Context *nodegitContext, v8::Local<v8::Value> input) {
  if (!input->IsObject()) {
    return {
      "Must pass object for ConfigurableGitProxyOptions"
    };
  }

  Nan::HandleScope scope;
  v8::Local<v8::Object> inputObj = input.As<v8::Object>();
  std::shared_ptr<ConfigurableGitProxyOptions> output(new ConfigurableGitProxyOptions(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, "type");
          if (!maybeNumber.IsEmpty() && !maybeNumber->IsUndefined() && !maybeNumber->IsNull()) {
            if (!maybeNumber->IsNumber()) {
              return {
                "Must pass Int32 to type"
              };
            }

            output->raw->type = static_cast<git_proxy_t>(maybeNumber->Int32Value(Nan::GetCurrentContext()).FromJust());
          }
        }
          output->raw->url = nullptr;
        {
          v8::Local<v8::Value> maybeString = nodegit::safeGetField(inputObj, "url");
          if (!maybeString.IsEmpty() && !maybeString->IsUndefined() && !maybeString->IsNull()) {
            if (!maybeString->IsString()) {
              return {
                "Must pass string to url"
              };
            }

            Nan::Utf8String utf8String(maybeString.As<v8::String>());
            output->raw->url = strdup(*utf8String);
          }
        }
          {
          v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(inputObj, "credentials");
          if (!maybeCallback.IsEmpty() && !maybeCallback->IsUndefined() && !maybeCallback->IsNull()) {
            if (!maybeCallback->IsFunction() && !maybeCallback->IsObject()) {
              return {
                "Must pass Function or CallbackSpecifier to credentials"
              };
            }

            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->credentials.SetCallback(std::move(callback), throttle, waitForResult);
            output->raw->credentials = (git_credential_acquire_cb)credentials_cppCallback;
          }
        }
          {
          v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(inputObj, "certificateCheck");
          if (!maybeCallback.IsEmpty() && !maybeCallback->IsUndefined() && !maybeCallback->IsNull()) {
            if (!maybeCallback->IsFunction() && !maybeCallback->IsObject()) {
              return {
                "Must pass Function or CallbackSpecifier to certificateCheck"
              };
            }

            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->certificateCheck.SetCallback(std::move(callback), throttle, waitForResult);
            output->raw->certificate_check = (git_transport_certificate_check_cb)certificateCheck_cppCallback;
          }
        }
          output->raw->payload = (void *)output.get();
    
  return {
    output
  };
}

            ConfigurableGitProxyOptions* ConfigurableGitProxyOptions::credentials_getInstanceFromBaton(CredentialsBaton* baton) {
           return static_cast<ConfigurableGitProxyOptions*>(baton->
                  payload
  );
       }

      int ConfigurableGitProxyOptions::credentials_cppCallback (
          git_credential ** credential,           const char * url,           const char * username_from_url,           unsigned int allowed_types,           void * payload        ) {
        CredentialsBaton *baton =
          new CredentialsBaton(1);

          baton->credential = credential;
          baton->url = url;
          baton->username_from_url = username_from_url;
          baton->allowed_types = allowed_types;
          baton->payload = payload;
 
        ConfigurableGitProxyOptions* instance = credentials_getInstanceFromBaton(baton);

           int result;

          if (instance->nodegitContext != nodegit::ThreadPool::GetCurrentContext()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->credentials.WillBeThrottled()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->credentials.ShouldWaitForResult()) {
            result = baton->ExecuteAsync(credentials_async, credentials_cancelAsync);
            delete baton;
          } else {
            result = baton->defaultResult;
            baton->ExecuteAsync(credentials_async, credentials_cancelAsync, nodegit::deleteBaton);
          }
          return result;
       }

      void ConfigurableGitProxyOptions::credentials_cancelAsync(void *untypedBaton) {
        CredentialsBaton* baton = static_cast<CredentialsBaton*>(untypedBaton);
          baton->result = -1;
         baton->Done();
      }

      void ConfigurableGitProxyOptions::credentials_async(void *untypedBaton) {
        Nan::HandleScope scope;

        CredentialsBaton* baton = static_cast<CredentialsBaton*>(untypedBaton);
        ConfigurableGitProxyOptions* instance = credentials_getInstanceFromBaton(baton);

        if (instance->credentials.GetCallback()->IsEmpty()) {
            baton->result = baton->defaultResult; // no results acquired
           baton->Done();
          return;
        }

           v8::Local<Value> argv[3] = {
               baton->url == NULL
                ? Nan::EmptyString()
                : Nan::New( baton->url).ToLocalChecked()
 ,               baton->username_from_url == NULL
                ? Nan::EmptyString()
                : Nan::New( baton->username_from_url).ToLocalChecked()
 ,                Nan::New(baton->allowed_types)
            };
 
        Nan::TryCatch tryCatch;

        Nan::MaybeLocal<v8::Value> maybeResult = (*(instance->credentials.GetCallback()))(
          baton->GetAsyncResource(),
          3,
          argv
        );
        v8::Local<v8::Value> result;
        if (!maybeResult.IsEmpty()) {
          result = maybeResult.ToLocalChecked();
        }

        if (PromiseCompletion::ForwardIfPromise(result, baton, ConfigurableGitProxyOptions::credentials_promiseCompleted)) {
          return;
        }

             if (result.IsEmpty() || result->IsNativeError()) {
              baton->result = -1;
            }
            else if (!result->IsNull() && !result->IsUndefined()) {
              GitCredential* wrapper = Nan::ObjectWrap::Unwrap<GitCredential>(Nan::To<v8::Object>(result).ToLocalChecked());
              wrapper->selfFreeing = false;

              *baton->credential = wrapper->GetValue();
              baton->result = 0;
             }
            else {
              baton->result = baton->defaultResult;
            }
           baton->Done();
       }

      void ConfigurableGitProxyOptions::credentials_promiseCompleted(bool isFulfilled, nodegit::AsyncBaton *_baton, v8::Local<v8::Value> result) {
        Nan::HandleScope scope;

        CredentialsBaton* baton = static_cast<CredentialsBaton*>(_baton);
           if (isFulfilled) {
              if (result.IsEmpty() || result->IsNativeError()) {
                baton->result = -1;
              }
              else if (!result->IsNull() && !result->IsUndefined()) {
                GitCredential* wrapper = Nan::ObjectWrap::Unwrap<GitCredential>(Nan::To<v8::Object>(result).ToLocalChecked());
                wrapper->selfFreeing = false;

                *baton->credential = wrapper->GetValue();
                baton->result = 0;
               }
              else {
                baton->result = baton->defaultResult;
              }
           }
          else {
            // promise was rejected
               ConfigurableGitProxyOptions* instance = static_cast<ConfigurableGitProxyOptions*>(baton->    payload  );
             baton->SetCallbackError(result);
            baton->result = -1;
          }
          baton->Done();
       }
        ConfigurableGitProxyOptions* ConfigurableGitProxyOptions::certificateCheck_getInstanceFromBaton(CertificateCheckBaton* baton) {
           return static_cast<ConfigurableGitProxyOptions*>(baton->
                 payload
  );
       }

      int ConfigurableGitProxyOptions::certificateCheck_cppCallback (
          git_cert * cert,           int valid,           const char * host,           void * payload        ) {
        CertificateCheckBaton *baton =
          new CertificateCheckBaton(1);

          baton->cert = cert;
          baton->valid = valid;
          baton->host = host;
          baton->payload = payload;
 
        ConfigurableGitProxyOptions* instance = certificateCheck_getInstanceFromBaton(baton);

           int result;

          if (instance->nodegitContext != nodegit::ThreadPool::GetCurrentContext()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->certificateCheck.WillBeThrottled()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->certificateCheck.ShouldWaitForResult()) {
            result = baton->ExecuteAsync(certificateCheck_async, certificateCheck_cancelAsync);
            delete baton;
          } else {
            result = baton->defaultResult;
            baton->ExecuteAsync(certificateCheck_async, certificateCheck_cancelAsync, nodegit::deleteBaton);
          }
          return result;
       }

      void ConfigurableGitProxyOptions::certificateCheck_cancelAsync(void *untypedBaton) {
        CertificateCheckBaton* baton = static_cast<CertificateCheckBaton*>(untypedBaton);
          baton->result = -1;
         baton->Done();
      }

      void ConfigurableGitProxyOptions::certificateCheck_async(void *untypedBaton) {
        Nan::HandleScope scope;

        CertificateCheckBaton* baton = static_cast<CertificateCheckBaton*>(untypedBaton);
        ConfigurableGitProxyOptions* instance = certificateCheck_getInstanceFromBaton(baton);

        if (instance->certificateCheck.GetCallback()->IsEmpty()) {
            baton->result = baton->defaultResult; // no results acquired
           baton->Done();
          return;
        }

           v8::Local<Value> argv[3] = {
               GitCert::New(baton->cert, false)
 ,                Nan::New(baton->valid)
 ,               baton->host == NULL
                ? Nan::EmptyString()
                : Nan::New( baton->host).ToLocalChecked()
            };
 
        Nan::TryCatch tryCatch;

        Nan::MaybeLocal<v8::Value> maybeResult = (*(instance->certificateCheck.GetCallback()))(
          baton->GetAsyncResource(),
          3,
          argv
        );
        v8::Local<v8::Value> result;
        if (!maybeResult.IsEmpty()) {
          result = maybeResult.ToLocalChecked();
        }

        if (PromiseCompletion::ForwardIfPromise(result, baton, ConfigurableGitProxyOptions::certificateCheck_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 ConfigurableGitProxyOptions::certificateCheck_promiseCompleted(bool isFulfilled, nodegit::AsyncBaton *_baton, v8::Local<v8::Value> result) {
        Nan::HandleScope scope;

        CertificateCheckBaton* baton = static_cast<CertificateCheckBaton*>(_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
               ConfigurableGitProxyOptions* instance = static_cast<ConfigurableGitProxyOptions*>(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<GitProxyOptionsTraits>;
