class_name QuestorSprite extends QuestSprite

@onready var levelup_banner_template = preload("res://templates/levelup_banner.tscn")

var data : Adventurer = null
var banner_lag : float

func _ready() -> void:
	if data:		
		life = data.life
		max_life = data.max_life
		energy = data.energy
		max_energy = data.max_energy
		level = data.level
		exp = data.exp
		stats = StatBlock.copy(data.stats)
		gold = data.gold
		data.changed.connect(_on_questor_changed)
	super._ready()
	
func _process(delta) -> void:
	if banner_lag > 0:
		banner_lag -= delta

func set_animation(anim_name : String) -> void:
	anim_player.play(anim_name)

func setup(adv : Adventurer) -> void:
	data = adv
	life = data.life
	max_life = data.max_life
	energy = data.energy
	max_energy = data.max_energy
	level = data.level
	exp = data.exp
	stats = StatBlock.copy(data.stats)
	gold = data.gold
	adv.quest_sprite = self
	data.levelled.connect(_on_level_up)

func show_levelup_banner(lagged : bool = false) -> void:
	if !lagged and banner_lag > 0:
		var tween = create_tween()
		tween.tween_interval(banner_lag)
		tween.tween_callback(show_levelup_banner.bind(true))
		banner_lag += .75
		return
	var banner = levelup_banner_template.instantiate()
	banner.position = Vector2.ZERO
	banner_offset.add_child(banner)
	banner_lag = .75


func check_levelup() -> void:
	data.gain_exp(exp)



func _on_level_up() -> void:
	show_levelup_banner()

func _on_questor_changed() -> void:
	pass

func attack(target : QuestSprite) -> void:
	print("Attack by %s to %s" % [name, target.name])
	hitting.connect(spear_hit.bind(target), CONNECT_ONE_SHOT)
	anim_player.play("spear attack")

func spear_hit(target : QuestSprite) -> void:
	target.take_damage(self, 5)
