extends CanvasLayer

signal advanced

@onready var subtitle_panel: PanelContainer = $SubtitlePanel
@onready var who_label: Label = $SubtitlePanel/V/Who
@onready var text_label: RichTextLabel = $SubtitlePanel/V/Text
@onready var hint_label: Label = $SubtitlePanel/V/Hint

@onready var note_overlay: Control = $NoteOverlay
@onready var note_text: RichTextLabel = $NoteOverlay/Center/P/V2/Text

@onready var prompt_label: Label = $PromptLabel
@onready var controls_hint: Label = $ControlsHint
@onready var crosshair: Control = $Crosshair

var _hint_timer: Timer

@onready var date_time_label: Label = $DateTimeLabel
@onready var objective_label: Label = $ObjectiveLabel
@onready var money_label: Label = $MoneyLabel
@onready var weather_label: Label = $WeatherLabel
@onready var stats_label: Label = $StatsLabel

var _waiting: bool = false


func _ready() -> void:
	subtitle_panel.visible = false
	note_overlay.visible = false
	prompt_label.visible = false
	objective_label.visible = false
	money_label.visible = false
	weather_label.visible = false
	stats_label.visible = false
	crosshair.visible = true

	_hint_timer = Timer.new()
	_hint_timer.one_shot = true
	add_child(_hint_timer)
	_hint_timer.timeout.connect(_fade_hint)


func set_crosshair_visible(v: bool) -> void:
	crosshair.visible = v


func _fade_hint() -> void:
	var t: Tween = create_tween()
	t.tween_property(controls_hint, "modulate:a", 0.0, 1.5)


func say(who: String, line: String) -> Signal:
	who_label.text = "[%s]" % who
	text_label.text = line
	hint_label.text = "ЛКМ или ПРОБЕЛ — дальше"
	subtitle_panel.visible = true
	note_overlay.visible = false
	_waiting = true
	return advanced


func think(line: String) -> Signal:
	return say("Я", line)


func show_note(text: String) -> Signal:
	note_text.text = text
	note_overlay.visible = true
	subtitle_panel.visible = false
	_waiting = true
	return advanced


func set_prompt(text: String) -> void:
	if text == "":
		prompt_label.visible = false
		return
	prompt_label.text = "[ЛКМ] %s" % text
	prompt_label.visible = true


func clear_prompt() -> void:
	prompt_label.visible = false


func set_date_time(date: String, time: String) -> void:
	if date == "" and time == "":
		date_time_label.visible = false
		return
	date_time_label.text = "%s · %s" % [date, time]
	date_time_label.visible = true


func set_objective(text: String) -> void:
	if text == "":
		objective_label.visible = false
		return
	objective_label.text = "◆ " + text
	objective_label.visible = true


func set_money(rub: int, usd: float = 0.0, eur: float = 0.0) -> void:
	if rub == 0 and usd == 0.0 and eur == 0.0:
		money_label.visible = false
		return
	var parts: Array[String] = ["₽ %d" % rub]
	if usd > 0.0:
		parts.append("$ %.2f" % usd)
	if eur > 0.0:
		parts.append("€ %.2f" % eur)
	money_label.text = "  ".join(parts)
	money_label.visible = true


func set_weather(text: String, temp_c: int = -999) -> void:
	if text == "":
		weather_label.visible = false
		return
	if temp_c == -999:
		weather_label.text = text
	else:
		weather_label.text = "%s  %+d°C" % [text, temp_c]
	weather_label.visible = true


func set_stat_alert(text: String) -> void:
	if text == "":
		stats_label.visible = false
		return
	stats_label.text = text
	stats_label.visible = true


func set_controls_hint(text: String, auto_hide_after: float = 8.0) -> void:
	if _hint_timer != null:
		_hint_timer.stop()
	if text == "":
		controls_hint.visible = false
		return
	controls_hint.text = text
	controls_hint.visible = true
	controls_hint.modulate = Color(0.45, 0.45, 0.45, 0.8)
	if auto_hide_after > 0 and _hint_timer != null:
		_hint_timer.start(auto_hide_after)


func _input(event: InputEvent) -> void:
	if not _waiting:
		return
	var advance: bool = false
	if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
		advance = true
	elif event is InputEventKey and event.pressed and (event.keycode == KEY_SPACE or event.keycode == KEY_ENTER):
		advance = true
	if advance:
		get_viewport().set_input_as_handled()
		subtitle_panel.visible = false
		note_overlay.visible = false
		_waiting = false
		advanced.emit()
