/*
 * Copyright 2023 ailurux <ailuruxx@gmail.com>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */
#include "lua/file_iterator.hpp"
#include "esp_log.h"

#include <string>
#include <algorithm>

#include "drivers/spi.hpp"
#include "ff.h"

namespace lua {

[[maybe_unused]] static const char* kTag = "FileIterator";

FileIterator::FileIterator(std::string filepath, bool showHidden)
    : original_path_(filepath), show_hidden_(showHidden), current_(), offset_(-1) {
  const TCHAR* path = static_cast<const TCHAR*>(filepath.c_str());
  FRESULT res = f_opendir(&dir_, path);
  if (res != FR_OK) {
    ESP_LOGE(kTag, "Error opening directory: %s", filepath.c_str());
  }
}

FileIterator::~FileIterator() {
  f_closedir(&dir_);
}

auto FileIterator::value() const -> const std::optional<FileEntry>& {
  return current_;
}

auto FileIterator::next() -> void {
  size_t prev_index = -1;
  if (current_) {
    prev_index = current_->index;
  }
  do {
    bool res = iterate(show_hidden_);
    if (!res) {
      break;
    }
  } while (!current_ || current_->index == prev_index);
}

auto FileIterator::prev() -> void {
  f_rewinddir(&dir_);
  if (offset_ <= 0) {
    offset_ = -1;
    current_.reset();
    return;
  }
  auto new_offset = offset_ - 1;
  offset_ = -1;
  while (offset_ < new_offset) {
    if (!iterate(show_hidden_)) {
      break;
    }
  }
}

auto FileIterator::iterate(bool show_hidden) -> bool {
  FILINFO info;
  auto res = f_readdir(&dir_, &info);
  if (res != FR_OK) {
    ESP_LOGE(kTag, "Error reading directory. Error: %d", res);
    return false;
  }
  if (info.fname[0] == 0) {
    // End of directory
    // Set value to nil
    current_.reset();
    return false;
  } else {
    // Update current value
    bool hidden =  (info.fattrib & AM_HID) > 0 || info.fname[0] == '.';
    if (!hidden || show_hidden) {
      offset_++;
      current_ = FileEntry{
          .index = offset_,
          .isHidden = hidden,
          .isDirectory = (info.fattrib & AM_DIR) > 0,
          .filepath = original_path_ + (original_path_.size() > 0 ? "/" : "") +
                      info.fname,
          .name = info.fname,
      };
    }
  }
  return true;
}



FileIteratorSorted::FileIteratorSorted(std::string filepath, bool showHidden)
    : offset_(-1) {

  FileIterator iter(filepath, showHidden);

  while (true) {
    iter.next();
    std::optional<FileEntry> res = iter.value();
    if (res) {
      files_.push_back(*res);
    } else {
      break;
    }
  }

  std::sort(files_.begin(), files_.end(),
    [](const auto& lhs, const auto& rhs) {
        if (lhs.isDirectory > rhs.isDirectory) {
          return true;
        } else if (lhs.isDirectory < rhs.isDirectory) {
          return false;
        }

        return lhs.name < rhs.name;
    });

  // reindex the files
  for(size_t i = 0; i != files_.size(); i++) {
    files_[i].index = i + 1;
  }
}

FileIteratorSorted::~FileIteratorSorted() {
}

auto FileIteratorSorted::value() const -> const std::optional<FileEntry> {
  if (offset_ < 0 || offset_ >= files_.size()) {
    return std::nullopt;
  }
  return std::optional(files_[offset_]);
}

auto FileIteratorSorted::next() -> void {
  if (offset_ < (int) files_.size()) {
    offset_++;
  }
}

auto FileIteratorSorted::prev() -> void {
  if (offset_ <= 0) {
    offset_ = -1;
    return;
  }

  offset_--;
}


}  // namespace lua