extends Node

# 场景管理器 - 负责场景切换和管理
# 提供场景切换的统一接口

signal scene_changed(scene_name: String)
signal scene_change_started(scene_name: String)

var current_scene_name: String = ""
var is_changing_scene: bool = false

# 场景路径映射
var scene_paths: Dictionary = {
	"main": "res://scenes/main_scene.tscn",
	"auth": "res://scenes/auth_scene.tscn",
	"game": "res://scenes/game_scene.tscn",
	"battle": "res://scenes/battle_scene.tscn",
	"inventory": "res://scenes/inventory_scene.tscn",
	"shop": "res://scenes/shop_scene.tscn",
	"settings": "res://scenes/settings_scene.tscn"
}

func _ready():
	print("SceneManager 初始化完成")

func change_scene(scene_name: String, use_transition: bool = true):
	if is_changing_scene:
		print("场景切换中，忽略新的切换请求")
		return false
	
	if not scene_paths.has(scene_name):
		print("错误: 未找到场景 ", scene_name)
		return false
	
	var scene_path = scene_paths[scene_name]
	print("开始切换场景: ", current_scene_name, " -> ", scene_name)
	
	is_changing_scene = true
	scene_change_started.emit(scene_name)
	
	if use_transition:
		await show_transition()
	
	var error = get_tree().change_scene_to_file(scene_path)
	if error != OK:
		print("场景切换失败: ", error)
		is_changing_scene = false
		return false
	
	current_scene_name = scene_name
	is_changing_scene = false
	scene_changed.emit(scene_name)
	
	if use_transition:
		await hide_transition()
	
	print("场景切换完成: ", scene_name)
	return true

func get_current_scene_name() -> String:
	return current_scene_name

func register_scene(scene_name: String, scene_path: String):
	scene_paths[scene_name] = scene_path
	print("注册场景: ", scene_name, " -> ", scene_path)

func show_transition():
	# TODO: 实现场景切换过渡效果
	print("显示场景切换过渡效果")
	await get_tree().create_timer(0.2).timeout

func hide_transition():
	# TODO: 隐藏场景切换过渡效果
	print("隐藏场景切换过渡效果")
	await get_tree().create_timer(0.2).timeout
