#include <iostream>
#include <regex>
#include <sstream>
#include <string>

#include "cli11/CLI11.hpp"

#include "PhotoCompressArchiver.hh"

// Alias the namespace for readability
namespace sfs = std::filesystem;

void header()
{
    std::cout << BOLD << "Photo Compress Archiver -- " << version << ENDC << "\n"
              << "  Copyright 2017-2025 -- David Sorber\n"
              << "  >>> Utilizing packJPG by Matthias Stirner\n" << std::endl;
}

// NOTE: useful filter: -f ^\\._.+$
int main(int argc, char** argv)
{
    // Options
    bool decompress = false;
    bool delete_orig = false;
    bool verbose = false;
    const uint32_t num_cores(std::thread::hardware_concurrency());
    uint32_t num_threads = num_cores;
    std::string filter_str;
    std::string search_path;

    // Build header into single string for use below
    std::ostringstream header_strm;
    header_strm << BOLD << "Photo Compress Archiver -- " << version << ENDC << "\n"
                << "  Copyright 2017-2025 -- David Sorber\n"
                << "  >>> Utilizing packJPG by Matthias Stirner";

    CLI::App app{header_strm.str()};

    // app.add_flag("-h,--help", showHelp, "display help message");
    app.add_flag("-d,--decompress", decompress, "find '*.pjg' files and decompress them");
    app.add_flag("-k,--delete-orig", delete_orig, "delete original files");
    app.add_flag("-v,--verbose", verbose, "verbose output while processing");
    app.add_option("-t,--num-threads", num_threads, "number of processing threads to use (max 2x system cores)");
    app.add_option("-f,--filter", filter_str, "regex with which to filter out input files");
    app.add_option("search_path", search_path, "path in which to recursively look for input files");

    CLI11_PARSE(app, argc, argv);

    // Make sure that path specified actually exists before proceeding
    if (! sfs::exists(search_path))
    {
        std::cerr << BOLD << RED << "\nERROR: " << ENDC << "the path \"" 
                  << search_path << "\" does not exist\n" << std::endl;
        return -2;
    }
    
    // Validate number of threads
    if (num_threads > (2 * num_cores))
    {
        std::cerr << BOLD << RED << "\nERROR: " << ENDC << "the maximum number "
                  << "of threads is: " << (2 * num_cores) << "(2 * " << num_cores 
                 << ")\n" << std::endl;
        return -2;
    }
    
    // Check for, and if present validate the filter regex
    std::regex filter_regex("");
    if (! filter_str.empty())
    {
        try
        {
            filter_regex.assign(filter_str, std::regex_constants::ECMAScript);
        }
        catch (std::exception& e) 
        {
            std::cerr << BOLD << RED << "ERROR: " << ENDC << "the filter \"" 
                      << filter_str << "\" is not a valid regular expression.\n";
            std::cerr << "       " << e.what() << std::endl;
            return -1;
        }
    }
    
    // Display header and options
    header();
    std::cout << "  [" << num_cores << " cores detected]\n" << std::endl;
    std::cout << BOLD << ">>> Options:" << ENDC << std::endl;
    
    std::cout << "  decompress mode        => " << (decompress ? "ON" : "OFF") << std::endl;
    std::cout << "  delete original files  => " << (delete_orig ? "ON" : "OFF") << std::endl;
    std::cout << "  verbose                => " << (verbose ? "ON" : "OFF") << std::endl;
    std::cout << "  num processing threads => " << num_threads << std::endl;
    if (filter_str.empty())
    {
        std::cout << "  filter regex           => OFF\n";
    }
    else
    {
        std::cout << "  filter regex           => ON (" << filter_str << ")\n";
    }
    std::cout << std::endl;


    // Create the PCA object and let 'er rip!
    auto pca = new PhotoCompressArchiver(search_path,
                                         num_threads,
                                         decompress,
                                         delete_orig,
                                         verbose,
                                         filter_str.empty(),
                                         filter_regex);
    pca->execute();
    delete pca;
    
    return 0;
}
