#include "fileSystem.h"
#include <FS.h>
#include <LittleFS.h>

static const char *tag = "fs";

#define LogEnabled true

void Init_File_System(void){
    if (!LittleFS.begin()){
        ESP_LOGE(tag, "An Error occured while mounting LittleFS");
        return;
    }

    // Get all information of your LITTLEFS
    ESP_LOGI(tag, "File system info.");
    ESP_LOGI(tag, "Total: %d, Used: %d, Free:%d", LittleFS.totalBytes(), LittleFS.usedBytes(), LittleFS.totalBytes() - LittleFS.usedBytes());

    // Open dir folder
    File dir = LittleFS.open("/");

    // Cycle all the content
    //printAllSystemFiles();
}

void getAllDirectories(String (&directoryList)[MAX_DIRECTORIES], int &count){
    File root = LittleFS.open("/");
    if (!root || !root.isDirectory()){
        ESP_LOGE("FileSystem", "Failed to open root directory or root is not a directory");
        return;
    }

    directoryList[0] = "/";
    count = 1;

    File file = root.openNextFile();
    while (file) {
        if (file.isDirectory()){
            if (count < 10){ // Ensure we don't overflow the array
                directoryList[count] = '/' + String(file.name());
                count++;
            }else{
                ESP_LOGW("FileSystem", "Directory list array is full");
                break;
            }
        }
        file = root.openNextFile();
    }

    root.close();
}

void printFilesInDirectories(const String directoryList[], int count){
    for (int i = 0; i < count; i++){
        ESP_LOGI(tag, "Dir: %s", directoryList[i].c_str());

        File dir = LittleFS.open(directoryList[i]);
        if (!dir || !dir.isDirectory()){
            ESP_LOGE(tag, "Failed to open directory: %s", directoryList[i].c_str());
            continue;
        }

        File file = dir.openNextFile();
        while (file){
            if (!file.isDirectory()){
                ESP_LOGI(tag, "    File: %s", file.name());
            }
            file = dir.openNextFile();
        }

        dir.close();
    }
}

void printAllSystemFiles(void){
    String directories[MAX_DIRECTORIES];
    int dirCount = 0;

    getAllDirectories(directories, dirCount);

    ESP_LOGI(tag, "File System Listing:");
    printFilesInDirectories(directories, dirCount);
}

/*****************************************************************************************/

/*
void getAllDirectoriesRecursive(const String& path, String directoryList[], int& count, int maxSize) {
    File dir = LittleFS.open(path);
    if (!dir || !dir.isDirectory()) {
        ESP_LOGE("FileSystem", "Failed to open directory: %s", path.c_str());
        return;
    }

    File file = dir.openNextFile();
    while (file) {
        if (file.isDirectory()) {
            if (count < maxSize) { // Ensure we don't overflow the array
                String dirName = String(file.name());
                if (!dirName.startsWith("/")) {
                    dirName = "/" + dirName;
                }
                directoryList[count] = dirName;
                count++;
                getAllDirectoriesRecursive(dirName, directoryList, count, maxSize); // Recursive call for subdirectories
            } else {
                ESP_LOGW("FileSystem", "Directory list array is full");
                break;
            }
        }
        file = dir.openNextFile();
    }

    dir.close();
}

void getAllDirectories(String directoryList[], int& count, int maxSize) {
    count = 0;
    getAllDirectoriesRecursive("/", directoryList, count, maxSize);
}

void printFilesInDirectories(const String directoryList[], int count) {
    for (int i = 0; i < count; i++) {
        ESP_LOGD(tag, "Dir: %s", directoryList[i].c_str());

        File dir = LittleFS.open(directoryList[i]);
        if (!dir || !dir.isDirectory()) {
            ESP_LOGE(tag, "Failed to open directory: %s", directoryList[i].c_str());
            continue;
        }

        File file = dir.openNextFile();
        while (file) {
            if (!file.isDirectory()) {
                ESP_LOGD(tag, "    File: %s", file.name());
            }
            file = dir.openNextFile();
        }

        dir.close();
    }
}

void printAllFiles(int maxDirs) {
    String directories[maxDirs];
    int dirCount = 0;

    getAllDirectories(directories, dirCount, maxDirs);

    ESP_LOGD(tag, "File System Listing:");
    printFilesInDirectories(directories, dirCount);
}
*/