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, image, font, font_size, buttonText='', onclickFunction=None, onePress=False, attributes=None):
        self.font = pygame.font.Font(f'fonts/{fonts[font]}', font_size)
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.attributes = attributes
        self.onclickFunction = onclickFunction
        self.onePress = onePress
        self.alreadyPressed = False

        with open(f'art/images/box/{image}', '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 update(self, screen):
        mousePos = pygame.mouse.get_pos()
        if self.buttonRect.collidepoint(mousePos):
            if pygame.mouse.get_pressed(3)[0]:
                if self.onePress:
                    self.onclickFunction()
                elif not self.alreadyPressed:
                    if self.attributes:
                        self.onclickFunction(*self.attributes)
                        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 Label():
    def __init__(self, x, y, width, height, text, font='simple', font_size=20, font_color = '#1e90ff', sprite = 'label.png') -> None:
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.font = pygame.font.Font(f'fonts/{fonts[font]}', font_size)
        self.font_color = font_color
        self.text = text
        self.hidden = False
        with open(f'art/images/box/{sprite}', 'r') as tb:
            self.box = pygame.image.load(tb)
            self.box = pygame.transform.scale(self.box, (width, height))
        

    def draw(self, screen):
        if self.hidden:
            return
        self.labelRect = pygame.Rect(self.x, self.y, self.width, self.height)
        self.labelSurf = self.font.render(self.text, True, self.font_color)
        self.box.blit(self.labelSurf, [
            self.labelRect.width / 2 - self.labelSurf.get_rect().width / 2,
            self.labelRect.height / 2 - self.labelSurf.get_rect().height / 2
        ])
        screen.blit(self.box, self.labelRect)



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/box/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:
            if WIDTH != None and HEIGHT != None:
                with open(bg, 'r') as bg:
                    self.background = pygame.transform.scale(pygame.image.load(bg), [WIDTH, HEIGHT])
            else:
                with open(bg, 'r') as bg:
                    self.background = pygame.transform.scale2x(pygame.image.load(bg)) 
        self.objects = objects

    def update(self, objects):
        return
    def draw(self, screen):
        return

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

    def update(self, change:bool=False, objects=None):
        if change:
            self.current_level += 1
        self.level[self.current_level].update(objects)
        self.background = self.level[self.current_level].background
        """if isinstance(self.objects, list):
            for obj in self.objects[0] + self.objects[1] + self.objects[2]:
                obj.update()"""
        
    def draw(self, screen):
        if isinstance(self.objects, list):
            for obj in self.objects[0] + self.objects[1] + self.objects[2] + self.objects[3]:
                obj.draw(screen)
        self.level[self.current_level].draw(screen)

    def getObjects(self):
        return self.level[self.current_level].getObjects()
        

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, objects):
        for room in self.rooms:
            if room.id == self.current:
                room.update(objects)
                self.background = room.background
        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)

    def getObjects(self):
        for room in self.rooms:
            if room.id == self.current:
                return room.getObjects()

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[4].append(wall) for wall in self.genWalls(WIDTH, HEIGHT)]
    
    def genWalls(self, WIDTH, HEIGHT):
        walls = []
        walls.append(Obstacle('wall_l', 'wall', None, True, 32, 32, True, WIDTH=4, HEIGHT=HEIGHT))
        walls.append(Obstacle('wall_r', 'wall', None, True, WIDTH + 28, 32, True, WIDTH=4, HEIGHT=HEIGHT))
        walls.append(Obstacle('wall_t', 'wall', None, True, 32, 32, True, WIDTH=WIDTH, HEIGHT=4))
        walls.append(Obstacle('wall_b', 'wall', None, True, 32, HEIGHT + 28, True, WIDTH=WIDTH, HEIGHT=4))
        return walls

    def update(self, objects):
        if objects is not None:
            self.objects = objects
        if not self.objects[1]:
            self.locked = False
        return
    
    def draw(self, screen):
        screen.blit(self.background, (32, 32))
        if isinstance(self.objects, list):
            for obj in self.objects[3] + self.objects[0] + self.objects[1] + self.objects[2]:
                obj.draw(screen)

    def getObjects(self):
        return self.objects

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.hidden = hidden
        self.width = WIDTH
        self.height = HEIGHT
        if self.background is not None:
            self.rect = pygame.Rect((x, y), self.background.get_size())
        else:
            self.rect = pygame.Rect(x, y, WIDTH, HEIGHT)
    
    def draw(self, screen):
        if not self.hidden:
            screen.blit(self.background, self.rect)
        else:
            pygame.draw.rect(screen, '#e7f8e0', self.rect, 2)



