class_name GamePanel extends MarginContainer

const notice_template = preload("res://templates/notice_panel.tscn")
const quest_progress_bar_template = preload("res://templates/quest_progress_bar.tscn")

signal time_changed(time : float)

@onready var timer : Timer = $Timer

func _ready() -> void:
	Game.panel = self

func _process(delta: float) -> void:
	time_changed.emit(timer.time_left)

func add_quest_progress_bar(quest : Quest) -> void:
	var qpb : QuestProgressBar = quest_progress_bar_template.instantiate()
	%QuestList.add_child(qpb)
	qpb.setup(quest)
	#TODO: Change the hero portrait to match
	if quest.steps > 1:
		for i in range(quest.steps-1):
			qpb.add_waypoint(float(i) / quest.steps, quest.step_messages[i])
			

func switch_panel(active : bool) -> void:
	%OpenShift.visible = active
	%WorkingShift.visible = !active
	%Timer.start(300 if active else 1500)

func _on_show_quests_pressed() -> void:
	%QuestProgressList.visible = !%QuestProgressList.visible

func _on_end_shift_pressed() -> void:
	Game.confirm_end_shift()

func _on_visitor_spawned(remaining : int, total : int) -> void:
	update_visitor_count(total - remaining, total)

func connect_visitor_spawner(spawner : VisitorSpawner) -> void:
	spawner.visitor_spawned.connect(_on_visitor_spawned)
	update_visitor_count(spawner.total_visitors - spawner.visitors_remaining, spawner.total_visitors)
	
func update_visitor_count(current : int, total : int) -> void:
	%OpenList/VisitorsLabel.text = "Visitors: %d/%d" % [current, total]

func populate_quest_bars() -> void:
	
	var count : int = 0
	for quest in Guild.quests:
		if quest.is_taken():
			count+=1
			add_quest_progress_bar(quest)
	%ShowQuestsButton.disabled = (count == 0)

func notice(msg : String, time : float) -> void:
	var ntc : NoticePanel = notice_template.instantiate()
	%Notices.add_child(ntc)
	ntc.message = msg
	ntc.duration = time
	
