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

[((227, 254), 'pygame.font.SysFont', 'font.SysFont', (['"""Verdana"""', '(16)'], {}), "('Verdana', 16)\n", (239, 254), False, 'from pygame import Surface, font\n'), ((384, 412), 'frontend.Renderer.add_widget', 'Renderer.add_widget', (['self', '(1)'], {}), '(self, 1)\n', (403, 412), False, 'from frontend import Renderer, WidgetHandler\n'), ((421, 454), 'frontend.WidgetHandler.add_widget', 'WidgetHandler.add_widget', (['self', '(1)'], {}), '(self, 1)\n', (445, 454), False, 'from frontend import Renderer, WidgetHandler\n'), ((563, 586), 'pygame.Surface', 'Surface', (['(w + 4, h + 2)'], {}), '((w + 4, h + 2))\n', (570, 586), False, 'from pygame import Surface, font\n')]