# Audio operations module
# Handles: add_audio_stream_player, set_audio_stream, configure_audio_bus
# Corresponds to: test_audio.py
class_name AudioOps


## Add an audio stream player to a scene
static func add_audio_stream_player(ctx: SceneTree, params: Dictionary) -> void:
	ctx.log_info("Adding audio stream player: " + params.player_name)

	var full_scene_path: String = params.scene_path
	if not full_scene_path.begins_with("res://"):
		full_scene_path = "res://" + full_scene_path

	if not FileAccess.file_exists(full_scene_path):
		ctx.log_error("Scene file does not exist: " + full_scene_path)
		ctx.quit(1)

	var scene: PackedScene = load(full_scene_path)
	if not scene:
		ctx.log_error("Failed to load scene: " + full_scene_path)
		ctx.quit(1)

	var scene_root: Node = scene.instantiate()

	# Find parent node
	var parent_node: Node = scene_root.get_node_or_null(NodePath(params.parent_node_path))
	if not parent_node:
		ctx.log_error("Parent node not found: " + params.parent_node_path)
		ctx.quit(1)

	# Create audio player
	var player: Node = ctx.instantiate_class(params.player_type)
	if not player:
		ctx.log_error("Failed to create audio player of type: " + params.player_type)
		ctx.quit(1)

	player.name = params.player_name

	# Set autoplay if requested
	if params.get("autoplay", false):
		player.autoplay = true

	# Add to scene
	parent_node.add_child(player)
	player.owner = scene_root

	if ctx.debug_mode:
		print("[DEBUG] Audio player added to scene")

	# Save scene
	var packed_scene: PackedScene = PackedScene.new()
	var result: int = packed_scene.pack(scene_root)

	if result == OK:
		var save_error: int = ResourceSaver.save(packed_scene, full_scene_path)
		if save_error == OK:
			print("Audio stream player '" + params.player_name + "' added successfully")
		else:
			ctx.log_error("Failed to save scene: " + str(save_error))
			ctx.quit(1)
	else:
		ctx.log_error("Failed to pack scene: " + str(result))
		ctx.quit(1)


## Set audio stream for a player
static func set_audio_stream(ctx: SceneTree, params: Dictionary) -> void:
	ctx.log_info("Setting audio stream")

	var full_scene_path: String = params.scene_path
	if not full_scene_path.begins_with("res://"):
		full_scene_path = "res://" + full_scene_path

	if not FileAccess.file_exists(full_scene_path):
		ctx.log_error("Scene file does not exist: " + full_scene_path)
		ctx.quit(1)

	var scene: PackedScene = load(full_scene_path)
	if not scene:
		ctx.log_error("Failed to load scene: " + full_scene_path)
		ctx.quit(1)

	var scene_root: Node = scene.instantiate()

	# Find audio player
	var player: Node = scene_root.get_node_or_null(NodePath(params.player_path))
	if not player:
		ctx.log_error("Audio player not found: " + params.player_path)
		ctx.quit(1)

	# Verify it's an audio player
	if not (player is AudioStreamPlayer or player is AudioStreamPlayer2D or player is AudioStreamPlayer3D):
		ctx.log_error("Node is not an AudioStreamPlayer: " + params.player_path)
		ctx.quit(1)

	# Load audio stream
	var audio_path: String = params.audio_file_path
	if not audio_path.begins_with("res://"):
		audio_path = "res://" + audio_path

	if not FileAccess.file_exists(audio_path):
		ctx.log_error("Audio file does not exist: " + audio_path)
		ctx.quit(1)

	var audio_stream: AudioStream = load(audio_path)
	if not audio_stream:
		ctx.log_error("Failed to load audio stream: " + audio_path)
		ctx.quit(1)

	# Set stream and properties
	player.stream = audio_stream
	player.volume_db = params.get("volume_db", 0.0)
	player.pitch_scale = params.get("pitch_scale", 1.0)

	if ctx.debug_mode:
		print("[DEBUG] Audio stream set on player")

	# Save scene
	var packed_scene: PackedScene = PackedScene.new()
	var result: int = packed_scene.pack(scene_root)

	if result == OK:
		var save_error: int = ResourceSaver.save(packed_scene, full_scene_path)
		if save_error == OK:
			print("Audio stream set successfully")
		else:
			ctx.log_error("Failed to save scene: " + str(save_error))
			ctx.quit(1)
	else:
		ctx.log_error("Failed to pack scene: " + str(result))
		ctx.quit(1)


## Configure audio bus
static func configure_audio_bus(ctx: SceneTree, params: Dictionary) -> void:
	ctx.log_info("Configuring audio bus: " + params.bus_name)

	# Find bus index
	var bus_idx: int = AudioServer.get_bus_index(params.bus_name)
	if bus_idx == -1:
		ctx.log_error("Audio bus not found: " + params.bus_name)
		ctx.quit(1)

	# Set properties
	if params.has("volume_db"):
		AudioServer.set_bus_volume_db(bus_idx, params.volume_db)
	if params.has("mute"):
		AudioServer.set_bus_mute(bus_idx, params.mute)
	if params.has("solo"):
		AudioServer.set_bus_solo(bus_idx, params.solo)

	if ctx.debug_mode:
		print("[DEBUG] Audio bus configured")

	print("Audio bus '" + params.bus_name + "' configured successfully")
