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

#ifndef GITPATCH_H
#define GITPATCH_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/convenient_patch.h"
#include "../include/blob.h"
#include "../include/diff_options.h"
#include "../include/diff.h"
#include "../include/diff_delta.h"
#include "../include/diff_hunk.h"
#include "../include/diff_line.h"
#include "../include/repository.h"
// Forward declaration.
struct git_patch {
};

using namespace node;
using namespace v8;

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

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

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

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

                                                        

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

    struct FromBlobsBaton {
      int error_code;
      const git_error* error;
      git_patch * out;
      const git_blob * old_blob;
      const char * old_as_path;
      const git_blob * new_blob;
      const char * new_as_path;
      const git_diff_options * opts;
     };
    class FromBlobsWorker : public nodegit::AsyncWorker {
      public:
        FromBlobsWorker(
            FromBlobsBaton *_baton,
            Nan::Callback *callback,
            std::map<std::string, std::shared_ptr<nodegit::CleanupHandle>> &cleanupHandles
        ) : nodegit::AsyncWorker(callback, "nodegit:AsyncWorker:GitPatch:FromBlobs", cleanupHandles)
          , baton(_baton) {};
        FromBlobsWorker(const FromBlobsWorker &) = delete;
        FromBlobsWorker(FromBlobsWorker &&) = delete;
        FromBlobsWorker &operator=(const FromBlobsWorker &) = delete;
        FromBlobsWorker &operator=(FromBlobsWorker &&) = delete;
        ~FromBlobsWorker() {};
        void Execute();
        void HandleErrorCallback();
        void HandleOKCallback();
        nodegit::LockMaster AcquireLocks();

      private:
        FromBlobsBaton *baton;
    };

    static NAN_METHOD(FromBlobs);

    struct FromDiffBaton {
      int error_code;
      const git_error* error;
      git_patch * out;
      git_diff * diff;
      size_t idx;
     };
    class FromDiffWorker : public nodegit::AsyncWorker {
      public:
        FromDiffWorker(
            FromDiffBaton *_baton,
            Nan::Callback *callback,
            std::map<std::string, std::shared_ptr<nodegit::CleanupHandle>> &cleanupHandles
        ) : nodegit::AsyncWorker(callback, "nodegit:AsyncWorker:GitPatch:FromDiff", cleanupHandles)
          , baton(_baton) {};
        FromDiffWorker(const FromDiffWorker &) = delete;
        FromDiffWorker(FromDiffWorker &&) = delete;
        FromDiffWorker &operator=(const FromDiffWorker &) = delete;
        FromDiffWorker &operator=(FromDiffWorker &&) = delete;
        ~FromDiffWorker() {};
        void Execute();
        void HandleErrorCallback();
        void HandleOKCallback();
        nodegit::LockMaster AcquireLocks();

      private:
        FromDiffBaton *baton;
    };

    static NAN_METHOD(FromDiff);

    static NAN_METHOD(GetDelta);

    struct GetHunkBaton {
      int error_code;
      const git_error* error;
      const git_diff_hunk * out;
      size_t * lines_in_hunk;
      git_patch * patch;
      size_t hunk_idx;
     };
    class GetHunkWorker : public nodegit::AsyncWorker {
      public:
        GetHunkWorker(
            GetHunkBaton *_baton,
            Nan::Callback *callback,
            std::map<std::string, std::shared_ptr<nodegit::CleanupHandle>> &cleanupHandles
        ) : nodegit::AsyncWorker(callback, "nodegit:AsyncWorker:GitPatch:GetHunk", cleanupHandles)
          , baton(_baton) {};
        GetHunkWorker(const GetHunkWorker &) = delete;
        GetHunkWorker(GetHunkWorker &&) = delete;
        GetHunkWorker &operator=(const GetHunkWorker &) = delete;
        GetHunkWorker &operator=(GetHunkWorker &&) = delete;
        ~GetHunkWorker() {};
        void Execute();
        void HandleErrorCallback();
        void HandleOKCallback();
        nodegit::LockMaster AcquireLocks();

      private:
        GetHunkBaton *baton;
    };

    static NAN_METHOD(GetHunk);

    struct GetLineInHunkBaton {
      int error_code;
      const git_error* error;
      const git_diff_line * out;
      git_patch * patch;
      size_t hunk_idx;
      size_t line_of_hunk;
     };
    class GetLineInHunkWorker : public nodegit::AsyncWorker {
      public:
        GetLineInHunkWorker(
            GetLineInHunkBaton *_baton,
            Nan::Callback *callback,
            std::map<std::string, std::shared_ptr<nodegit::CleanupHandle>> &cleanupHandles
        ) : nodegit::AsyncWorker(callback, "nodegit:AsyncWorker:GitPatch:GetLineInHunk", cleanupHandles)
          , baton(_baton) {};
        GetLineInHunkWorker(const GetLineInHunkWorker &) = delete;
        GetLineInHunkWorker(GetLineInHunkWorker &&) = delete;
        GetLineInHunkWorker &operator=(const GetLineInHunkWorker &) = delete;
        GetLineInHunkWorker &operator=(GetLineInHunkWorker &&) = delete;
        ~GetLineInHunkWorker() {};
        void Execute();
        void HandleErrorCallback();
        void HandleOKCallback();
        nodegit::LockMaster AcquireLocks();

      private:
        GetLineInHunkBaton *baton;
    };

    static NAN_METHOD(GetLineInHunk);

    static NAN_METHOD(LineStats);

    static NAN_METHOD(NumHunks);

    static NAN_METHOD(NumLinesInHunk);

    static NAN_METHOD(Owner);

    static NAN_METHOD(Size);

    struct ConvenientFromDiffBaton {
      int error_code;
      const git_error* error;
      git_diff * diff;
      std::vector<int> indexes;
      std::vector<PatchData*> * out;
     };
    class ConvenientFromDiffWorker : public nodegit::AsyncWorker {
      public:
        ConvenientFromDiffWorker(
            ConvenientFromDiffBaton *_baton,
            Nan::Callback *callback,
            std::map<std::string, std::shared_ptr<nodegit::CleanupHandle>> &cleanupHandles
        ) : nodegit::AsyncWorker(callback, "nodegit:AsyncWorker:GitPatch:ConvenientFromDiff", cleanupHandles)
          , baton(_baton) {};
        ConvenientFromDiffWorker(const ConvenientFromDiffWorker &) = delete;
        ConvenientFromDiffWorker(ConvenientFromDiffWorker &&) = delete;
        ConvenientFromDiffWorker &operator=(const ConvenientFromDiffWorker &) = delete;
        ConvenientFromDiffWorker &operator=(ConvenientFromDiffWorker &&) = delete;
        ~ConvenientFromDiffWorker() {};
        void Execute();
        void HandleErrorCallback();
        void HandleOKCallback();
        nodegit::LockMaster AcquireLocks();

      private:
        ConvenientFromDiffBaton *baton;
    };

    static NAN_METHOD(ConvenientFromDiff);
};

#endif
