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 GameObjects():
    def __init__(self, name:str, _type:str, bg, objects:list, WIDTH, HEIGHT) -> None:
        self.name = name
        self.type = _type
        self.background = bg
        if bg != None:
            with open(bg, 'r') as bg:
                self.background = pygame.transform.scale(pygame.image.load(bg), [WIDTH, HEIGHT])
        self.objects = objects


class Scene(GameObjects):
    def __init__(self, name:str, _type:str, bg, objects:list | None, WIDTH, HEIGHT, level:list) -> None:
        super().__init__(name, _type, bg, objects, WIDTH, HEIGHT)
        self.level = level
        self.current_level = 0

    def update(self, change:bool):
        if change:
            self.current_level += 1
        self.level[self.current_level].update()
        if isinstance(self.objects, list):
            for obj in self.objects:
                obj.update()
        
    def draw(self, screen):
        if isinstance(self.objects, list):
            for obj in self.objects:
                obj.draw(screen)
        self.level[self.current_level].draw(screen)
        

class Stage(GameObjects):
    def __init__(self, name: str, _type: str, bg, objects: list, WIDTH, HEIGHT, stage:str, rooms:list) -> None:
        super().__init__(name, _type, bg, objects, WIDTH, HEIGHT)
        self.stage = stage
        self.rooms = rooms
        self.current = 0
    
    def update(self):
        for room in self.rooms:
            if room.id == self.current:
                room.update()
        keys = pygame.key.get_pressed()
        if keys[pygame.K_RIGHT]:
            self.current += 1
        if self.current >= len(self.rooms):
            return 1

    def draw(self, screen):
        for room in self.rooms:
            if room.id == self.current:
                room.draw(screen)

class Room(GameObjects):
    def __init__(self, name:str, _type:str, bg, objects:list, WIDTH, HEIGHT, exits:list, id:int) -> None:
        super().__init__(name, _type, bg, objects, WIDTH, HEIGHT)
        self.exits = exits
        self.id = id
        if self.type == 'normal' or self.type == 'boss':
            self.locked = True
        else:
            self.locked = False

        self.objects.append(self.genWalls(WIDTH, HEIGHT))
        
    def genWalls(self, WIDTH, HEIGHT):
        walls = []
        walls.append(pygame.Rect(0, 0, 4, HEIGHT))
        walls.append(pygame.Rect(WIDTH - 4, 0, 4, HEIGHT))
        walls.append(pygame.Rect(0, 0, WIDTH, 4))
        walls.append(pygame.Rect(0, HEIGHT - 4, WIDTH, 4))
        return walls

    def update(self):
        pass

    def draw(self, screen):
        screen.blit(self.background, (32, 32))
        if isinstance(self.objects, list):
            for obj in self.objects[0]:
                obj.draw(screen)

class Obstacle(GameObjects):
    def __init__(self, name: str, _type: str, bg, collision: bool, x: int, y: int, hidden: bool=False, objects: list = None, WIDTH=None, HEIGHT=None) -> None:
        super().__init__(name, _type, bg, objects, WIDTH, HEIGHT)
        self.collision = collision
        self.rect = pygame.Rect((x, y), self.background.get_size())
    
    def draw(self, screen):
        screen.blit(self.background, self.rect)