@tool
extends Control

@onready var grid: GridContainer = %GC_Rows
@onready var refresh_btn: Button = %BRefresh

const DB_PATH := "res://addons/reedscene/demo/data_base/GLOBAL_SCENE_ID.tres"

func _ready() -> void:
	refresh_btn.pressed.connect(_reload)
	#_reload()

func _notification(what: int) -> void:
	# 切回该主屏时自动刷新（可选，但很实用）
	if what == NOTIFICATION_VISIBILITY_CHANGED and visible:
		pass
		#_reload()

func _reload() -> void:
	_clear_grid()
	_build_header()

	var db = load(DB_PATH)
	if db == null:
		# 没 DB 就只显示表头，避免空白
		return

	# 假设 db.entries: Array[SceneIDEntry]，且 entry.scene_uid / entry.scene_id 存在
	for entry in db.entries:
		_add_row(entry)

func _clear_grid() -> void:
	for c in grid.get_children():
		c.queue_free()

func _build_header() -> void:
	grid.columns = 5
	_add_header_cell("ID")
	_add_header_cell("Name")
	_add_header_cell("UID")
	_add_header_cell("Path")
	_add_header_cell("Function")

func _add_header_cell(text: String) -> void:
	var l := Label.new()
	l.text = text
	l.size_flags_horizontal = Control.SIZE_EXPAND_FILL
	l.clip_text = true
	grid.add_child(l)

func _add_row(entry) -> void:
	var uid_text: String = entry.scene_uid
	var uid_id := ResourceUID.text_to_id(uid_text)

	var path := ""
	if uid_id != ResourceUID.INVALID_ID:
		path = ResourceUID.get_id_path(uid_id)

	var name := path.get_file().get_basename() if path != "" else "(missing)"

	# 1) ID
	grid.add_child(_cell_label(str(entry.scene_id)))
	# 2) Name
	grid.add_child(_cell_label(name))
	# 3) UID
	grid.add_child(_cell_label(uid_text))
	# 4) Path（先显示，后面你想隐藏我们再做）
	grid.add_child(_cell_label(path))
	# 5) Function（真按钮）
	grid.add_child(_function_cell(uid_text))

func _cell_label(text: String) -> Control:
	var l := Label.new()
	l.text = text
	l.clip_text = true
	l.size_flags_horizontal = Control.SIZE_EXPAND_FILL
	return l

func _function_cell(uid_text: String) -> Control:
	var box := HBoxContainer.new()
	box.size_flags_horizontal = Control.SIZE_EXPAND_FILL

	## 打開場景所使用的函數
	var reveal_btn := Button.new()
	reveal_btn.text = "Show"
	reveal_btn.tooltip_text = "Reveal in FileSystem"
	reveal_btn.pressed.connect(func():
		_reveal_scene_in_filesystem_by_uid(uid_text)
	)
	box.add_child(reveal_btn)

	# Delete 先做占位，下一步再落地到 Resource 修改/保存
	var del_btn := Button.new()
	del_btn.text = "Delete"
	del_btn.pressed.connect(func():
		print("[SceneID] TODO delete: ", uid_text)
	)
	box.add_child(del_btn)

	return box

func _open_scene_by_uid(uid_text: String) -> void:
	var uid_id := ResourceUID.text_to_id(uid_text)
	if uid_id == ResourceUID.INVALID_ID:
		push_warning("[SceneID] Invalid UID: %s" % uid_text)
		return

	var path := ResourceUID.get_id_path(uid_id)
	if path == "" or not ResourceLoader.exists(path):
		push_warning("[SceneID] Scene not found for UID: %s" % uid_text)
		return

	EditorInterface.open_scene_from_path(path)

## 打開文件管理系統裏的Scene
func _reveal_in_filesystem(path: String) -> void:
	# FileSystemDock 才有 navigate_to_path
	var dock := EditorInterface.get_file_system_dock()
	dock.navigate_to_path(path)

## 通過UID打開文件管理系統裏的Scene
func _reveal_scene_in_filesystem_by_uid(uid_text: String) -> void:
	var uid_id := ResourceUID.text_to_id(uid_text)
	if uid_id == ResourceUID.INVALID_ID:
		push_warning("[SceneID] Invalid UID: %s" % uid_text)
		return

	var path := ResourceUID.get_id_path(uid_id)
	if path == "" or not ResourceLoader.exists(path):
		push_warning("[SceneID] Scene not found for UID: %s" % uid_text)
		return

	var dock := EditorInterface.get_file_system_dock()
	dock.navigate_to_path(path)

func on_activated() -> void:
	_reload()
