extends Node

## Manages the visibility of animal friends in the animal_friends group
## Animals start invisible and are revealed one at a time randomly as ranks are unlocked

const ANIMAL_FRIENDS_UNLOCK_ID: int = 6  # Forest Friends unlock

var all_animals: Array[Node] = []
var hidden_animals: Array[Node] = []
var revealed_animals: Array[Node] = []
var last_known_rank: int = 0

func _ready() -> void:
	# Wait a frame to ensure all nodes are ready
	call_deferred("_initialize")

func _initialize() -> void:
	# Get all nodes in the animal_friends group
	all_animals = get_tree().get_nodes_in_group("animal_friends")

	if all_animals.is_empty():
		Log.pr("Warning: No animal friends found in the animal_friends group")
		return

	# Shuffle the animals to randomize the order they'll be revealed
	all_animals.shuffle()

	# Start with all animals hidden
	for animal in all_animals:
		animal.visible = false
		hidden_animals.append(animal)

	Log.pr("Animal Friends Manager initialized with %d animals" % all_animals.size())

	# Get the Forest Friends unlock to track its rank
	var forest_friends = Unlocks.get_unlock_by_id(ANIMAL_FRIENDS_UNLOCK_ID)
	if forest_friends:
		last_known_rank = forest_friends.current_rank
		# Reveal animals based on current rank (for game loads)
		_reveal_animals_for_current_rank()
	else:
		Log.pr("Warning: Forest Friends unlock (ID %d) not found" % ANIMAL_FRIENDS_UNLOCK_ID)

	# Connect to unlock signal to detect when new ranks are purchased
	Unlocks.item_unlocked.connect(_on_item_unlocked)

func _reveal_animals_for_current_rank() -> void:
	# Reveal one animal per rank in the Forest Friends unlock
	var forest_friends = Unlocks.get_unlock_by_id(ANIMAL_FRIENDS_UNLOCK_ID)
	if not forest_friends:
		return

	var ranks_to_reveal = forest_friends.current_rank
	for i in range(ranks_to_reveal):
		if hidden_animals.is_empty():
			break
		_reveal_next_animal()

func _on_item_unlocked() -> void:
	# Check if the Forest Friends unlock was ranked up
	var forest_friends = Unlocks.get_unlock_by_id(ANIMAL_FRIENDS_UNLOCK_ID)
	if not forest_friends:
		return

	var current_rank = forest_friends.current_rank
	var new_ranks = current_rank - last_known_rank

	if new_ranks > 0:
		Log.pr("Forest Friends ranked up! Revealing %d animal friend(s)" % new_ranks)

		# Reveal one animal for each new rank
		for i in range(new_ranks):
			if hidden_animals.is_empty():
				Log.pr("All animal friends have been revealed!")
				break
			_reveal_next_animal()

		last_known_rank = current_rank

func _reveal_next_animal() -> void:
	if hidden_animals.is_empty():
		return

	# Take the first animal from the shuffled hidden list (random order)
	var animal = hidden_animals.pop_front()
	animal.visible = true
	revealed_animals.append(animal)

	Log.pr("Revealed animal friend: %s (%d/%d visible)" % [animal.name, revealed_animals.size(), all_animals.size()])

## Debug function to manually reveal the next animal
func debug_reveal_next() -> void:
	_reveal_next_animal()

## Debug function to hide all animals
func debug_reset_all() -> void:
	for animal in all_animals:
		animal.visible = false
	hidden_animals = all_animals.duplicate()
	revealed_animals.clear()
	Log.pr("All animals hidden")
