// This is a generated file, modify: generate/templates/templates/class_header.h

#ifndef GITREFDB_H
#define GITREFDB_H
#include <nan.h>
#include <string>
#include <utility>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <sstream>

#include "async_baton.h"
#include "async_worker.h"
#include "cleanup_handle.h"
#include "context.h"
#include "lock_master.h"
#include "nodegit_wrapper.h"
#include "promise_completion.h"
#include "reference_counter.h"
#include "worker_pool.h"

extern "C" {
#include <git2.h>
#include <git2/sys/errors.h>
}

#include "../include/typedefs.h"

#include "../include/repository.h"
// Forward declaration.
struct git_refdb {
};

using namespace node;
using namespace v8;

class GitRefdb;
 
struct GitRefdbTraits {
  typedef GitRefdb cppClass;
  typedef git_refdb cType;
 
  static const bool isDuplicable = false;
  static void duplicate(git_refdb **dest, git_refdb *src) {
     Nan::ThrowError("duplicate called on GitRefdb which cannot be duplicated");
   }

  static std::string className() { return "GitRefdb"; };
  static const bool isSingleton = false;
  static const bool isFreeable = true;
  static void free(git_refdb *raw) {
    unsigned long referenceCount = 0;
     if (referenceCount == 0) {
      ::git_refdb_free(raw); // :: to avoid calling this free recursively
    }
   }
};

class GitRefdb : public
  NodeGitWrapper<GitRefdbTraits>
{
    // grant full access to base class
    friend class NodeGitWrapper<GitRefdbTraits>;
   public:
    GitRefdb(const GitRefdb &) = delete;
    GitRefdb(GitRefdb &&) = delete;
    GitRefdb &operator=(const GitRefdb &) = delete;
    GitRefdb &operator=(GitRefdb &&) = delete;

    static void InitializeComponent (v8::Local<v8::Object> target, nodegit::Context *nodegitContext);

        

  private:
    GitRefdb()
      : NodeGitWrapper<GitRefdbTraits>(
           "A new GitRefdb cannot be instantiated."
       )
    {}
    GitRefdb(git_refdb *raw, bool selfFreeing, v8::Local<v8::Object> owner = v8::Local<v8::Object>())
      : NodeGitWrapper<GitRefdbTraits>(raw, selfFreeing, owner)
    {}
    ~GitRefdb();

    static NAN_METHOD(Compress);

    struct OpenBaton {
      int error_code;
      const git_error* error;
      git_refdb * out;
      git_repository * repo;
     };
    class OpenWorker : public nodegit::AsyncWorker {
      public:
        OpenWorker(
            OpenBaton *_baton,
            Nan::Callback *callback,
            std::map<std::string, std::shared_ptr<nodegit::CleanupHandle>> &cleanupHandles
        ) : nodegit::AsyncWorker(callback, "nodegit:AsyncWorker:GitRefdb:Open", cleanupHandles)
          , baton(_baton) {};
        OpenWorker(const OpenWorker &) = delete;
        OpenWorker(OpenWorker &&) = delete;
        OpenWorker &operator=(const OpenWorker &) = delete;
        OpenWorker &operator=(OpenWorker &&) = delete;
        ~OpenWorker() {};
        void Execute();
        void HandleErrorCallback();
        void HandleOKCallback();
        nodegit::LockMaster AcquireLocks();

      private:
        OpenBaton *baton;
    };

    static NAN_METHOD(Open);
};

#endif
