@tool
extends Polygon2D
class_name SetPiece


# Declare member variables here. Examples:
# var a = 2
# var b = "text"


# Called when the node enters the scene tree for the first time.
func _ready():
	_update_polygon_from_resource()
	self.color.a = 0.25
	if Engine.is_editor_hint():
		self.color.a = 0.25
		notify_property_list_changed()
		pass
	else:
		hide()
	pass # Replace with function body


func _update_polygon_from_resource() -> void:
	if points_resource and points_resource.points.size() > 0:
		polygon = points_resource.points

signal interacted
signal walked
signal looked
signal touched
signal talked
signal entered(lab)
signal exited(lab)

@export var label: String
@export var priority: int = 0
@export var points_resource: PolygonPointsResource:
	set(value):
		points_resource = value
		_update_polygon_from_resource()

var is_in = false
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	if Engine.is_editor_hint():
		self.color.a = 0.25
	else:
		if Geometry2D.is_point_in_polygon(to_local(get_global_mouse_position()), self.polygon):
			if is_in == false:
				is_in = true
				emit_signal("entered", label)
		else:
			if is_in == true:
				emit_signal("exited", label)
			is_in = false



func _get_overlapping_setpieces() -> Array[SetPiece]:
	var overlapping: Array[SetPiece] = []
	var mouse_pos = to_local(get_global_mouse_position())
	
	for child in get_tree().get_nodes_in_group("set-piece"):
		if child is SetPiece and child != self:
			if child.polygon.size() > 0 and Geometry2D.is_point_in_polygon(mouse_pos, child.polygon):
				overlapping.append(child)
	
	return overlapping

func _input(event):
	if not Engine.is_editor_hint():
		if Geometry2D.is_point_in_polygon(to_local(get_global_mouse_position()), self.polygon):
			if event is InputEventMouseButton and Input.is_action_just_released("interact"):
				# Check if a script is running that shouldn't be interrupted
				var script_builder = get_node_or_null("/root/Node2D/GameScript")
				if script_builder and script_builder.current_script and not script_builder.current_script.can_interrupt:
					return  # Let the script handle the input
				
				# Find all overlapping SetPieces and check if this one has highest priority
				var overlapping = _get_overlapping_setpieces()
				overlapping.append(self)
				
				var max_priority = self.priority
				for piece in overlapping:
					if piece.priority > max_priority:
						max_priority = piece.priority
				
				# Only process if this SetPiece has the highest priority
				if self.priority < max_priority:
					return
				
				get_viewport().set_input_as_handled()
				
				# Check if interacted signal has connections - it takes precedence
				if interacted.get_connections().size() > 0:
					emit_signal("interacted")
					return
				
				# Otherwise, emit action-specific signal based on current cursor
				match ActionState.current_action:
					ActionState.Action.WALK:
						if walked.get_connections().size() > 0:
							emit_signal("walked")
					ActionState.Action.LOOK:
						if looked.get_connections().size() > 0:
							emit_signal("looked")
					ActionState.Action.TOUCH:
						if touched.get_connections().size() > 0:
							emit_signal("touched")
					ActionState.Action.TALK:
						if talked.get_connections().size() > 0:
							emit_signal("talked")
