# Viewport operations module
# Handles: add_viewport, set_viewport_size, configure_viewport
# Corresponds to: test_viewport.py
class_name ViewportOps


## Add a SubViewport to a scene
static func add_viewport(ctx: SceneTree, params: Dictionary) -> void:
	ctx.log_info("Adding viewport: " + params.viewport_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 SubViewport
	var viewport: SubViewport = SubViewport.new()
	viewport.name = params.viewport_name
	viewport.size = Vector2i(params.get("width", 1024), params.get("height", 600))

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

	if ctx.debug_mode:
		print("[DEBUG] SubViewport 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("Viewport '" + params.viewport_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 viewport size
static func set_viewport_size(ctx: SceneTree, params: Dictionary) -> void:
	ctx.log_info("Setting viewport size")

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

	if not viewport is SubViewport:
		ctx.log_error("Node is not a SubViewport: " + params.viewport_path)
		ctx.quit(1)

	# Set size
	viewport.size = Vector2i(params.width, params.height)

	if ctx.debug_mode:
		print("[DEBUG] Viewport size set")

	# 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("Viewport size updated")
		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 viewport properties
static func configure_viewport(ctx: SceneTree, params: Dictionary) -> void:
	ctx.log_info("Configuring viewport")

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

	if not viewport is SubViewport:
		ctx.log_error("Node is not a SubViewport: " + params.viewport_path)
		ctx.quit(1)

	# Set properties
	if params.has("transparent_bg"):
		viewport.transparent_bg = params.transparent_bg

	if params.has("handle_input_locally"):
		viewport.handle_input_locally = params.handle_input_locally

	if params.has("render_target_update_mode"):
		var mode: String = params.render_target_update_mode.to_upper()
		match mode:
			"DISABLED":
				viewport.render_target_update_mode = SubViewport.UPDATE_DISABLED
			"ONCE":
				viewport.render_target_update_mode = SubViewport.UPDATE_ONCE
			"WHENVISIBLE":
				viewport.render_target_update_mode = SubViewport.UPDATE_WHEN_VISIBLE
			"WHENPARENTVISIBLE":
				viewport.render_target_update_mode = SubViewport.UPDATE_WHEN_PARENT_VISIBLE
			"ALWAYS":
				viewport.render_target_update_mode = SubViewport.UPDATE_ALWAYS

	if params.has("msaa"):
		var msaa_mode: String = params.msaa.to_upper()
		match msaa_mode:
			"DISABLED":
				viewport.msaa_3d = Viewport.MSAA_DISABLED
			"2X":
				viewport.msaa_3d = Viewport.MSAA_2X
			"4X":
				viewport.msaa_3d = Viewport.MSAA_4X
			"8X":
				viewport.msaa_3d = Viewport.MSAA_8X

	if ctx.debug_mode:
		print("[DEBUG] Viewport 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("Viewport 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)
