import pygame
from player import Player
from enemy import Enemy 
from settings import SCREEN_HEIGHT, SCREEN_WIDTH

from pygame.locals import (
    RLEACCEL,
    K_UP,
    K_DOWN,
    K_LEFT,
    K_RIGHT,
    K_ESCAPE,
    KEYDOWN,
    QUIT,
    K_SPACE,
    K_f,
    K_RETURN,
)

pygame.mixer.init()
pygame.init()

pygame.font.init()
my_font = pygame.font.SysFont('Comic Sans MS', 30)

pygame.mixer.music.load("sounds/aura.mp3")

collision_sound = pygame.mixer.Sound("sounds/dead.ogg")

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

ADDENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(ADDENEMY, 1000)

running = True
game_started = False

# Initialize player, enemy, and sprite groups
player = Player()
enemy = Enemy()
enemies = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()

all_sprites.add(player)
all_sprites.add(enemy)
enemies.add(enemy)

bg1 = pygame.transform.scale(pygame.image.load("sprites/Day.city.png").convert(), (SCREEN_WIDTH, SCREEN_HEIGHT))

clock = pygame.time.Clock()

# Start screen loop
while not game_started:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_SPACE:
                game_started = True  # Start the game when Space is pressed
            if event.key == K_f:
                game_started = True  # Start the game when F is pressed (optional)
        elif event.type == QUIT:
            running = False
            game_started = True  # Exit the loop if quit event occurs

    screen.fill((0, 0, 0))
    text = my_font.render("Start", True, (255, 255, 255))
    screen.blit(text, (
        (SCREEN_WIDTH - text.get_width()) / 2,
        (SCREEN_HEIGHT - text.get_height()) / 2,
    ))
    text = my_font.render("Press Space to Start", True, (255, 255, 255))
    screen.blit(text, (
        (SCREEN_WIDTH - text.get_width()) / 2,
        (SCREEN_HEIGHT - text.get_height() - 200)
    ))
    
    pygame.display.flip()
    clock.tick(30)  # Limit FPS to 30

pygame.mixer.music.play(loops=-1, fade_ms=1000)


# Main game loop
while running:
    screen.fill((0, 0, 0))  # Background color
    screen.blit(bg1, (0, 0))  # Draw the background image
    

    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False  # Quit on Escape
            elif event.key == K_RETURN:  # Press Enter to restart
                # Reset the game state
                player.kill()  # Remove player from all_sprites
                enemy.kill()  # Remove enemy from all_sprites
                enemies.empty()  # Empty enemies group
                all_sprites.empty()  # Empty all_sprites group

                # Reinitialize player, enemies, and all sprites
                player = Player()
                enemy = Enemy()
                enemies.add(enemy)
                all_sprites.add(player)
                all_sprites.add(enemy)

                # Reset background and re-enter game loop
                continue  # Restart game loop from the beginning

        elif event.type == QUIT:
            running = False  # Quit on window close

        elif event.type == ADDENEMY:
            # Add a new enemy each second
            new_enemy = Enemy()
            enemies.add(new_enemy)
            all_sprites.add(new_enemy)

    pressed_keys = pygame.key.get_pressed()

    player.update(pressed_keys)
    enemies.update()

    # Render all sprites
    for entity in all_sprites:
        screen.blit(entity.surf, entity.rect)

    # Check for collisions between player and enemies
    if pygame.sprite.spritecollideany(player, enemies):
        player.kill()  # Remove player when it collides with an enemy
        pygame.time.delay(250)  # Wait a bit before showing the game over screen

        pygame.mixer.stop

        collision_sound.play()
        # Game Over screen after collision
        screen.fill((0, 0, 0))  # Clear the screen with a black background
        
        text = my_font.render("Game Over", True, (255, 0, 0))
        screen.blit(text, (
            (SCREEN_WIDTH - text.get_width()) / 2,
            (SCREEN_HEIGHT - text.get_height()) / 2
        ))

        text = my_font.render("Press Enter to Restart", True, (255, 255, 255))
        screen.blit(text, (
            (SCREEN_WIDTH - text.get_width()) / 2,
            (SCREEN_HEIGHT - text.get_height()) / 2 + 50,
        ))

        text = my_font.render("Press Escape to Quit", True, (255, 255, 255))
        screen.blit(text, (
            (SCREEN_WIDTH - text.get_width()) / 2,
            (SCREEN_HEIGHT - text.get_height()) / 2 +100,
        ))

        pygame.display.flip()

        # Wait for the player to press Enter to restart or Escape to quit
        waiting_for_input = True
        while waiting_for_input:
            for event in pygame.event.get():
                if event.type == KEYDOWN:
                    if event.key == K_RETURN:
                        waiting_for_input = False  # Player pressed Enter to restart
                    if event.key == K_ESCAPE:
                        running = False  # Player pressed Escape to quit
                        waiting_for_input = False
                elif event.type == QUIT:
                    waiting_for_input = False
                    running = False

    pygame.display.flip()  # Update the screen

    clock.tick(60)  # Limit FPS to 60

pygame.mixer.music.stop()
pygame.mixer.quit()
pygame.quit()
