#include "watcher.hpp"
#include <filesystem>
#include <algorithm>
#include <cassert>

using std::string;
namespace fs = std::filesystem;

void UpdateListener::handleFileAction(efsw::WatchID watchid,
    const string& dir,
    const string& filename,
    efsw::Action action,
    string oldFilename)
{
  (void)watchid;
  (void)action;
  (void)oldFilename;

  // this is some gnarly BS here, probably tons
  // of memory leaks for now but it's working
  int ignored = 1;
  auto the_path = fs::path(dir) / fs::path(filename);
  string full_path = the_path.lexically_normal().string();

  std::replace(full_path.begin(),
      full_path.end(), '\\', '/');

  int rc = git_ignore_path_is_ignored(&ignored, repo, full_path.c_str());
  assert(rc == 0 && "libgit2 says it can't check the ignored file");

  if(!ignored) {
    changes = changes || !ignored;
  }
}

void UpdateListener::reset_state() {
  changes = false;
}