extends Node2D


var hovered_setpieces: Array[Dictionary] = []

@onready var label : Label = $"label"
func _ready():
	_connect_setpieces(get_parent())

func _on_transitioned(s):
	hovered_setpieces.clear()
	$label.hide()
	_connect_setpieces(get_parent())

func _connect_setpieces(scene: Node) -> void:
	for n in scene.find_children("*", "SetPiece", true, false):
		print("LOOKING AT", n)
		n.connect("entered", Callable(self, "_on_setpiece_entered"))
		n.connect("exited", Callable(self, "_on_setpiece_exited"))
		
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	#var ideal:Vector2 = $"..".to_local(get_global_mouse_position())
	#var bg = $"..".find_child("bg")
	#var far_point = self.to_global(Vector2(bg.texture.get_width(), bg.texture.get_height() ) * bg.get_global_transform())
	##self.position.x = clamp(ideal.x, 0,  far_point.x - 200)
	#var camera = $"..".find_child("Camera2D")
	#var cam = get_viewport()
	#var z = cam.get_visible_rect()
	#var cam2 = cam.get_camera_2d()
	#cam2.get_screen_transform()
	#self.position.x = clamp(ideal.x, z.position.x, z.end.x - 500)
	#self.position.y = clamp(ideal.y, z.position.y, z.end.y - 50)
	#
	#var viewport = get_viewport()
	#var viewport_size = viewport.get_visible_rect().size
	#
	#var camera_position = cam2.global_position
	#
	#var margin = 10
	## Example positioning: Top-left corner of the viewport
	#var start = Vector2(camera_position.x - viewport_size.x / 2 + margin,  camera_position.y - viewport_size.y / 2 + margin)
	#var end =  start +  viewport_size
	## Here 'margin' is the distance you want to keep from the viewport edge
	#
	#
	##print("ideal", ideal, "start", start, "end", end)
	#print(cam2.get_screen_center_position())
	## Adjust this to keep the label within the viewport bounds
	#global_position = Vector2(clamp(ideal.x, start.x, end.x - 400), clamp(ideal.y, start.y, end.y))
	var viewport = get_viewport_rect()
	var mouse_pos = get_viewport().get_mouse_position()

	# Obtain camera information if camera is used
	var camera = get_viewport().get_camera_2d()
	mouse_pos = camera.get_global_mouse_position()
	if camera:
		# Calculate the screen boundaries considering the camera's position and zoom
		var cam_top_left = camera.get_screen_center_position() - (viewport.size * 0.5 / camera.zoom)
		var cam_bottom_right = camera.get_screen_center_position() + (viewport.size * 0.5 / camera.zoom)

		# Clamp mouse position to camera's visible area
		var margin = 100
		var s = label.size
		mouse_pos.x = clamp(mouse_pos.x, cam_top_left.x + margin, cam_bottom_right.x - label.size.x - margin)
		mouse_pos.y = clamp(mouse_pos.y, cam_top_left.y, cam_bottom_right.y)

 
   # Update label position
	global_position = mouse_pos


func _on_setpiece_entered(lab: String) -> void:
	var piece = get_setpiece_by_label(lab)
	if piece:
		var entry = {"label": lab, "priority": piece.priority}
		if not _has_entry_with_label(lab):
			hovered_setpieces.append(entry)
	_update_label()


func _on_setpiece_exited(lab: String) -> void:
	for i in range(hovered_setpieces.size()):
		if hovered_setpieces[i].label == lab:
			hovered_setpieces.remove_at(i)
			break
	_update_label()


func _has_entry_with_label(lab: String) -> bool:
	for entry in hovered_setpieces:
		if entry.label == lab:
			return true
	return false

func get_setpiece_by_label(lab: String) -> SetPiece:
	var scene = get_parent()
	for child in scene.find_children("*", "SetPiece", true, false):
		if child is SetPiece and child.label == lab:
			return child
	return null

func _update_label() -> void:
	print(hovered_setpieces)
	if hovered_setpieces.is_empty():
		$label.hide()
		return
	
	hovered_setpieces.sort_custom(func(a, b): return b.priority > a.priority)
	var top_entry = hovered_setpieces[0]
	$label.show()
	$label.text = top_entry.label
	
	var size = label.label_settings.font.get_string_size(top_entry.label, HORIZONTAL_ALIGNMENT_LEFT, 200, 32)
	label.size = size
