extends RefCounted
## Thai monster / skill names everywhere (Boss 2026-10-20): selftest --only=thname (also in the full run).
## In Thai the target frame, the map / minimap icons (tooltips), kill + boss chat lines, quest objectives and the skill
## shouts over a hero (sent by an English server) use the same Thai name as the nameplate; in English the English one.

const SelfTest = preload("res://scripts/selftest.gd")
const MsgTh = preload("res://scripts/ui/msg_th.gd")
const MapIcons = preload("res://scripts/map_icons.gd")


static func check(ok: bool, what: String) -> void:
	SelfTest.check(ok, "thname: " + what)


static func _view_of(main: Node, mid: String, near: Vector3):
	var best = null
	var bd := 1e9
	for v in main.get_tree().get_nodes_in_group("monsters"):
		if v.get("mid") == null or str(v.get("mid")) != mid or v.get("data") == null or not (v is Node3D):
			continue
		var dd: float = (v as Node3D).global_position.distance_to(near)
		if dd < bd:
			bd = dd
			best = v
	return best


static func _labels(n: Node, out: Array) -> void:
	if n is Label3D:
		out.append(str((n as Label3D).text))
	for c in n.get_children():
		_labels(c, out)


static func run(main: Node) -> void:
	print("SELFTEST thname")
	var old_loc := TranslationServer.get_locale()
	var mid := "carrot_bunny"
	var md: Dictionary = GameData.monsters.get(mid, {})
	var en := str(md.get("name", ""))
	var th := str(md.get("name_th", ""))
	check(en != "" and th != "" and th != en, "the test monster has a Thai name (%s / %s)" % [en, th])
	TranslationServer.set_locale("th")
	var wrong: Array = []
	for id in GameData.monsters:
		var d: Dictionary = GameData.monsters[id]
		if str(d.get("name_th", "")) != "" and MsgTh.mob_name(str(id), d) != str(d["name_th"]):
			wrong.append(id)
	check(wrong.is_empty(), "Thai: every monster's shown name is its name_th (wrong: %s)" % str(wrong.slice(0, 6)))
	check(MsgTh.mob_name("", {"name": en}) == th, "Thai: a monster without a known kind goes through name_th")
	check(MsgTh.tr_msg("You defeated %s." % en).contains(th), "Thai: kill chat line names the monster in Thai")
	check(MsgTh.tr_msg("*** %s has been defeated! ***" % en).contains(th), "Thai: boss defeated line in Thai")
	var sk: Dictionary = GameData.skills.get("as_grim_talon", {})
	check(str(sk.get("name_th", "")) != "" and MsgTh.name_th(str(sk.get("name", ""))) == str(sk.get("name_th", "")),
		"Thai: an English skill shout name -> Thai (%s)" % MsgTh.name_th(str(sk.get("name", ""))))
	# live: a monster next to the hero -> target frame, map icons, a shout event
	var pe = main.me()
	var hud = main.hud
	var m = pe.map.spawn_mob(mid, pe.global_position + Vector3(3.0, 0, 0), 0.05, false)
	await SelfTest.frames(main, 12)
	var v = _view_of(main, mid, pe.global_position + Vector3(3.0, 0, 0))
	check(v != null, "the spawned monster has a view")
	if v != null and hud != null:
		hud.target_mon = v
		hud._update_target()
		check(str(hud.target_lbl.text).begins_with(th), "Thai: target frame shows the Thai name (%s)" % hud.target_lbl.text)
		var icons: Array = MapIcons.collect(str(pe.map.map_id) if pe.map.get("map_id") != null else "", true, main.get_tree())
		var names: Array = []
		for e in icons:
			if str(e.get("t", "")) == "mob":
				names.append(str(e.get("name", "")))
		check(names.has(th) and not names.has(en), "Thai: map / minimap monster icons name it in Thai")
		TranslationServer.set_locale("en")
		hud._update_target()
		check(str(hud.target_lbl.text).begins_with(en), "English: target frame shows the English name (%s)" % hud.target_lbl.text)
		TranslationServer.set_locale("th")
	# the shout of a skill, as an English server sends it
	var my = main.get("my_id")
	if my != null and sk.has("name"):
		main._on_event({"t": "shout", "id": int(my), "text": str(sk["name"]) + "!!"})
		await SelfTest.frames(main, 2)
		var ls: Array = []
		_labels(main, ls)
		check(ls.has(str(sk["name_th"]) + "!!") and not ls.has(str(sk["name"]) + "!!"), "Thai: the skill shout over the hero is in Thai")
	TranslationServer.set_locale(old_loc)
	if hud != null:
		hud.target_mon = null
	if is_instance_valid(m):
		pe.map.remove_mob(m)
	await SelfTest.frames(main, 4)
