#include "../source/resource_pack.hpp"
#include <iostream>
#include <filesystem>

constexpr const char* APP_NAME = "ViBe3 Physics";

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

void listPackContents(const std::string& packFile) {
    ResourcePack pack;
    if (!pack.loadPack(packFile)) {
        std::cerr << "Error: Cannot open pack file: " << packFile << std::endl;
        return;
    }
    
    auto resources = pack.getResourceList();
    std::cout << "Pack file: " << packFile << std::endl;
    std::cout << "Resources: " << resources.size() << std::endl;
    std::cout << "Contents:" << std::endl;
    
    for (const auto& resource : resources) {
        std::cout << "  " << resource << std::endl;
    }
}

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

    // Parse arguments
    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]; // Next argument is pack file to list
            }
        } 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" << std::endl;
    std::cout << "===============================================" << std::endl;
    std::cout << "Input directory: " << dataDir << std::endl;
    std::cout << "Output file: " << outputFile << std::endl;
    std::cout << std::endl;
    
    if (!std::filesystem::exists(dataDir)) {
        std::cerr << "Error: Input directory does not exist: " << dataDir << std::endl;
        return 1;
    }
    
    ResourcePack pack;
    
    std::cout << "Scanning and packing resources..." << std::endl;
    if (!pack.addDirectory(dataDir)) {
        std::cerr << "Error: Failed to add directory to pack" << std::endl;
        return 1;
    }
    
    std::cout << "Found " << pack.getResourceCount() << " resources" << std::endl;
    
    std::cout << "Saving pack file..." << std::endl;
    if (!pack.savePack(outputFile)) {
        std::cerr << "Error: Failed to save pack file" << std::endl;
        return 1;
    }
    
    std::filesystem::path packPath(outputFile);
    auto fileSize = std::filesystem::file_size(packPath);
    
    std::cout << "Pack file created successfully!" << std::endl;
    std::cout << "File size: " << (fileSize / 1024.0 / 1024.0) << " MB" << std::endl;
    
    return 0;
}