import string

from compLib.LogstashLogging import logstash_logger
from compLib.Spi import Spi, Register

LINE_COUNT = 4
CHARS_PER_LINE = 16


class Display(object):
    """Access the display on the robot.
    The display is split into 4 Rows and 16 Columns. Each function call changes one line at a time.
    """

    @staticmethod
    def write(line: int, text: str):
        """Write a string of text to the integrated display.

        :param line: Line to write. Between 1 and 4
        :param text: Text to write. Up to 16 characters
        :raises: IndexError
        """
        if len(text) > CHARS_PER_LINE:
            logstash_logger.error(f"Too many characters specified!")
            return

        if line <= 0 or line > LINE_COUNT:
            raise IndexError("Invalid line number specified")

        to_write = [0] * CHARS_PER_LINE
        for i in range(len(text)):
            to_write[i] = ord(text[i])

        if line == 1:
            Spi.write_array(Register.DISPLAY_LINE_1_C0, CHARS_PER_LINE, to_write)
        elif line == 2:
            Spi.write_array(Register.DISPLAY_LINE_2_C0, CHARS_PER_LINE, to_write)
        elif line == 3:
            Spi.write_array(Register.DISPLAY_LINE_3_C0, CHARS_PER_LINE, to_write)
        elif line == 4:
            Spi.write_array(Register.DISPLAY_LINE_4_C0, CHARS_PER_LINE, to_write)

    @staticmethod
    def clear():
        """Clear the display

        """
        for i in range(1, LINE_COUNT + 1):
            Display.write(i, "")
