# CanvasLayer operations module
# Handles: add_canvas_layer, set_canvas_layer_order, configure_canvas_layer
# Corresponds to: test_canvas_layer.py
class_name CanvasLayerOps


## Add a canvas layer to a scene
static func add_canvas_layer(ctx: SceneTree, params: Dictionary) -> void:
	ctx.log_info("Adding canvas layer: " + params.layer_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 canvas layer
	var canvas_layer: CanvasLayer = CanvasLayer.new()
	canvas_layer.name = params.layer_name
	canvas_layer.layer = params.get("layer_order", 0)

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

	if ctx.debug_mode:
		print("[DEBUG] CanvasLayer 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("CanvasLayer '" + params.layer_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 canvas layer order
static func set_canvas_layer_order(ctx: SceneTree, params: Dictionary) -> void:
	ctx.log_info("Setting canvas layer order")

	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 canvas layer
	var canvas_layer: Node = scene_root.get_node_or_null(NodePath(params.layer_path))
	if not canvas_layer:
		ctx.log_error("CanvasLayer not found: " + params.layer_path)
		ctx.quit(1)

	if not canvas_layer is CanvasLayer:
		ctx.log_error("Node is not a CanvasLayer: " + params.layer_path)
		ctx.quit(1)

	# Set order
	canvas_layer.layer = params.layer_order

	if ctx.debug_mode:
		print("[DEBUG] CanvasLayer order set to: " + str(params.layer_order))

	# 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("CanvasLayer order 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 canvas layer properties
static func configure_canvas_layer(ctx: SceneTree, params: Dictionary) -> void:
	ctx.log_info("Configuring canvas layer")

	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 canvas layer
	var canvas_layer: Node = scene_root.get_node_or_null(NodePath(params.layer_path))
	if not canvas_layer:
		ctx.log_error("CanvasLayer not found: " + params.layer_path)
		ctx.quit(1)

	if not canvas_layer is CanvasLayer:
		ctx.log_error("Node is not a CanvasLayer: " + params.layer_path)
		ctx.quit(1)

	# Set properties
	if params.has("offset"):
		var offset_data = _parse_vector2(params.offset)
		if offset_data:
			canvas_layer.offset = offset_data

	if params.has("rotation"):
		canvas_layer.rotation = params.rotation

	if params.has("scale"):
		var scale_data = _parse_vector2(params.scale)
		if scale_data:
			canvas_layer.scale = scale_data

	if params.has("follow_viewport"):
		canvas_layer.follow_viewport_enabled = params.follow_viewport

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


# Helper function

static func _parse_vector2(data) -> Vector2:
	if typeof(data) == TYPE_STRING:
		var json: JSON = JSON.new()
		if json.parse(data) == OK:
			var arr: Array = json.get_data()
			if arr.size() >= 2:
				return Vector2(arr[0], arr[1])
	elif typeof(data) == TYPE_ARRAY and data.size() >= 2:
		return Vector2(data[0], data[1])
	return Vector2.ZERO
