extends Node2D

func _ready() -> void:
	var tracking = Node.new()
	tracking.name = "TrackerSystem"
	tracking.add_to_group("system:tracking")
	tracking.set_script(preload("res://../vendor/open-vt/lib/tracking/tracking_system.gd"))
	add_child(tracking)

	var manager = Node.new()
	manager.name = "ModelManager"
	manager.set_script(preload("res://../vendor/open-vt/lib/model_manager.gd"))
	add_child(manager)

	await get_tree().process_frame
	await get_tree().process_frame

	var stage = preload("res://../vendor/open-vt/minimal_stage.tscn").instantiate()
	add_child(stage)
	await get_tree().process_frame
	await get_tree().process_frame

	var mm = get_tree().get_first_node_in_group("system:model")
	if mm == null:
		push_error("viewer_openvt: model manager unavailable")
		return

	var model = mm.make_model("user://Live2DModels/Haru")
	if model == null:
		push_error("viewer_openvt: failed to make Haru model")
		return

	stage.spawn_model(model)
	await model.loaded

	if model.is_queued_for_deletion():
		push_error("viewer_openvt: Haru model queued for deletion")
		return

	model.position = Vector2(640, 520)
	model.scale = Vector2(0.32, 0.32)

	print("MINIVIEW_OPENVT loaded=true")
	print("MINIVIEW_OPENVT mesh_count=%s" % JSON.stringify(model.get_meshes().size()))

