extends GridContainer

func _ready():
	load_boss_buttons()

func load_boss_buttons():
	if "bosses" in BossDatabase.boss_data:
		for boss in BossDatabase.boss_data["bosses"]:
			var button = create_boss_button(boss)
			add_child(button)
	else:
		print("No bosses found in boss data.")
		
		
func create_boss_button(boss: Dictionary) -> TextureButton:
	var button = TextureButton.new()
	#Loads the texture specified in the memberData sheet
	if boss.has("texturePath"):
		button.texture_normal = load(boss["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, boss))
	button.connect("mouse_entered", Callable(self, "_on_button_hovered").bind(button, boss))
	button.connect("mouse_exited", Callable(self, "_on_button_exited").bind(button))
	return button


func _on_button_hovered(button: TextureButton, boss: Dictionary):
	#Generates the hovered tooltips
	#The backslash  \ allows you to continue to the next line while concatenating the string
	var tooltip_text = "" + str(boss["name"]) + "\n" + "" + str(boss["description"]) + "\n" + "Total Health: " + str(boss["totalHealth"]) + "\n" + \
	"Minimum DPS Required: " + str(boss["minDPS"])
	button.tooltip_text = tooltip_text

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


func _on_button_pressed(button: TextureButton, boss: Dictionary):
	#Checks you have the prerquisites, deducts damage
	if Global.globalDamagePerClick >= int(boss["minClickDmg"]):
		if Global.globalDamagePerSec >= int(boss["minDPS"]):
			if Global.globalDamage >= int(boss["totalHealth"]):
				var damageCost = int(boss["totalHealth"])
				var bossTier = boss["bossTier"]
				Global.globalDamage -= damageCost
				$CharacterRoll.roll_character(bossTier)
			else:
				print("Not enough Total Damage!")
		else:
			print("Not enough DPS!")
	else:
		print("Not enough Damage per Click!")
		
