#include "../include/FileUtils.h"
#include <filesystem>
#include <fstream>
#include <iostream>
#include <vector>

bool FileUtils::fileExists(const char *path){
    return filesystem::exists(path);
}

vector<unsigned char> FileUtils::open_image(const string& path) {
    ifstream file(path, ios::binary);
    if (!file) {
        cerr << "Error opening file: " << path << endl;
        return {}; // Return an empty vector on error
    }

    vector<unsigned char> data;
    char c;

    while (file.get(c)) {
        data.push_back(static_cast<unsigned char>(c));
    }

    file.close();
    return data;
}

vector<string> FileUtils::get_image_list(const string& path) {
    vector<string> images;
    const string ext(".avif");
    for (auto &p : filesystem::recursive_directory_iterator(path)) {
        if (p.path().extension() == ext) {
            images.push_back(p.path().stem().string());
        }
    }

    return images;
}

vector<string> FileUtils::get_wordlists(const string& path) {
    vector<string> images;
    const string ext(".txt");
    for (auto &p : filesystem::recursive_directory_iterator(path)) {
        if (p.path().extension() == ext) {
            images.push_back(p.path().string());
        }
    }

    return images;
}