extends PanelContainer

const quest_info_window_template = preload("res://templates/quest_info_window.tscn")

var _enabled: bool
var enabled: bool:
	get:
		return _enabled
	set(value):
		enable(value)
		
var quest : Quest
@onready var status_label = %StatusLabel

func enable(en: bool) -> void:
	_enabled = en
	if en:
		mouse_filter=Control.MOUSE_FILTER_STOP
	else:
		mouse_filter=Control.MOUSE_FILTER_IGNORE

func setup(qst : Quest) -> void:
	quest = qst
	quest.status_changed.connect(_on_quest_status_changed)
	%NameLabel.text = quest.name
	#TODO: Add star rating
	#%LevelLabel.text = str(data.level)
	%StatusLabel.text = ("Taken ()" % [quest.questor.full_name()]) if quest.taken else "OPEN"
	#TODO: Change portrait

func _on_gui_input(event: InputEvent) -> void:
	var evt = event as InputEventMouseButton
	if evt and evt.button_index == MOUSE_BUTTON_LEFT and evt.pressed:
		var wnd = quest_info_window_template.instantiate()
		add_child(wnd)
		wnd.setup(quest)
		wnd.popup_centered()

func _on_quest_status_changed(status: Quest.Status) -> void:
	match(status):
		Quest.Status.OPEN: status_label.text = "OPEN"
		Quest.Status.COMPLETED: queue_free()
		Quest.Status.IN_PROGRESS: status_label.text = "IN PROGRESS"
		_: status_label.text = "Taken"
