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")
const quest_view_template = preload("res://templates/quest_view.tscn")
const options_template = preload("res://scenes/options.tscn")

signal time_changed(time : float)

@onready var timer : Timer = $Timer
@onready var audioplayer : AudioStreamPlayer = $AudioStreamPlayer

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

func _process(delta: float) -> void:
	time_changed.emit(timer.time_left)
	if Input.is_action_just_pressed("save"):
		Game.test_save()

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 add_quest_view(quest : Quest, visible : bool) -> void:
	var qv : QuestView = quest_view_template.instantiate()
	qv.visible = visible
	%QuestView.add_child(qv)
	qv.setup(quest)
	

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

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_views() -> void:
	var count : int = 0
	for quest in Guild.quests:
		if quest.is_taken():
			count+=1
			add_quest_view(quest, count == 1)
	%QuestViewerButton.disabled = (count == 0)

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

var dragging : bool = false
var drag_pos : Vector2

func _on_drag_region_gui_input(event: InputEvent) -> void:
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_LEFT:
			dragging = event.pressed
			if dragging:
				Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
				drag_pos = event.global_position
			else:
				Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
				Input.warp_mouse(drag_pos)
	elif dragging and event is InputEventMouseMotion:
		var window = get_window()
		window.position += Vector2i(event.screen_relative)

func _on_quest_viewer_button_pressed() -> void:
	%QuestView.visible = !%QuestView.visible


func _on_options_button_pressed() -> void:
	var opt = options_template.instantiate()
	get_tree().root.add_child(opt)
	pass # Replace with function body.
