import os
import pygame
from pygame.locals import *
# from PIL import Image
import threading
# import exifread

# Chemin du dossier contenant les photos
PHOTO_FOLDER = "./photos"
# Temps affichage photos (1000 = 1 seconde)
TIMER = 5000


# Liste des fichiers dans le dossier
def get_photo_files():
    return [os.path.join(PHOTO_FOLDER, f) for f in os.listdir(PHOTO_FOLDER) if
            os.path.isfile(os.path.join(PHOTO_FOLDER, f))]


photo_files = get_photo_files()

# Initialisation de Pygame
pygame.init()

# Récupération de la résolution de l'écran
info = pygame.display.Info()
max_width = info.current_w
max_height = info.current_h

# Création de la fenêtre
screen = pygame.display.set_mode((max_width, max_height), FULLSCREEN)

# Initialisation de la variable fullscreen
fullscreen = False

# Boucle principale
running = True
photo_index = 0
clock = pygame.time.Clock()
auto_change = True
auto_change_timer = 0
pause = False  # Initialisation de la variable pause


# Chargement des photos
def load_photos():
    global photo_files
    global photo_index
    while True:
        if not auto_change:
            continue
        next_index = (photo_index + 1) % len(photo_files)
        pygame.image.load(photo_files[next_index])
        pygame.time.wait(10)


threading.Thread(target=load_photos, daemon=True).start()

# Création du logo de pause
pause_logo = pygame.image.load("pause_logo.png")
pause_logo_rect = pause_logo.get_rect(center=screen.get_rect().center)
play_logo = pygame.image.load("play_logo.png")
play_logo_rect = play_logo.get_rect(center=screen.get_rect().center)

while running:
    # Gestion des événements
    for event in pygame.event.get():
        if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
            running = False
        elif event.type == KEYDOWN and event.key == K_SPACE:
            auto_change = not auto_change
            if not auto_change:
                screen.blit(pause_logo, pause_logo_rect)
                pygame.display.flip()
            else:
                screen.blit(play_logo, play_logo_rect)
                pygame.display.flip()
        elif event.type == KEYDOWN and event.key == K_LEFT:
            photo_index = (photo_index - 1) % len(photo_files)
        elif event.type == KEYDOWN and event.key == K_RIGHT:
            photo_index = (photo_index + 1) % len(photo_files)
        elif event.type == KEYDOWN and event.key == K_f:
            fullscreen = not fullscreen
            if fullscreen:
                screen = pygame.display.set_mode((0, 0), FULLSCREEN)
            else:
                screen = pygame.display.set_mode((800, 600), RESIZABLE)
        elif event.type == KEYDOWN and event.key == K_r:
            photo_files = get_photo_files()
            photo_index = 0

    # Changement automatique de photo
    if auto_change:
        if pygame.time.get_ticks() - auto_change_timer > TIMER:
            photo_index = (photo_index + 1) % len(photo_files)
            auto_change_timer = pygame.time.get_ticks()

    # Affichage de la photo courante
    photo = pygame.image.load(photo_files[photo_index])
    photo_rect = photo.get_rect()

    # Calcul de l'échelle nécessaire pour adapter la photo à la fenêtre
    scale = min(float(screen.get_width()) / photo_rect.width, float(screen.get_height()) / photo_rect.height)

    # Redimensionnement de la photo en conservant son rapport d'aspect
    new_width = int(photo_rect.width * scale)
    new_height = int(photo_rect.height * scale)
    photo = pygame.transform.smoothscale(photo, (new_width, new_height))

    # Positionnement de la photo au centre de la fenêtre
    photo_rect = photo.get_rect(center=screen.get_rect().center)

    # Affichage de la photo
    screen.fill((0, 0, 0))
    screen.blit(photo, photo_rect)

    pygame.display.flip()

    # Attente avant de passer à la photo suivante
    # clock.tick(60) # 60 images par seconde

# Fermeture de Pygame
pygame.quit()
