#!/usr/bin/env python3
"""Builds the table "which WCAG 2.2 success criterion applies to which kind of media" and checks that the one phrase each row relies on is present in the saved quote text.
The mapping from a kind of video to a row is our reading of the criteria; the script does not check that, only that the phrase is there.
Usage: python3 matrix.py wcag-quotes.json     (made by extract-wcag.py)
A row is printed as yes only when its phrase is in the quoted criterion (or in the quoted note); otherwise the script stops with FAIL."""
import json, sys

NOTE = "if all of the important information in the video track is already conveyed in the audio track, no additional audio description is necessary"
# (kind of media, [(criterion, what it asks, phrase that must be in the criterion text, or NOTE to look in the note instead)])
SCEN = [
    ("Recorded audio only (a podcast episode)",
     [("1.2.1", "A text alternative with equivalent information", "prerecorded audio-only")]),
    ("Recorded video, speech says everything the screen shows (a talking head)",
     [("1.2.2", "Captions for the speech", "captions are provided for all prerecorded audio content in synchronized media, except when the media is a media alternative for text and is clearly labeled as such"),
      ("1.2.3", "No additional description necessary (W3C note)", NOTE),
      ("1.2.5", "No additional description necessary (W3C note)", NOTE)]),
    ("Recorded video, screen shows more than the speech says (a demo)",
     [("1.2.2", "Captions for the speech", "captions are provided for all prerecorded audio content in synchronized media, except when the media is a media alternative for text and is clearly labeled as such"),
      ("1.2.3", "Audio description, or a text alternative covering sound and picture", "an alternative for time-based media or audio description of the prerecorded video content"),
      ("1.2.5", "Audio description", "audio description is provided for all prerecorded video content")]),
    ("Recorded video with no sound (a silent animation)",
     [("1.2.1", "A text alternative, or an audio track, with equivalent information", "prerecorded video-only")]),
    ("Live video with speech (a webinar)",
     [("1.2.4", "Captions for the live audio", "captions are provided for all live audio content in synchronized media")]),
]
LEVEL = {"1.2.1": "A", "1.2.2": "A", "1.2.3": "A", "1.2.4": "AA", "1.2.5": "AA"}

def main(path):
    try:
        data = json.load(open(path, encoding="utf-8"))
    except OSError as e:
        sys.exit("FAIL cannot read %s: %s" % (path, e.strerror))
    except ValueError:
        sys.exit("FAIL %s is not JSON (empty file or the wrong file)" % path)
    crit = data.get("criteria") or {}
    for num, lvl in LEVEL.items():
        if num not in crit or crit[num].get("level") != lvl:
            sys.exit("FAIL criterion %s is missing or not level %s in %s" % (num, lvl, path))
    note = (data.get("note_no_description") or "").lower()
    bad = 0
    print("| Kind of media | Criterion | Level | What it asks | Phrase present in the quote |")
    print("|---|---|---|---|---|")
    for name, cells in SCEN:
        for num, asks, phrase in cells:
            ok = (NOTE in note) if phrase == NOTE else phrase in crit[num]["text"].lower()
            print("| %s | %s | %s | %s | %s |" % (name, num, crit[num]["level"], asks, "yes" if ok else "NO"))
            bad += not ok
    print("\nunmatched cells: %d" % bad)
    if bad:
        sys.exit("FAIL: %d cells are not matched by the quoted text" % bad)
    print("MATRIX-OK")

if __name__ == "__main__":
    if len(sys.argv) != 2:
        sys.exit("usage: python3 matrix.py wcag-quotes.json")
    main(sys.argv[1])
