class_name QuestProgressBar extends Control

const waypoint_template = preload("res://templates/waypoint.tscn")
var length : float
var waypoints : Array[Waypoint] = []
@onready var hero_offset : Vector2 = %Hero.position
@onready var hero : HeroIcon = %Hero
@onready var heroLabel : Label = %HeroLabel
@onready var startpoint : Endpoint = %Start
@onready var endpoint : Endpoint = %End
@onready var bar : TextureProgressBar = $ProgressBar
@onready var path : Control = %Path
@onready var speech_bubble = %SpeechBubble
var quest : Quest = null
var current_event : Quest.Event = null
var time_elapsed : float = 0
var next_waypoint = 0
#signal value_changed(value : float)
#var min_value
#var max_value
#var step
#var page
#var value
#var exp_edit
#var rounded
#var allow_greater
func _ready() -> void:
	length = path.size.x
	hero.position = hero_offset + Vector2(length * bar.value / bar.max_value, 0)

func _process(delta: float) -> void:
	if time_elapsed < quest.length:
		if current_event != null:
			if current_event.type != Quest.Event.Type.COMBAT:
				current_event.process(delta)
		else:
			time_elapsed += delta
			progress_quest()

func generate_waypoints():
	if len(waypoints) > 0:
		for wp in waypoints:
			wp.queue_free()
	waypoints = []
	
	#Include the "end" in the count
	var num = quest.num_events()+1
	for i in range(1,num):
		var pct : float = i / float(num)
		add_waypoint( pct, quest.events[i-1])
	
func add_waypoint(pct : float, event : Quest.Event):
		var wp = waypoint_template.instantiate()
		waypoints.append(wp)
		wp.percent = pct
		#TODO: Make events matter
		wp.event = event
		%Waypoints.add_child(wp)
		wp.global_position = %Waypoints.global_position + Vector2(pct * length - 16, -9)
		if bar.value / bar.max_value >= pct:
			wp.fill = true

func update_waypoints(value : float) -> void:
	startpoint.fill = (bar.value / bar.max_value >= startpoint.percent)
	for wp : Waypoint in waypoints:
		wp.fill = (bar.value / bar.max_value >= wp.percent)
	endpoint.fill = (bar.value / bar.max_value >= endpoint.percent)

func start_event(event : Quest.Event, offset : float) -> void:
	current_event = event
	current_event.completed.connect(_on_event_complete)
	event.time_elapsed = offset

func setup(quest : Quest) -> void:
	self.quest = quest
	heroLabel.text = quest.questor.full_name()
	time_elapsed = 0
	#Generate the waypoints
	generate_waypoints()
	#TODO: Change the hero's portrait
	bar.value = quest.progress

func progress_quest() -> void:
	var pct = time_elapsed / quest.length
	if next_waypoint < len(waypoints) and pct >= waypoints[next_waypoint].percent:
		start_event(waypoints[next_waypoint].event, (pct - waypoints[next_waypoint].percent) * quest.length)
		pct = waypoints[next_waypoint].percent
		waypoints[next_waypoint].blink(true)
	bar.value = clampf(pct, 0, bar.max_value)
	hero.position = hero_offset + Vector2(length * bar.value / bar.max_value, 0)
	if time_elapsed >= quest.length:
		quest.complete()
	update_waypoints(bar.value)


func _on_event_complete() -> void:
	#TODO: Show event message!
	speech_bubble.show_message("Event complete!")
	waypoints[next_waypoint].blink(false)
	next_waypoint += 1
	current_event = null
