
"""This module provides fancy text display on a 7-segment display."""

from . import color as c

letters = {
    '1': ('00100', '01100', '00100', '00100', '00100', '00100', '01110'),
    '2': ('01110', '10001', '00001', '00010', '00100', '01000', '11111'),
    '3': ('01110', '10001', '00001', '00110', '00001', '10001', '01110'),
    '4': ('00100', '01000', '10100', '11111', '00100', '00100', '00100'),
    '5': ('11111', '10000', '10000', '11110', '00001', '00001', '11110'),
    '6': ('01110', '10001', '10000', '11110', '10001', '10001', '01110'),
    '7': ('11111', '00001', '00010', '00100', '01000', '01000', '01000'),
    '8': ('01110', '10001', '10001', '01110', '10001', '10001', '01110'),
    '9': ('01110', '10001', '10001', '01111', '00001', '00001', '01110'),
    '0': ('01110', '10001', '10001', '10101', '10001', '10001', '01110'),
    '-': ('00000', '00000', '00000', '11111', '00000', '00000', '00000'),
    '_': ('00000', '00000', '00000', '00000', '00000', '00000', '11111'),
    ' ': ('00000', '00000', '00000', '00000', '00000', '00000', '00000'),
    '.': ('00000', '00000', '00000', '00000', '00000', '00000', '00100'),
    ',': ('00000', '00000', '00000', '00000', '00000', '00100', '01000'),
    ';': ('00000', '00000', '00000', '00100', '00000', '00100', '01000'),
    ':': ('00000', '00000', '00000', '00100', '00000', '00000', '00100'),
    '!': ('00100', '00100', '00100', '00100', '00100', '00000', '00100'),
    '?': ('01110', '10001', '00001', '00010', '00100', '00000', '00100'),
    '+': ('00000', '00100', '00100', '11111', '00100', '00100', '00000'),
    '*': ('00000', '10001', '01010', '00100', '01010', '10001', '00000'),
    '/': ('00000', '00001', '00010', '00100', '01000', '10000', '00000'),
    '%': ('11001', '11001', '00010', '00100', '01000', '10011', '10011'),
    '=': ('00000', '00000', '11111', '00000', '11111', '00000', '00000'),
    '(': ('00010', '00100', '01000', '01000', '01000', '00100', '00010'),
    ')': ('01000', '00100', '00010', '00010', '00010', '00100', '01000'),
    '$': ('00100', '01110', '10100', '11110', '00101', '11110', '00100'),
    '€': ('00111', '01000', '11110', '10000', '11110', '01000', '00111'),
    '£': ('00100', '01010', '10000', '11100', '10000', '11110', '00100'),
    '¥': ('10001', '01010', '00100', '11111', '00100', '11111', '00100'),
    '&': ('01100', '10010', '10100', '01000', '10101', '10010', '01101'),
    'a': ('00000', '00000', '01110', '00001', '01111', '10001', '01111'),
    'b': ('10000', '10000', '11110', '10001', '10001', '10001', '11110'),
    'c': ('00000', '00000', '01110', '10001', '10000', '10001', '01110'),
    'd': ('00001', '00001', '01111', '10001', '10001', '10001', '01111'),
    'e': ('00000', '00000', '01110', '10001', '11111', '10000', '01110'),
    'f': ('00110', '01001', '01000', '11100', '01000', '01000', '01000'),
    'g': ('00000', '00000', '01111', '10001', '10001', '01111', '00001'),
    'h': ('10000', '10000', '11110', '10001', '10001', '10001', '10001'),
    'i': ('00100', '00000', '01100', '00100', '00100', '00100', '01110'),
    'j': ('00010', '00000', '00110', '00010', '00010', '10010', '01100'),
    'k': ('10000', '10000', '10010', '10100', '11000', '10100', '10010'),
    'l': ('01100', '00100', '00100', '00100', '00100', '00100', '01110'),
    'm': ('00000', '00000', '11010', '10101', '10101', '10101', '10101'),
    'n': ('00000', '00000', '11110', '10001', '10001', '10001', '10001'),
    'o': ('00000', '00000', '01110', '10001', '10001', '10001', '01110'),
    'p': ('00000', '00000', '11110', '10001', '10001', '11110', '10000'),
    'q': ('00000', '00000', '01111', '10001', '10001', '01111', '00001'),
    'r': ('00000', '00000', '10110', '11001', '10000', '10000', '10000'),
    's': ('00000', '00000', '01111', '10000', '01110', '00001', '11110'),
    't': ('01000', '01000', '11100', '01000', '01000', '01001', '00110'),
    'u': ('00000', '00000', '10001', '10001', '10001', '10001', '01111'),
    'v': ('00000', '00000', '10001', '10001', '10001', '01010', '00100'),
    'w': ('00000', '00000', '10001', '10001', '10101', '10101', '01010'),
    'x': ('00000', '00000', '10001', '01010', '00100', '01010', '10001'),
    'y': ('00000', '00000', '10001', '10001', '10001', '01111', '00001'),
    'z': ('00000', '00000', '11111', '00010', '00100', '01000', '11111'),
    'A': ('01110', '10001', '10001', '11111', '10001', '10001', '10001'),
    'B': ('11110', '10001', '10001', '11110', '10001', '10001', '11110'),
    'C': ('01110', '10001', '10000', '10000', '10000', '10001', '01110'),
    'D': ('11110', '10001', '10001', '10001', '10001', '10001', '11110'),
    'E': ('11111', '10000', '10000', '11110', '10000', '10000', '11111'),
    'F': ('11111', '10000', '10000', '11110', '10000', '10000', '10000'),
    'G': ('01110', '10001', '10000', '10000', '10011', '10001', '01110'),
    'H': ('10001', '10001', '10001', '11111', '10001', '10001', '10001'),
    'I': ('01110', '00100', '00100', '00100', '00100', '00100', '01110'),
    'J': ('00001', '00001', '00001', '00001', '00001', '10001', '01110'),
    'K': ('10001', '10010', '10100', '11000', '10100', '10010', '10001'),
    'L': ('10000', '10000', '10000', '10000', '10000', '10000', '11111'),
    'M': ('10001', '11011', '10101', '10101', '10001', '10001', '10001'),
    'N': ('10001', '10001', '11001', '10101', '10011', '10001', '10001'),
    'O': ('01110', '10001', '10001', '10001', '10001', '10001', '01110'),
    'P': ('11110', '10001', '10001', '11110', '10000', '10000', '10000'),
    'Q': ('01110', '10001', '10001', '10001', '10101', '10010', '01101'),
    'R': ('11110', '10001', '10001', '11110', '10100', '10010', '10001'),
    'S': ('01110', '10001', '10000', '01110', '00001', '10001', '01110'),
    'T': ('11111', '00100', '00100', '00100', '00100', '00100', '00100'),
    'U': ('10001', '10001', '10001', '10001', '10001', '10001', '01110'),
    'V': ('10001', '10001', '10001', '10001', '10001', '01010', '00100'),
    'W': ('10001', '10001', '10001', '10101', '10101', '10101', '01010'),
    'X': ('10001', '10001', '01010', '00100', '01010', '10001', '10001'),
    'Y': ('10001', '10001', '01010', '00100', '00100', '00100', '00100'),
    'Z': ('11111', '00001', '00010', '00100', '01000', '10000', '11111'),
}


def build_text_code(text, on="#", off=" ", spacing=1):
    """Helper for big_print."""
    lines = [''] * 7
    for letter in text:
        try:
            letters[letter]
        except KeyError:
            letter = " "
        for i, line in enumerate(letters[letter]):
            lines[i] += line + "0" * spacing
    return lines


def big_text(
    text: str,
    on: str = "#",
    off: str = " ",
    spacing: int = 1,
    underline: bool = False,
    color: str = None,
):
    """Prints text in big letters.
    Parameters:
    - on: The character to use for the on pixels.
    - off: The character to use for the off pixels.
    - spacing: The number of spaces between each letter.
    - underline: Whether to underline the text or not.
    - color: The color to use for the text (flamewok.colors)
    """
    display = ''
    if color is None:
        color = ''
        color_end = ''
    else:
        color = color
        color_end = c.end
    lines = build_text_code(text, on, off, spacing)
    for line in lines:
        display_line = ""
        for i in line:
            if i == "0":
                display_line += off
            else:
                display_line += on
        display += color + display_line + color_end + "\n"
    if underline:
        for i in range(spacing):
            display += color + (off * len(display_line)) + color_end + "\n"
        display += color + (on * len(display_line)) + color_end + "\n"
    return display
