import yaml
import pygame
from typing import KeysView, Tuple

"""
    TODO description
"""
class ConfigurationManager:
    
    CONFIG_FILE_LOCATION = "config.yaml"
    configuration = []

    @classmethod
    def load(cls) -> None:
        with open(cls.CONFIG_FILE_LOCATION, "r") as yaml_file:
            cls.configuration = yaml.safe_load(yaml_file)

    @classmethod
    def get(cls, key: str, sub_key: str = None):
        if sub_key:
            return cls.configuration[key][sub_key]
        else:
            return cls.configuration[key]

"""
    TODO description
"""
class TextGenerator:

    sheet = None
    glyph_size = (-1, -1)
    characters = { }

    @classmethod
    def load(cls, file: str, glyph_size: Tuple) -> None:
        cls.sheet = pygame.image.load(file)
        cls.glyph_size = glyph_size

        # load character positions in bitmap into the characters dictionary
        # letters
        cls.characters["A"] = (9 * glyph_size[0], 2 * glyph_size[1])
        cls.characters["B"] = (10 * glyph_size[0], 2 * glyph_size[1])
        cls.characters["C"] = (11 * glyph_size[0], 2 * glyph_size[1])
        cls.characters["D"] = (0 * glyph_size[0], 3 * glyph_size[1])
        cls.characters["E"] = (1 * glyph_size[0], 3 * glyph_size[1])
        cls.characters["F"] = (2 * glyph_size[0], 3 * glyph_size[1])
        cls.characters["G"] = (3 * glyph_size[0], 3 * glyph_size[1])
        cls.characters["H"] = (4 * glyph_size[0], 3 * glyph_size[1])
        cls.characters["I"] = (5 * glyph_size[0], 3 * glyph_size[1])
        cls.characters["J"] = (6 * glyph_size[0], 3 * glyph_size[1])
        cls.characters["K"] = (7 * glyph_size[0], 3 * glyph_size[1])
        cls.characters["L"] = (8 * glyph_size[0], 3 * glyph_size[1])
        cls.characters["M"] = (9 * glyph_size[0], 3 * glyph_size[1])
        cls.characters["N"] = (10 * glyph_size[0], 3 * glyph_size[1])
        cls.characters["O"] = (11 * glyph_size[0], 3 * glyph_size[1])
        cls.characters["P"] = (0 * glyph_size[0], 4 * glyph_size[1])
        cls.characters["Q"] = (1 * glyph_size[0], 4 * glyph_size[1])
        cls.characters["R"] = (2 * glyph_size[0], 4 * glyph_size[1])
        cls.characters["S"] = (3 * glyph_size[0], 4 * glyph_size[1])
        cls.characters["T"] = (4 * glyph_size[0], 4 * glyph_size[1])
        cls.characters["U"] = (5 * glyph_size[0], 4 * glyph_size[1])
        cls.characters["V"] = (6 * glyph_size[0], 4 * glyph_size[1])
        cls.characters["W"] = (7 * glyph_size[0], 4 * glyph_size[1])
        cls.characters["X"] = (8 * glyph_size[0], 4 * glyph_size[1])
        cls.characters["Y"] = (9 * glyph_size[0], 4 * glyph_size[1])
        cls.characters["Z"] = (10 * glyph_size[0], 4 * glyph_size[1])

        # numbers
        cls.characters["0"] = (4 * glyph_size[0], 1 * glyph_size[1])
        cls.characters["1"] = (5 * glyph_size[0], 1 * glyph_size[1])
        cls.characters["2"] = (6 * glyph_size[0], 1 * glyph_size[1])
        cls.characters["3"] = (7 * glyph_size[0], 1 * glyph_size[1])
        cls.characters["4"] = (8 * glyph_size[0], 1 * glyph_size[1])
        cls.characters["5"] = (9 * glyph_size[0], 1 * glyph_size[1])
        cls.characters["6"] = (10 * glyph_size[0], 1 * glyph_size[1])
        cls.characters["7"] = (11 * glyph_size[0], 1 * glyph_size[1])
        cls.characters["8"] = (0 * glyph_size[0], 2 * glyph_size[1])
        cls.characters["9"] = (1 * glyph_size[0], 2 * glyph_size[1])
    
    @classmethod
    def draw(cls, text: str, position: Tuple, surface: pygame.Surface) -> None:
        x_position = 0
        for char_ in text:
            if not char_.isspace():
                surface.blit(cls.sheet, (position[0] + x_position, position[1]), pygame.Rect(cls.characters[char_.upper()], (cls.glyph_size[0], cls.glyph_size[1])))
            x_position += cls.glyph_size[0]

"""
    TODO description
"""
class Controller:

    keys_pressed = {}

    @classmethod
    def key_down(cls, key: int) -> bool:
        prior_pressed_state = False if key not in cls.keys_pressed else cls.keys_pressed[key]
        cls.keys_pressed[key] = pygame.key.get_pressed()[key]

        return cls.keys_pressed[key] and not prior_pressed_state

    @classmethod
    def key_pressed(cls, key: int) -> bool:
        return pygame.key.get_pressed()[key]