// Juega el lema de bombeo completo con cadenas cortas: pump_demo [P_MAX]
// Para cada lenguaje y cada p, lista todas las divisiones s = uvxyz con |vy| > 0 y |vxy| ≤ p,
// y cuenta cuántas salen del lenguaje con i = 2, cuántas necesitan i = 0 y cuántas sobreviven.
// Es una comprobación con p pequeños, no una demostración: la demostración vale para todo p.
#include <cstddef>
#include <functional>
#include <iomanip>
#include <iostream>
#include <string>

#include "pumping.h"

namespace {

void table(const std::string& title, const std::function<bool(const std::string&)>& in_language,
           const std::function<std::string(std::size_t)>& choose, std::size_t p_max) {
    std::cout << title << '\n';
    std::cout << "   p   |s|  divisiones  fuera con i=2  solo con i=0  sobreviven\n";
    for (std::size_t p = 1; p <= p_max; p++) {
        std::string s = choose(p);
        PumpReport report = pump_report(in_language, s, p);
        std::cout << std::setw(4) << p << std::setw(6) << s.size() << std::setw(12) << report.total << std::setw(15)
                  << report.out_with_2 << std::setw(14) << report.out_with_0 << std::setw(12) << report.survive
                  << '\n';
        if (p == p_max && report.survive > 0) {
            const Division& d = report.example;
            auto part = [&s](std::size_t from, std::size_t length) {
                return length == 0 ? std::string("ε") : s.substr(from, length);
            };
            std::cout << "  una división que sobrevive con p = " << p << ": u = " << part(0, d.u)
                      << ", v = " << part(d.u, d.v) << ", x = " << part(d.u + d.v, d.x)
                      << ", y = " << part(d.u + d.v + d.x, d.y) << ", z = "
                      << part(d.u + d.v + d.x + d.y, s.size() - d.u - d.v - d.x - d.y) << '\n';
        }
    }
    std::cout << '\n';
}

std::string repeat(char c, std::size_t n) {
    return std::string(n, c);
}

}  // namespace

int main(int argc, char** argv) {
    std::size_t p_max = argc > 1 ? std::stoul(argv[1]) : 6;
    table("{ 0^n 1^n },  s = 0^p 1^p  (libre de contexto: tiene que haber divisiones que sobreviven)", is_zeros_ones,
          [](std::size_t p) { return repeat('0', p) + repeat('1', p); }, p_max);
    table("{ a^n b^n c^n },  s = a^p b^p c^p", is_anbncn,
          [](std::size_t p) { return repeat('a', p) + repeat('b', p) + repeat('c', p); }, p_max);
    table("{ ww },  s = 0^p 1 0^p 1  (una mala elección)", is_ww,
          [](std::size_t p) { return repeat('0', p) + "1" + repeat('0', p) + "1"; }, p_max);
    table("{ ww },  s = 0^p 1^p 0^p 1^p", is_ww,
          [](std::size_t p) { return repeat('0', p) + repeat('1', p) + repeat('0', p) + repeat('1', p); }, p_max);
    table("{ a^i b^j c^k : i ≤ j ≤ k },  s = a^p b^p c^p", is_ordered_abc,
          [](std::size_t p) { return repeat('a', p) + repeat('b', p) + repeat('c', p); }, p_max);
}
