# Timer operations module
# Handles: add_timer, configure_timer
# Corresponds to: test_timer.py
class_name TimerOps


## Add a timer node to a scene
static func add_timer(ctx: SceneTree, params: Dictionary) -> void:
	ctx.log_info("Adding timer: " + params.timer_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 timer
	var timer: Timer = Timer.new()
	timer.name = params.timer_name
	timer.wait_time = params.get("wait_time", 1.0)
	timer.one_shot = params.get("one_shot", false)
	timer.autostart = params.get("autostart", false)

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

	if ctx.debug_mode:
		print("[DEBUG] Timer 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("Timer '" + params.timer_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)


## Configure timer properties
static func configure_timer(ctx: SceneTree, params: Dictionary) -> void:
	ctx.log_info("Configuring timer")

	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 timer node
	var timer: Node = scene_root.get_node_or_null(NodePath(params.timer_path))
	if not timer:
		ctx.log_error("Timer node not found: " + params.timer_path)
		ctx.quit(1)

	if not timer is Timer:
		ctx.log_error("Node is not a Timer: " + params.timer_path)
		ctx.quit(1)

	# Set properties
	if params.has("wait_time"):
		timer.wait_time = params.wait_time
	if params.has("one_shot"):
		timer.one_shot = params.one_shot
	if params.has("autostart"):
		timer.autostart = params.autostart

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

	# 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("Timer configured 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)
