class_name QuestBoardEntry extends Button


const quest_window_template = preload("res://templates/quest_window.tscn")
var quest : Quest = null
@onready var tex_icon : TextureRect = %Icon
@onready var name_label : Label = %NameLabel
@onready var status_label : Label = %StatusLabel
var board_window : QuestBoardWindow = null

func setup(quest : Quest) -> void:
	self.quest = quest
	quest.status_changed.connect(_on_quest_status_changed)
	name_label.text = quest.name
	status_label.text = "Available" if !quest.taken else "Unavailable"

func update() -> void:
	status_label.text = "Available" if !quest.taken else "Unavailable"


func _on_quest_status_changed(status: Quest.Status) -> void:
	match(status):
		Quest.Status.OPEN: status_label.text = "Available"
		Quest.Status.TAKEN:queue_free()
		Quest.Status.COMPLETED: queue_free()
		_: status_label.text = "Unavailable"

func _on_pressed() -> void:
	var window = quest_window_template.instantiate()
	window.setup(quest, true)
	window.quest_accepted.connect(board_window._on_quest_accepted)
	add_child(window)
	window.popup_centered()
		
