#!./bin/python3

import os

import yaml
import pygame # type: ignore
from typing import Dict
import random
import tempfile

# Color constants

BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)

class Display:
    '''
    Display object which displays a list of strings. It can optionally highlight
    a string in the list and display a picture.
    '''

    def __init__(self, config, pydsp):

        # Set config values

        self._dx = config.get("dimx", 128) # Drawing dimensions
        self._dy = config.get("dimy", 160)
        self._pydsp = pydsp # Pygame display object
        self._fontsize = config.get("fontsize", 12) # Fontsize to use

        self.drawlines = int(self._dy / 12) - 5 # Max number of drawn lines


    def update(self, text=None, highlight=-1, title=None, img=None):
        '''
        Update the display.

        text      - List of max self.drawlines items to display as list.
        highlight - Text item to highlight.
        title     - List of max 4 items for the title.
        img       - Image to display in upper left corner.
        '''
        self._pydsp.fill((0,0,0))

        font = pygame.font.Font('freesansbold.ttf', self._fontsize)

        if not img:
            img = os.path.join("img", "c64tetris.png")

        i = pygame.transform.scale(pygame.image.load(img), (50, 50))
        self._pydsp.blit(i, (0,0), (0, 0, self._dx, 50))

        if not title:
            title = ["", "Hacker", "       Radio"]

        for i, item in enumerate(title):
            rendertext = font.render(item, True, WHITE, BLACK)
            self._pydsp.blit(rendertext, (55, 2 + i*12))
            if i == 3:
                break

        if not text:
            text = []

        for i, item in enumerate(text):
            if i == highlight:
                rendertext = font.render(item, True, BLACK, GREEN)
            else:
                rendertext = font.render(item, True, GREEN, BLACK)

            self._pydsp.blit(rendertext, (0, i * self._fontsize+50))

            if i == self.drawlines:
                break
