import pygame
import os
import subprocess
import sys
from sys import exit

# ---------------- INIT ----------------
os.chdir(os.path.dirname(os.path.abspath(__file__)))
pygame.init()

# ---------------- CONFIG ----------------
WIDTH, HEIGHT = 800, 600
FPS = 60

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("AFRIPLAY Game Launcher")
clock = pygame.time.Clock()
font = pygame.font.Font(None, 32)

# ---------------- GAME STATES ----------------
MENU = "menu"
ABOUT = "about"

game_state = MENU

# ---------------- GAMES (UNCHANGED LOGIC) ----------------
GAMES = {
    "Riddle Game": r"C:\Users\Koda\Desktop\Game launcher\Riddle game by nadriekoda\game ver 5.py",
    "Marble Game": r"C:\Users\Koda\Desktop\Game launcher\marbble game\marble.py",
    "Nyama Nyama": r"C:\Users\Koda\Desktop\Game launcher\Nyama Nyama Game\nyama_game.py"
}

# ---------------- BUTTON IMAGES ----------------
start_btn = pygame.transform.scale(
    pygame.image.load("assets/buttons/start.png").convert_alpha(), (220, 80)
)
start_rect = start_btn.get_rect(center=(400, 220))

marble_btn = pygame.transform.scale(
    pygame.image.load("assets/buttons/marble.png").convert_alpha(), (220, 80)
)
marble_rect = marble_btn.get_rect(center=(400, 310))

nyama_btn = pygame.transform.scale(
    pygame.image.load("assets/buttons/nyama.png").convert_alpha(), (220, 80)
)
nyama_rect = nyama_btn.get_rect(center=(400, 400))

about_btn = pygame.transform.scale(
    pygame.image.load("assets/buttons/about.png").convert_alpha(), (180, 60)
)
about_rect = about_btn.get_rect(topleft=(20, 20))

back_btn = pygame.transform.scale(
    pygame.image.load("assets/buttons/back.png").convert_alpha(), (120, 50)
)
back_rect = back_btn.get_rect(topleft=(20, 20))

# ---------------- BACKGROUNDS ----------------
menu_back = pygame.transform.scale(
    pygame.image.load("assets/backgrounds/menu.png"), (WIDTH, HEIGHT)
)

about_back = pygame.transform.scale(
    pygame.image.load("assets/backgrounds/about.png"), (WIDTH, HEIGHT)
)

# ---------------- GAME LAUNCHER (UNCHANGED) ----------------
def launch_game(path):
    if os.path.exists(path):
        subprocess.Popen([sys.executable, path])
    else:
        print("❌ Game not found:", path)

# ---------------- MAIN LOOP ----------------
running = True
while running:
    clock.tick(FPS)

    # -------- EVENTS --------
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse_pos = event.pos

            if game_state == MENU:
                if start_rect.collidepoint(mouse_pos):
                    launch_game(GAMES["Riddle Game"])

                elif marble_rect.collidepoint(mouse_pos):
                    launch_game(GAMES["Marble Game"])

                elif nyama_rect.collidepoint(mouse_pos):
                    launch_game(GAMES["Nyama Nyama"])

                elif about_rect.collidepoint(mouse_pos):
                    game_state = ABOUT

            elif game_state == ABOUT:
                if back_rect.collidepoint(mouse_pos):
                    game_state = MENU

    # -------- DRAW --------
    if game_state == MENU:
        screen.blit(menu_back, (0, 0))
        screen.blit(start_btn, start_rect)
        screen.blit(marble_btn, marble_rect)
        screen.blit(nyama_btn, nyama_rect)
        screen.blit(about_btn, about_rect)

    elif game_state == ABOUT:
        screen.blit(about_back, (0, 0))
        screen.blit(
            font.render("AFRIPLAY - African Game Hub", True, (0, 0, 0)),
            (220, 260),
        )
        screen.blit(
            font.render("Created by nadriekoda", True, (0, 0, 0)),
            (260, 300),
        )
        screen.blit(back_btn, back_rect)

    pygame.display.flip()

pygame.quit()
