import xml.etree.ElementTree as ET
import re

pattern = re.compile("table_(.)(.*)")
pattern_text = re.compile("label_(.)")


def letters_to_numbers(group_string, number_string):
    out1 = ord(group_string) - ord('a')
    if len(number_string) == 1:
        # only one letter
        out2 = ord(number_string) - ord('a')
    elif len(number_string) == 2:
        # its an umlaut
        out2 = {"ae": 26, "oe": 27, "ue": 28}[number_string]
    return out1, out2

def draw_only_group(group_id):
    tree = ET.parse("mensa.svg")
    root = tree.getroot()

    rects = root.findall(".//{http://www.w3.org/2000/svg}rect")
    rects = list(filter(lambda rect: pattern.match(rect.attrib["id"]), rects))

    texts = root.findall(".//{http://www.w3.org/2000/svg}text")
    texts = list(filter(lambda text: pattern_text.match(text.attrib["id"]), texts))
    for text in texts:
        match = pattern_text.match(text.attrib["id"])
        group = ord(match.group(1)) - ord('a')
        if group != group_id:
            # color the child spans text white
            span = list(text)[0]
            style_span="font-size:3.17499995px;fill:#ffffff;fill-opacity:1;stroke-width:0.26458332;"
            span.attrib["style"] = style_span

    for rect in rects:
        match = pattern.match(rect.attrib["id"])
        group, number = letters_to_numbers(match.group(1), match.group(2))
        if group != group_id:
            # other group, color grey
            rect.attrib["style"] = "fill:#808080;stroke-width:0.26458332"
    groupstring = chr(ord('a') + group_id)
    tree.write(f"mensamap_highlight_group_{groupstring}.svg")

for i in range(9):
    draw_only_group(i)
