import os
from PIL import Image
import pygame
import threading

# Chemin vers le dossier contenant les images
IMAGE_DIR = './photos'

# Initialisation de Pygame
pygame.init()
pygame.display.set_caption("Diaporama")
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)

# Créez une nouvelle surface pour dessiner les images et les logos
intermediate_surface = pygame.Surface(screen.get_size())

# Logos Play, Pause, Next, Previous
LOGO_SIZE = (200, 200)

pause_logo = pygame.image.load("./logos/pause.png")
pause_logo = pygame.transform.smoothscale(pause_logo, LOGO_SIZE)
pause_logo_rect = pause_logo.get_rect(center=screen.get_rect().center)

play_logo = pygame.image.load("./logos/play.png")
play_logo = pygame.transform.smoothscale(play_logo, LOGO_SIZE)
play_logo_rect = play_logo.get_rect(center=screen.get_rect().center)

next_logo = pygame.image.load("./logos/next.png")
next_logo = pygame.transform.smoothscale(next_logo, LOGO_SIZE)
next_logo_rect = next_logo.get_rect(midright=(screen.get_rect().right - 200, screen.get_rect().centery))

previous_logo = pygame.image.load("./logos/previous.png")
previous_logo = pygame.transform.smoothscale(previous_logo, LOGO_SIZE)
previous_logo_rect = previous_logo.get_rect(midleft=(screen.get_rect().left + 200, screen.get_rect().centery))

# Chargement des images dans une liste
images = []

# Fonction pour charger une image en utilisant un thread
def load_image(image_path):
    with Image.open(image_path) as img:
        img.thumbnail(screen.get_size())
        images.append(pygame.image.fromstring(img.tobytes(), img.size, "RGB"))

# Création des threads pour charger les images
image_threads = []
for file_name in os.listdir(IMAGE_DIR):
    if file_name.endswith('.jpg') or file_name.endswith('.png'):
        image_path = os.path.join(IMAGE_DIR, file_name)
        image_thread = threading.Thread(target=load_image, args=(image_path,))
        image_threads.append(image_thread)
        image_thread.start()

# Attente de la fin du chargement de toutes les images
for image_thread in image_threads:
    image_thread.join()

# Définition des zones d'affichage
screen_width, screen_height = screen.get_size()
left_rect = pygame.Rect(0, 0, screen_width // 3, screen_height)
center_rect = pygame.Rect(screen_width // 3, 0, screen_width // 3, screen_height)
right_rect = pygame.Rect(screen_width * 2 // 3, 0, screen_width // 3, screen_height)

# Variables pour le diaporama
current_image_index = 0
next_image_time = pygame.time.get_ticks() + 5000
is_paused = False

image = images[current_image_index]
image_rect = image.get_rect(center=screen.get_rect().center)

# Boucle principale
running = True
while running:
    # Gestion des événements Pygame
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False
            elif event.key == pygame.K_LEFT:
                current_image_index = (current_image_index - 1) % len(images)
                next_image_time = pygame.time.get_ticks() + 5000
                screen.blit(previous_logo, previous_logo_rect)
                pygame.display.flip()
                pygame.time.delay(1000)  # Pause de 1 seconde avant de masquer le logo
                screen.fill((0, 0, 0))
            elif event.key == pygame.K_RIGHT:
                screen.blit(next_logo, next_logo_rect)
                current_image_index = (current_image_index + 1) % len(images)
                pygame.display.flip()
                pygame.time.delay(1000)  # Pause de 1 seconde avant de masquer le logo
                screen.fill((0, 0, 0))
                next_image_time = pygame.time.get_ticks() + 5000
            elif event.key == pygame.K_SPACE:
                is_paused = not is_paused
                if is_paused:
                    screen.blit(pause_logo, pause_logo_rect)
                    pygame.display.flip()
                    pygame.time.delay(1000)  # Pause de 1 seconde avant de masquer le logo
                    screen.fill((0, 0, 0))
                else:
                    screen.blit(play_logo, play_logo_rect)
                    pygame.display.flip()
                    pygame.time.delay(1000)  # Pause de 1 seconde avant de masquer le logo
                    screen.fill((0, 0, 0))
        elif event.type == pygame.MOUSEBUTTONUP:
            pos = pygame.mouse.get_pos()
            if left_rect.collidepoint(pos):
                current_image_index = (current_image_index - 1) % len(images)
                next_image_time = pygame.time.get_ticks() + 5000
                screen.blit(previous_logo, previous_logo_rect)
                pygame.display.flip()
                pygame.time.delay(1000)  # Pause de 1 seconde avant de masquer le logo
                screen.fill((0, 0, 0))
                previous_image_time = pygame.time.get_ticks() + 5000
            elif center_rect.collidepoint(pos):
                is_paused = not is_paused
                if is_paused:
                    screen.blit(pause_logo, pause_logo_rect)
                    pygame.display.flip()
                    pygame.time.delay(1000)  # Pause de 1 seconde avant de masquer le logo
                    screen.fill((0, 0, 0))
                else:
                    screen.blit(play_logo, play_logo_rect)
                    pygame.display.flip()
                    pygame.time.delay(1000)  # Pause de 1 seconde avant de masquer le logo
                    screen.fill((0, 0, 0))
            elif right_rect.collidepoint(pos):
                screen.blit(next_logo, next_logo_rect)
                current_image_index = (current_image_index + 1) % len(images)
                pygame.display.flip()
                pygame.time.delay(1000)  # Pause de 1 seconde avant de masquer le logo
                screen.fill((0, 0, 0))
                next_image_time = pygame.time.get_ticks() + 5000

    image = images[current_image_index]
    image_rect = image.get_rect(center=screen.get_rect().center)

    # Dessinez l'image actuelle sur la surface intermédiaire
    intermediate_surface.fill((0, 0, 0))
    intermediate_surface.blit(image, image_rect)

    # Dessinez la surface intermédiaire sur l'écran
    screen.blit(intermediate_surface, (0, 0))
    pygame.display.flip()

    if not is_paused and pygame.time.get_ticks() >= next_image_time:
        current_image_index = (current_image_index + 1) % len(images)
        next_image_time = pygame.time.get_ticks() + 5000

# Fermeture de Pygame
pygame.quit()
