extends GridContainer

var target_buttons = []  # Array to hold target buttons
# Called when the node enters the scene tree for the first time.

func _ready():
	print("Team Selection loaded")
	MemberDatabase.load_character_data()
	load_character_buttons()
	#pass
	# Initialize or find the target buttons using their absolute paths
	target_buttons = [
		get_node("/root/Root/UI/AspectRatioContainer/TeamScreen/Member1"),  # Replace with the actual path to your target button 1
		get_node("/root/Root/UI/AspectRatioContainer/TeamScreen/Member2"),  # Replace with the actual path to your target button 2
		get_node("/root/Root/UI/AspectRatioContainer/TeamScreen/Member3"),  # Replace with the actual path to your target button 3
		get_node("/root/Root/UI/AspectRatioContainer/TeamScreen/Member4"),  # Replace with the actual path to your target button 4
		get_node("/root/Root/UI/AspectRatioContainer/TeamScreen/Member5")   # Replace with the actual path to your target button 5
	]
	
func load_character_buttons():
	#print("started load function")
	#print(MemberDatabase.character_data)
	if "members" in MemberDatabase.character_data:
		#print("found members in data")
		for character in MemberDatabase.character_data["members"]:
			#print("iterating through data")
			if character["isObtained"] ==true:
				print ("Found character:", character["name"])
				var button = create_character_button(character)
				add_child(button)
	else:
		print("No characters found in character data.")

# Function to create a button for a character
# This builds out the character inventory
func create_character_button(member: Dictionary) -> TextureButton:
	var button = TextureButton.new()
	#Loads the texture specified in the memberData sheet
	if FileAccess.file_exists(member["texturePath"]):
		button.texture_normal = load(member["texturePath"])
	else:
		button.texture_normal = preload("res://Images/Members/Soldier.png") # Use a default texture if none is specified
	button.connect("pressed", Callable(self, "_on_button_pressed").bind(button, member))
	button.connect("mouse_entered", Callable(self, "_on_button_hovered").bind(button, member))
	button.connect("mouse_exited", Callable(self, "_on_button_exited").bind(button))
	return button


# Function called when a button is hovered over
func _on_button_hovered(button: TextureButton, member: Dictionary):
	#current_member = member
	#The backslash  \ allows you to continue to the next line while concatenating the string
	var tooltip_text = "" + str(member["name"]) + "\n" + "" + str(member["description"]) + "\n" + "Damage Per Second: " + str(member["perSec"]) + "\n" + \
	"Damage Per Click: " + str(member["clickAdd"]) + "\n" + "Damage Multiplier: " + str(member["scoreMultiplier"])
	button.tooltip_text = tooltip_text

# Function called when the mouse exits the button
func _on_button_exited(button: TextureButton):
	button.tooltip_text = ""


# Update the global singleton for the given index
func update_global_texture_singleton(texture: Texture):
	match Global.currentMemberSelected:
		0:
			Global.set_texture1(texture)
		1:
			Global.set_texture2(texture)
		2:
			Global.set_texture3(texture)
		3:
			Global.set_texture4(texture)
		4:
			Global.set_texture5(texture)


## Handler function for the pressed signal
func _on_button_pressed(button: TextureButton, member: Dictionary):
	## Print the name of the button
	##print("Button pressed: ", button.name)
	
	var selected_texture = button.texture_normal
	#var texture = button.texture_normal
	# Logic to determine which target button to update (this can be based on user selection or some other logic)
	# For this example, we'll update the first available slot
	update_global_texture_singleton(selected_texture)


	#Adds the member stats into the global singletons
	Global.set_damage_per_second(member["perSec"])
	Global.set_damage_per_click(member["clickAdd"])
	Global.set_damage_multiplier(member["scoreMultiplier"])
	
	##For returning to the Team Selection Screen after selecting member
	##Sets the global variable for the team selection screen to true so when you move back it's already open
	Global.teamScreenVisible = true
	Global.teamSelectionVisible = false
	
	get_tree().change_scene_to_file("res://team.tscn")

	


