#!/usr/bin/env python3
"""Recounts every draft in inputs.json in Python and compares with measure.json (Node and the live Word counter)."""
import json
d = {i: s for i, _, s in json.load(open("inputs.json", encoding="utf-8"))["drafts"]}
m = json.load(open("measure.json", encoding="utf-8"))
bad = 0
same = 0
print(f"{'draft':18}{'py utf16':>9}{'node':>6}{'tool':>6}{'py len':>8}{'node cp':>8}  Python=Node  tool=UTF-16")
for r in m["rows"]:
    s = d[r["id"]]
    u = len(s.encode("utf-16-le")) // 2
    ok = u == r["utf16"] and len(s) == r["codepoints"] and len(s.encode()) == r["utf8bytes"]
    bad += not ok
    if r["tool"] == u:
        same += 1
        t = "yes"
    elif r["tool"] == len(s.replace("\r\n", "\n").encode("utf-16-le")) // 2:
        t = "no: the text box turned each CR LF into one break"
    else:
        t = "NO"
    print(f"{r['id']:18}{u:9}{r['utf16']:6}{r['tool']:6}{len(s):8}{r['codepoints']:8}  {'yes' if ok else 'NO':11}  {t}")
print("Python and Node agree on all drafts" if not bad else f"{bad} Python/Node MISMATCH")
print(f"Word counter = UTF-16 length of the draft on {same} of {len(m['rows'])} drafts")
