import pygame
from settings import MyColors
import settings

class Menu:
    def __init__(self, screen):
        self.screen = screen
        self.font = pygame.font.Font(settings.fonts.PressStart2P, 24)
        self.input_font = pygame.font.Font(settings.fonts.PressStart2P, 32)

        # Titelbild laden
        self.title_image = pygame.image.load('../assets/images/sign.png').convert_alpha()
        self.title_image = pygame.transform.scale(self.title_image, (600, 450))

        # Musik laden und abspielen
        pygame.mixer.music.load('../assets/sounds/menu.wav')
        pygame.mixer.music.set_volume(settings.VOLUME)
        pygame.mixer.music.play(-1)

        # Button-Rects
        self.start_button = pygame.Rect(406.7, 550, 200, 50)
        self.highscore_button = pygame.Rect(673.4, 550, 200, 50)
        self.back_button = pygame.Rect(390, 630, 200, 50)
        self.go_button = pygame.Rect(690, 630, 200, 50)

        self.input_active = False
        self.player_name = ""
        self.show_main_buttons = True

    def draw(self):
        self.screen.fill(MyColors.background)  # Hintergrundfarbe

        # Titelbild
        self.screen.blit(self.title_image, (340, 50))

        # Buttons nur im Hauptmenü anzeigen
        if self.show_main_buttons:
            pygame.draw.rect(self.screen, MyColors.yellow, self.start_button)
            pygame.draw.rect(self.screen, MyColors.yellow, self.highscore_button)

            start_text = self.font.render("Start", True, MyColors.brown)
            highscore_text = self.font.render("Punkte", True, MyColors.brown)

            self.screen.blit(start_text, (self.start_button.x + 40, self.start_button.y + 10))
            self.screen.blit(highscore_text, (self.highscore_button.x + 30, self.highscore_button.y + 10))

        # Eingabefeld und Buttons nur bei aktiver Eingabe anzeigen
        if self.input_active:
            input_box = pygame.Rect(390, 550, 500, 50)
            pygame.draw.rect(self.screen, MyColors.brown, input_box, 5)
            name_surface = self.input_font.render(self.player_name, True, MyColors.yellow)
            self.screen.blit(name_surface, (input_box.x + 10, input_box.y + 10))

            # Buttons
            pygame.draw.rect(self.screen, MyColors.yellow, self.back_button)
            pygame.draw.rect(self.screen, MyColors.yellow, self.go_button)

            back_text = self.font.render("Zurück", True, MyColors.brown)
            go_text = self.font.render("Los!", True, MyColors.brown)

            self.screen.blit(back_text, (self.back_button.x + 30, self.back_button.y + 10))
            self.screen.blit(go_text, (self.go_button.x + 55, self.go_button.y + 10))



    def handle_event(self, event):
        if event.type == pygame.MOUSEBUTTONDOWN:
            if self.show_main_buttons:
                if self.start_button.collidepoint(event.pos):
                    self.input_active = True
                    self.show_main_buttons = False
                if self.highscore_button.collidepoint(event.pos) and settings.DEBUG:
                    print("Highscores anzeigen (später!)")
            if self.input_active:
                if self.back_button.collidepoint(event.pos):
                    self.input_active = False
                    self.show_main_buttons = True
                    self.player_name = ""
                if self.go_button.collidepoint(event.pos):
                    if settings.DEBUG:
                        print(f"Spielstart mit Spielername: {self.player_name}")

        if event.type == pygame.KEYDOWN and self.input_active:
            if event.key == pygame.K_RETURN and settings.DEBUG:
                print(f"Name eingegeben: {self.player_name}")
            elif event.key == pygame.K_BACKSPACE:
                self.player_name = self.player_name[:-1]
            else:
                if len(self.player_name) < settings.MAX_LENGHT and event.unicode.isalpha():
                    self.player_name += event.unicode.upper()