extends Control

@export var photo_scene: PackedScene

@onready var grid_container: GridContainer = $%PhotoGrid

func _ready() -> void:
	CollectionManager.on_photo_saved.connect(_create_photo_thumbnail)
	load_gallery()

func load_gallery() -> void:
	for child in grid_container.get_children():
		child.queue_free()
		
	var saved_photos = CollectionManager.saved_photos
	
	for file_path in saved_photos:
		if FileAccess.file_exists(file_path):
			_create_photo_thumbnail(file_path)

func _create_photo_thumbnail(file_path: String) -> void:
	var image = Image.load_from_file(file_path)
	if image:
		var photo: Photo = photo_scene.instantiate()
		var texture = ImageTexture.create_from_image(image)
		photo.setup(texture)
		grid_container.add_child(photo)
		photo.custom_minimum_size = Vector2(132,154)
