extends SceneTree

const BattleStateScript := preload("res://scripts/core/battle_state.gd")
const BattleSceneScript := preload("res://scripts/scenes/battle_scene.gd")


func _init() -> void:
	var failures: Array[String] = []
	_check_status_summary_duration_text(failures)
	_check_hud_reuses_status_summary(failures)

	if failures.is_empty():
		print("status effect summary smoke ok")
		quit(0)
		return

	for failure in failures:
		push_error(failure)
	quit(1)


func _check_status_summary_duration_text(failures: Array[String]) -> void:
	var state = _loaded_state(failures, "status summary")
	if state == null:
		return
	var cao_cao: Dictionary = state.get_unit("cao_cao")
	cao_cao["status_effects"] = [
		{
			"type": "stat_bonus",
			"stat": "atk",
			"amount": 3,
			"remaining_phases": 2
		},
		{
			"type": "damage_over_time",
			"status": "poison",
			"amount": 4,
			"remaining_phases": 1
		},
		{
			"type": "action_lock",
			"status": "seal",
			"action": "skill",
			"remaining_phases": 3
		}
	]
	var summary: String = state.get_status_effect_summary("cao_cao")
	_check_contains(failures, "stat duration", summary, "ATK +3 (2 phases)")
	_check_contains(failures, "poison duration", summary, "Poison -4 HP (1 phase)")
	_check_contains(failures, "seal duration", summary, "Seal no tactics (3 phases)")


func _check_hud_reuses_status_summary(failures: Array[String]) -> void:
	var scene = BattleSceneScript.new()
	scene._create_hud()
	scene.state.load_battle("res://data/scenarios/001_yellow_turbans.json")
	var cao_cao: Dictionary = scene.state.get_unit("cao_cao")
	cao_cao["status_effects"] = [
		{
			"type": "stat_bonus",
			"stat": "def",
			"amount": -2,
			"remaining_phases": 2
		}
	]
	scene.state.selected_unit_id = "cao_cao"
	scene.hover_cell = Vector2i(1, 3)
	scene._update_hud()
	_check_contains(failures, "selected HUD status", scene.selected_label.text, "Effects: DEF -2 (2 phases)")
	_check_contains(failures, "hover tile status", scene.cell_info_label.text, "Effects: DEF -2 (2 phases)")
	scene.free()


func _loaded_state(failures: Array[String], label: String):
	var state = BattleStateScript.new()
	if not state.load_battle("res://data/scenarios/001_yellow_turbans.json"):
		failures.append("%s could not load opening scenario" % label)
		return null
	return state


func _check_contains(failures: Array[String], label: String, text: String, expected: String) -> void:
	if text.contains(expected):
		return
	failures.append("%s expected `%s` in `%s`" % [label, expected, text])
