#include <filesystem>
#include <iostream>

#include "core/resources/resource_pack.h"

static constexpr const char* APP_NAME = "Coffee Crisis";

void showHelp() {
    std::cout << APP_NAME << " - Resource Packer" << '\n';
    std::cout << "===============================================" << '\n';
    std::cout << "Usage: pack_resources [options] [input_dir] [output_file]" << '\n';
    std::cout << '\n';
    std::cout << "Options:" << '\n';
    std::cout << "  --help     Show this help message" << '\n';
    std::cout << "  --list     List contents of an existing pack file" << '\n';
    std::cout << '\n';
    std::cout << "Arguments:" << '\n';
    std::cout << "  input_dir  Directory to pack (default: data)" << '\n';
    std::cout << "  output_file Pack file name (default: resources.pack)" << '\n';
}

void listPackContents(const std::string& packFile) {
    ResourcePack pack;
    if (!pack.loadPack(packFile)) {
        std::cerr << "Error: Cannot open pack file: " << packFile << '\n';
        return;
    }

    auto resources = pack.getResourceList();
    std::cout << "Pack file: " << packFile << '\n';
    std::cout << "Resources: " << resources.size() << '\n';
    std::cout << "Contents:" << '\n';

    for (const auto& resource : resources) {
        std::cout << "  " << resource << '\n';
    }
}

int main(int argc, char* argv[]) {
    std::string dataDir = "data";
    std::string outputFile = "resources.pack";
    bool listMode = false;
    bool dataDirSet = false;

    for (int i = 1; i < argc; i++) {
        std::string arg = argv[i];
        if (arg == "--help" || arg == "-h") {
            showHelp();
            return 0;
        } else if (arg == "--list") {
            listMode = true;
            if (i + 1 < argc) {
                outputFile = argv[++i];
            }
        } else if (!arg.empty() && arg[0] != '-') {
            if (!dataDirSet) {
                dataDir = arg;
                dataDirSet = true;
            } else {
                outputFile = arg;
            }
        }
    }

    if (listMode) {
        listPackContents(outputFile);
        return 0;
    }

    std::cout << APP_NAME << " - Resource Packer" << '\n';
    std::cout << "===============================================" << '\n';
    std::cout << "Input directory: " << dataDir << '\n';
    std::cout << "Output file: " << outputFile << '\n';
    std::cout << '\n';

    if (!std::filesystem::exists(dataDir)) {
        std::cerr << "Error: Input directory does not exist: " << dataDir << '\n';
        return 1;
    }

    ResourcePack pack;

    std::cout << "Scanning and packing resources..." << '\n';
    if (!pack.addDirectory(dataDir)) {
        std::cerr << "Error: Failed to add directory to pack" << '\n';
        return 1;
    }

    std::cout << "Found " << pack.getResourceCount() << " resources" << '\n';

    std::cout << "Saving pack file..." << '\n';
    if (!pack.savePack(outputFile)) {
        std::cerr << "Error: Failed to save pack file" << '\n';
        return 1;
    }

    std::filesystem::path packPath(outputFile);
    auto fileSize = std::filesystem::file_size(packPath);

    std::cout << "Pack file created successfully!" << '\n';
    std::cout << "File size: " << (fileSize / 1024.0 / 1024.0) << " MB" << '\n';

    return 0;
}
