import pygame
import time
import re
import sys

# ------------------------------
# CONFIG
# ------------------------------
OGG_FILE = "../spotify/Golden.ogg"
LRC_FILE = "Golden.lrc.HR"
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 200
FONT_SIZE = 48
BACKGROUND_COLOR = (0, 0, 0)
TEXT_COLOR = (255, 255, 255)
HIGHLIGHT_COLOR = (255, 255, 0)

# ------------------------------
# FUNCTIONS
# ------------------------------
def load_lrc_words(lrc_file):
    """
    Loads LRC with start|end times (no spaces) and returns list of tuples:
    (start_time_seconds, end_time_seconds, word)
    """
    words = []
    pattern = re.compile(r"\[(\d+):(\d+\.\d+)\|(\d+):(\d+\.\d+)\](.+)")
    with open(lrc_file, "r", encoding="utf-8") as f:
        for line in f:
            match = pattern.match(line.strip())
            if match:
                start_minutes = int(match.group(1))
                start_seconds = float(match.group(2))
                end_minutes = int(match.group(3))
                end_seconds = float(match.group(4))
                start_time = start_minutes * 60 + start_seconds
                end_time = end_minutes * 60 + end_seconds
                word = match.group(5).strip()
                if word:
                    words.append((start_time, end_time, word))
    return words

# ------------------------------
# INITIALIZE PYGAME
# ------------------------------
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Karaoke Player")
font = pygame.font.Font(None, FONT_SIZE)
clock = pygame.time.Clock()

pygame.mixer.init()
pygame.mixer.music.load(OGG_FILE)

words = load_lrc_words(LRC_FILE)
if not words:
    print("No words found in LRC!")
    sys.exit(1)

# ------------------------------
# PLAY AUDIO AND SYNC LYRICS
# ------------------------------
pygame.mixer.music.play()
start_time = time.time()
word_index = 0

running = True
while running:
    current_time = pygame.mixer.music.get_pos() / 1000.0

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            pygame.mixer.music.stop()

    screen.fill(BACKGROUND_COLOR)

    # Find current word based on time
    current_word = None
    for idx, (word_start, word_end, word_text) in enumerate(words):
        if word_start <= current_time < word_end:
            current_word = (word_start, word_end, word_text)
            word_index = idx  # sync index for next loop
            break

    if current_word:
        word_start, word_end, word_text = current_word
        progress = (current_time - word_start) / (word_end - word_start)
        progress = max(0, min(1, progress))

        bg_rect_width = int(SCREEN_WIDTH * progress)
        pygame.draw.rect(screen, HIGHLIGHT_COLOR, (0, 0, bg_rect_width, SCREEN_HEIGHT))

        text_surface = font.render(word_text, True, TEXT_COLOR)
        text_rect = text_surface.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
        screen.blit(text_surface, text_rect)

    pygame.display.flip()
    clock.tick(60)


pygame.quit()
