extends Control

var node_name: String
var next_ui_node = null  # 存储下一个要显示的UI节点引用
var is_active: bool = false # 活动状态标志

# 添加一个信号，当notice关闭时发出
signal notice_closed

func _ready() -> void:
	node_name = str(self.name)
	#visible = false  # 初始状态为隐藏
	#set_process_input(false)

func _start(notice_info, next_node = null) -> void:
	var label = get_node_or_null("Part_2/Part_3/Label")
	if label: label.text = notice_info
	
	# 存储下一个UI节点
	next_ui_node = next_node
	
	self.visible = true
	GameState.pause_game()
	set_process_input(true)
	is_active = true # 标记为活动状态
	print(node_name + ": Activated.")

# 处理输入事件 (用于右键关闭弹窗)
func _input(event):
	if not is_active: return
	# 检测鼠标右键点击事件
	if event is InputEventMouseButton and (event.button_index == MOUSE_BUTTON_LEFT or event.button_index == MOUSE_BUTTON_RIGHT) and event.is_pressed():
		_close_notice()
		# 消耗事件，防止它被传递到游戏世界
		get_viewport().set_input_as_handled()

# 新增关闭notice的方法，便于代码复用
func _close_notice():
	if not is_active: return
	
	self.visible = false
	print(node_name + ": Closed popup via right-click: ", self.name)
	
	# 如果所有弹窗都已关闭，恢复游戏并停止输入处理
	print(node_name + ": resuming game.")
	if GameState: GameState.resume_game()
	set_process_input(false)
	
	# 发出关闭信号
	emit_signal("notice_closed")
	
	# 检查是否有下一个UI需要显示
	if next_ui_node != null and is_instance_valid(next_ui_node):
		print(node_name + ": Showing next UI: ", next_ui_node.name)
		# next_ui_node.visible = true
		if next_ui_node.has_method("show_up"):
			next_ui_node.show_up()
		
		# 重置引用
		next_ui_node = null
	
