@tool
extends EditorPlugin

var inspector_plugins: Array[EditorInspectorPlugin] = []

## Autoload的SceneRegistery的Name
const AUTOLOAD_REED_SCENE_NAME : StringName = &"ReedSceneRegistry"
## Autoload的SceneRegistery的PATH
const AUTOLOAD_REED_SCENE_PATH := "res://addons/reedscene/scene/SceneRegistry.gd"

## Act的Inspector輔助面板
const ACT_INSPECTOR_SCENE_PATH := "res://addons/reedscene/act/ActManagerInspector.gd"
## ID的Inspector輔助面板
const ID_INSEPCTOR_SCENE_PATH := "res://addons/reedscene/scene/ReedSceneIDInspector.gd"

## SceneID注冊查詢面板的位置
const SCENE_ID_MAIN_PANEL_PATH := "res://addons/reedscene/dock/SceneIDMainPanel.tscn"

## 配置Json的地址
const CONFIG_JSON_PATH:= "res://addons/reedscene/plugin_config.json"

var main_screen: Control


# ======================================================
# 生命周期
# ======================================================

func _enter_tree() -> void:
	_load_mainscreen()
	_ensure_autoload()
	_register_inspectors()


func _exit_tree() -> void:
	_unload_mainscreen()
	_remove_autoload()
	_unregister_inspectors()


## 注冊Inspector
func _register_inspectors() -> void:
	
	#TODO: 临时禁用Act的Inspector，后续如果有需求再迭代。
	# Inspector 1：ActManager
	#var act_inspector := preload(
		#ACT_INSPECTOR_SCENE_PATH
	#).new()
#
	#add_inspector_plugin(act_inspector)
	#inspector_plugins.append(act_inspector)

	# Inspector 2：ReedSceneID
	var scene_id_inspector := preload(
		ID_INSEPCTOR_SCENE_PATH
	).new()

	add_inspector_plugin(scene_id_inspector)
	inspector_plugins.append(scene_id_inspector)

## 注銷Inspector
func _unregister_inspectors() -> void:
	for plugin in inspector_plugins:
		remove_inspector_plugin(plugin)
	inspector_plugins.clear()


## 注冊Autoload
func _ensure_autoload() -> void:
	var key := "autoload/%s" % AUTOLOAD_REED_SCENE_NAME
	if not ProjectSettings.has_setting(key):
		add_autoload_singleton(
			AUTOLOAD_REED_SCENE_NAME,
			AUTOLOAD_REED_SCENE_PATH
		)

## 注銷Autoload
func _remove_autoload() -> void:
	var key := "autoload/%s" % AUTOLOAD_REED_SCENE_NAME
	if ProjectSettings.has_setting(key):
		remove_autoload_singleton(AUTOLOAD_REED_SCENE_NAME)

## 加載插件編輯面板
func _load_mainscreen() -> void:
	main_screen = preload(
		SCENE_ID_MAIN_PANEL_PATH
	).instantiate()

	EditorInterface.get_editor_main_screen().add_child(main_screen)
	main_screen.visible = false

## 注銷插件編輯面板
func _unload_mainscreen() -> void:
	if main_screen:
		main_screen.queue_free()
		main_screen = null


func _has_main_screen() -> bool:
	return true


func _make_visible(visible: bool) -> void:
	if not main_screen:
		return

	main_screen.visible = visible

	if visible and main_screen.has_method("on_activated"):
		main_screen.on_activated()


# ======================================================
# Meta
# ======================================================

func _get_plugin_name() -> String:
	return "Reed Scene"


func _get_plugin_icon() -> Texture2D:
	return EditorInterface.get_editor_theme().get_icon(
		"Node",
		"EditorIcons"
	)

## 通過Json獲取到Plugin的基本配置
func _get_config_data() -> Dictionary:
	return {}
