from pygame import Surface, font
from .basewidget import BaseWidget
from frontend import Renderer, WidgetHandler


class Button(BaseWidget):
    action = None

    def __init__(self, x, y, texto, action=None):
        self.f = font.SysFont('Verdana', 16)
        imagen = self.crear(texto)
        rect = imagen.get_rect(topleft=(x, y))
        super().__init__(imagen, rect)
        Renderer.add_widget(self, 1)
        WidgetHandler.add_widget(self, 1)
        self.action = action

    def crear(self, texto):
        w, h = self.f.size(texto)
        image = Surface((w + 4, h + 2))
        image.fill((125, 125, 125), (1, 1, w+2, h))
        render = self.f.render(texto, 1, (255, 255, 255), (125, 125, 125))
        image.blit(render, (2, 1))
        return image

    def on_mousebuttondown(self, button):
        if button == 1 and self.action is not None:
            self.action()

    def on_mouseover(self):
        pass

    def update(self):
        self.dirty = 1

[((10, 17, 10, 44), 'pygame.font.SysFont', 'font.SysFont', ({(10, 30, 10, 39): '"""Verdana"""', (10, 41, 10, 43): '16'}, {}), "('Verdana', 16)", False, 'from pygame import Surface, font\n'), ((14, 8, 14, 36), 'frontend.Renderer.add_widget', 'Renderer.add_widget', ({(14, 28, 14, 32): 'self', (14, 34, 14, 35): '(1)'}, {}), '(self, 1)', False, 'from frontend import Renderer, WidgetHandler\n'), ((15, 8, 15, 41), 'frontend.WidgetHandler.add_widget', 'WidgetHandler.add_widget', ({(15, 33, 15, 37): 'self', (15, 39, 15, 40): '(1)'}, {}), '(self, 1)', False, 'from frontend import Renderer, WidgetHandler\n'), ((20, 16, 20, 39), 'pygame.Surface', 'Surface', ({(20, 24, 20, 38): '(w + 4, h + 2)'}, {}), '((w + 4, h + 2))', False, 'from pygame import Surface, font\n')]