from typing import List
from typing import Dict


def _build_record_dict(record_strings: List[str]) -> Dict:
    """
    Parse the pbn line by line. When a block section like "Auction" or "Play" is encountered, collect all the content of
     the block into a single entry
    :param record_strings: List of string lines for a single board
    :return: A dictionary mapping keys from the pbn to strings or other useful values (e.g. list of strings for the
    bidding record)
    """
    record_dict = {}
    # Janky while loop to handle non bracketed lines
    i = 0
    while i < len(record_strings):
        record_string = record_strings[i]
        if not (record_string.startswith("[") or record_string.startswith("{")):
            i += 1
            continue
        if record_string.startswith("{"):
            commentary = ""
            while i < len(record_strings):
                record_string = record_strings[i]
                if record_string.startswith("["):
                    break
                commentary += record_string + " "
                i += 1
            record_dict["Commentary"] = commentary.strip()
            continue
        if record_string.startswith("[") and "]" not in record_string:
            while "]" not in record_string:
                i += 1
                record_string = record_string + record_strings[i]
        record_string = record_string.replace("[", "").replace("]", "")
        key, value = record_string.split(maxsplit=1)
        value = value.replace('"', "")
        if key == "Note":
            number, message = value.split(":", maxsplit=1)
            key = key + "_" + number
            value = message
        record_dict[key] = value
        if key == "Auction":
            auction_record = []
            i += 1
            while i < len(record_strings):
                auction_str = record_strings[i]
                if "[" in auction_str:
                    break
                auction_record.extend(auction_str.split())
                i += 1
            record_dict["bidding_record"] = auction_record

        elif key == "Play":
            play_record = []
            i += 1
            while i < len(record_strings):
                play_str = record_strings[i]
                if "[" in play_str or play_str == "*":
                    break
                play_record.append(play_str.split())
                i += 1
            record_dict["play_record"] = play_record
        else:
            i += 1
    return record_dict