import pygame

pygame.font.init()
fonts = {
    'medieval': 'medieval.ttf',
    'minecraft': 'Minecraft Evenings.otf',
    '3dpixel': '3D-Pixel.ttf',
    '8bit': '8bitlim.ttf',
    '8bito': '8blimro.ttf',
    'arcade': 'ARCADECLASSIC.ttf',
    'modern_game': 'astron boy video.otf',
    'modern': 'astron boy.otf',
    'wonder': 'Beyond Wonderland.ttf',
    'curved': 'Digitag.ttf',
    'simple': 'DisposableDroidBB.ttf',
    'rounded': 'dpcomic.ttf',
    'playfull': 'Endalian Script.ttf',
    'blocky': 'FREAKSOFNATURE.ttf',
    'catchy': 'Future TimeSplitters.otf',
    'simple_wide': 'Halo3.ttf',
    'simple_fat': 'INVASION2000.ttf',
    'very_gamy': 'ka1.ttf',
    'simple_round': 'Karma Suture.otf',
    'mono': 'manaspc.ttf',
    'damaged': 'Merchant Copy.ttf',
    'big_natural': 'MorialCitadel.TTF',
    'spacy': 'nasalization-rg.otf',
    'sci-fi': 'neuropol.otf',
    'hollow_big_edge': 'papercut.ttf',
    'space_shuttle': 'pdark.ttf',
    'thin': 'PixelFJVerdana12pt.ttf',
    'random': 'Seattle Avenue.ttf',
    'pixel': 'yoster.ttf'
}

class Button():
    def __init__(self, x, y, width, height, font, font_size, buttonText='Button', onclickFunction=None, onePress=False):
        self.font = pygame.font.Font(f'fonts/{fonts[font]}', font_size)
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.onclickFunction = onclickFunction
        self.onePress = onePress
        self.alreadyPressed = False

        with open('art/images/textbox.png', 'r') as tb:
            self.box = pygame.image.load(tb)
            self.box = pygame.transform.scale(self.box, (width, height))


        self.buttonRect = pygame.Rect(self.x, self.y, self.width, self.height)

        self.buttonSurf = self.font.render(buttonText, True, '#baab80')

    def process(self, screen, clock, running, background, isblack, WIDTH, HEIGHT):
        mousePos = pygame.mouse.get_pos()
        if self.buttonRect.collidepoint(mousePos):
            if pygame.mouse.get_pressed(num_buttons=3)[0]:
                if self.onePress:
                    self.onclickFunction()
                elif not self.alreadyPressed:
                    if 'play' in str(self.onclickFunction):
                        self.onclickFunction(screen, clock, running, background, isblack, WIDTH, HEIGHT)
                        self.alreadyPressed = True
                    else:
                        self.onclickFunction()
                        self.alreadyPressed = True
            else:
                self.alreadyPressed = False
        self.box.blit(self.buttonSurf, [
            self.buttonRect.width/2 - self.buttonSurf.get_rect().width/2,
            self.buttonRect.height/2 - self.buttonSurf.get_rect().height/2
        ])
        screen.blit(self.box, self.buttonRect)


class DropDown():
    def __init__(self, x, y, width, height, font, font_size, color_menu, color_option, main, options):
        self.rect = pygame.Rect(x, y, width, height)
        self.font = pygame.font.Font(f'fonts/{fonts[font]}', font_size)
        self.main = main
        self.options = options
        self.draw_menu = False
        self.menu_active = False
        self.active_option = -1

        with open('art/images/textbox.png', 'r') as tb:
            self.box = pygame.image.load(tb)
            self.box = pygame.transform.scale(self.box, (width, height))

    def draw(self, screen):
        #pygame.draw.rect(screen, self.color_menu[self.menu_active], self.rect, 0)
        surface = self.font.render(self.main, 1, (0, 0, 0))
        self.box.blit(surface, [
            self.rect.width/2 - surface.get_rect().width/2,
            self.rect.height/2 - surface.get_rect().height/2
        ])
        screen.blit(self.box, surface.get_rect(center = self.rect.center))

        if self.draw_menu:
            for i, text in enumerate(self.options):
                rect = self.rect.copy()
                rect.y += (i+1) * self.rect.height
                rect.x = self.rect.x
                #pygame.draw.rect(screen, self.color_option[1 if i == self.active_option else 0], rect, 0)
                #msg = self.font.render(text, 1, (0, 0, 0))
                #screen.blit(msg, msg.get_rect(center = rect.center))
                surface = self.font.render(text, 1, (0, 0, 0))
                self.box.blit(surface, [
                    rect.width/2 - surface.get_rect().width/2,
                    rect.height/2 - surface.get_rect().height/2
                ])
                screen.blit(self.box, rect)

    def update(self, event_list):
        mpos = pygame.mouse.get_pos()
        self.menu_active = self.rect.collidepoint(mpos)
        self.active_option = -1
        for i in range(len(self.options)):
            rect = self.rect.copy()
            rect.y += (i+1) * self.rect.height
            if rect.collidepoint(mpos):
                self.active_option = i
                break

        if not self.menu_active and self.active_option == -1:
            self.draw_menu = False
        #self.draw_menu = True
        #return -1
        if pygame.mouse.get_pressed(num_buttons=3)[0]:
            if self.menu_active:
                self.draw_menu = not self.draw_menu
            elif self.draw_menu and self.active_option >= 0:
                self.draw_menu = False
                return self.active_option
        return -1


class Scene():
    def __init__(self, _type, bg, objects) -> None:
        self.name
        self.type = _type
        if self.type != 'cutscene':
            self.background = bg
            self.objects = objects
