extends RefCounted
## Roadmap unlocks screenshots (main.gd --shot ... --shotroadmap=<op>,<op>...): ops run in order, then main.gd saves.
##   lvl:N            Base Lv N on every class slot
##   tut:all | tut:K  tutorials done up to the level (all) / feature K (force)
##   menu             fold nothing, just wait (the minimap-side menu shows its grey / open icons)
##   drawer           open the Functions drawer
##   wardrobe:KIND    open the Wardrobe on that tab
##   own:ID           give skin ID (owned)       wear:ID   give + wear skin ID
##   pull:KIND:N      shards + pull N from that skin well (the reveal stays open)
##   ride             get on the mount           awaken    the awakened aura on the own hero (view only)
##   cam:yaw,pitch,dist  camera (camera_rig orbit)
##   wait:S           seconds

const Unlocks = preload("res://scripts/server/unlocks.gd")
const Skins = preload("res://scripts/server/skins.gd")
const Mounts = preload("res://scripts/server/mounts.gd")


static func _frames(main: Node, n: int) -> void:
	for i in n:
		await main.get_tree().process_frame


static func run(main: Node, spec: String) -> void:
	var c = PlayerState.ch
	GameData.config["unlock_free_test"] = false
	GameData.config["roadmap_gates"] = true
	for op in spec.split(",", false):
		var p := op.split(":")
		match p[0]:
			"lvl":
				c.base_lv = int(p[1])
				for s in c.job_slots:
					if s is Dictionary:
						s["base_lv"] = int(p[1])
				c.changed.emit()
			"tut":
				if p[1] == "all":
					for k in Unlocks.keys_by_level():
						if Unlocks.level_for(c, str(k)) >= Unlocks.feature_level(str(k)):
							Unlocks.complete_tutorial(c, str(k), true)
				else:
					Unlocks.complete_tutorial(c, str(p[1]), true)
			"drawer":
				var m = main.hud.find_child("MenuIcons", true, false)
				if m:
					m.open_drawer()
			"wardrobe":
				load("res://scripts/ui/wardrobe_ui.gd").inst(main.hud).open(str(p[1]) if p.size() > 1 else "")
			"own", "wear":
				var id := str(p[1])
				if not Skins.owns(c, id):
					Skins.ensure(c)["owned"][id] = 1
				if p[0] == "wear":
					Skins.set_worn(c, Skins.kind_of(id), id)
				c.changed.emit()
			"pull":
				Mounts.ensure(c)["shards"] = 1000
				var w = load("res://scripts/ui/wardrobe_ui.gd").inst(main.hud)
				w.open(str(p[1]))
				w._pull(int(p[2]) if p.size() > 2 else 10)
			"ride":
				main.send({"t": "mount", "on": true})
			"awaken":
				if main.player:
					main.player.awaken(20.0)
			"cam":
				var v := str(p[1]).split(";")
				if main.rig and v.size() >= 3:
					main.rig.yaw = float(v[0])
					main.rig.pitch = float(v[1])
					main.rig.dist = float(v[2])
			"probe":
				# debug: the hero's surface materials and their dye
				var mdl = main.player.model if main.player else null
				print("PROBE skin_fs=", mdl.get_meta("skin_fs", "?") if mdl else "-", " worn=", Skins.worn(c, "fashion"))
				if mdl:
					for mi in mdl.find_children("*", "MeshInstance3D", true, false):
						var m3 := mi as MeshInstance3D
						for i in range(-1, m3.mesh.get_surface_count() if m3.mesh else 0):
							var mt = m3.material_override if i < 0 else m3.get_surface_override_material(i)
							if mt is ShaderMaterial:
								print("PROBE ", m3.name, " ", i, " ", (mt as ShaderMaterial).shader.resource_path, " amt=", mt.get_shader_parameter("outfit_amt"), " tint=", mt.get_shader_parameter("outfit_tint"))
							elif mt != null:
								print("PROBE ", m3.name, " ", i, " ", mt.get_class())
						if m3.mesh and m3.get_surface_override_material(0) == null and m3.material_override == null:
							print("PROBE ", m3.name, " mesh material ", m3.mesh.surface_get_material(0))
			"wait":
				await main.get_tree().create_timer(float(p[1])).timeout
		await _frames(main, 6)
	await _frames(main, 30)
