"""Builds the FCP report: report.html (the summary, sections/summary.html) and one page per study from the
fragments in sections/<topic>/. Studies drop fragments there; only this script writes the pages.
    python3 build.py"""
import datetime
import re
from pathlib import Path

R = Path(__file__).resolve().parent
PAGES = [  # file, nav label, title, fragments, source note
    ("report.html", "Summary", "FCP report", ["sections/summary.html"], None),
    ("collision.html", "Collision & inside-outside", "Collision and inside-outside",
     ["sections/collision/section.html"], "study: ~/code/data/meshes/fcp_2910_collision (sections/collision/README.txt)"),
    ("raycast.html", "Ray casting (provisional)", "Ray casting (provisional)", ["sections/raycast/section.html"],
     "written from docs/raycast.md on branch raycast-slab (sections/raycast/README.txt)"),
    ("solid.html", "Solid properties", "Solid properties",
     ["sections/solid/section.html"], "built by sections/solid/make.py from the fcp_3010_hull study's CSVs"),
    ("hull.html", "Convex hull", "Convex hull", ["sections/hull/section.html"],
     "study: ~/code/data/meshes/fcp_3010_hull (sections/hull/README.txt)"),
    ("volume.html", "Volume", "Volume", ["sections/hull/volume_section.html"],
     "study: ~/code/data/meshes/fcp_3010_hull (sections/hull/README.txt)"),
    ("bounds.html", "Bounding volumes", "Bounding volumes", ["sections/hull/bounds_section.html"],
     "study: ~/code/data/meshes/fcp_3010_hull (sections/hull/README.txt)"),
    ("tradeoffs.html", "Tradeoffs (one thread)", "Tradeoffs (one thread)", ["sections/hull/tradeoffs_section.html"],
     "study: ~/code/data/meshes/fcp_3010_hull (sections/hull/README.txt)"),
    ("monotonicity.html", "Monotonicity", "Monotonicity", ["sections/monotonic/section.html"],
     "built by sections/monotonic/ (README.txt there)"),
]


def frag(path):
    s = (R / path).read_text().strip()
    if "tablewrap" not in s:
        s = s.replace("<table>", '<div class="tablewrap"><table>').replace("</table>", "</table></div>")
    return s


def page(file, title, parts, src):
    nav = "".join(f'<a href="{f}"{" class=\"here\"" if f == file else ""}>{lab}</a>' for f, lab, *_ in PAGES)
    body = "\n".join(frag(p) for p in parts)
    if src:  # study pages: the fragment's <h2> becomes the page's <h1>
        body = re.sub(r"<h2>(.*?)</h2>", r"<h1>\1</h1>", body, count=1)
        stamp = max((R / p).stat().st_mtime for p in parts)
        body = body.replace("</h1>", f'</h1>\n<p class="src">Updated {datetime.datetime.fromtimestamp(stamp):%Y-%m-%d %H:%M}; {src}.</p>', 1)
    return f"""<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{title}</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<nav><div class="in">{nav}</div></nav>
<main>
{body}
</main>
</body>
</html>
"""


for file, _, title, parts, src in PAGES:
    (R / file).write_text(page(file, title, parts, src))
    print("wrote", file)
