@icon("res://assets/editor/64x64/fc1515.png")
extends Node2D
class_name RangedWeaponComponent

@export var stats: StatsComponent
@export var basic_projectile: PackedScene = preload("res://assets/projectiles/basic_projectile.tscn")

@onready var modifier_manager = $ModifierManager
@onready var projectile_scene: PackedScene = basic_projectile

var can_fire: bool = true
var cooldown: Timer

func _init() -> void:
	cooldown = Timer.new()
	add_child(cooldown)
	cooldown.one_shot = true

func _ready() -> void:
	Log.pr("RangedWeaponComponent initialized")
	modifier_manager.set_stats(stats)
	modifier_manager.connect("modifier_added", _on_modifier_added)
	modifier_manager.connect("modifier_removed", _on_modifier_removed)
	cooldown.connect("timeout", _on_fire_timer_timeout)

func add_modifier(modifier: Modifier) -> void:
	modifier_manager.add_modifier(modifier)

func remove_modifier(modifier_id: String) -> void:
	modifier_manager.remove_modifier(modifier_id)

func fire(direction: Vector2, target_position: Vector2) -> void:
	if !can_fire:
		return

	spawn_projectile(global_position, direction, target_position)
	
	can_fire = false
	cooldown.start(1 / stats.get_stat("ranged.attack_rate"))

func set_projectile_scene() -> void:
	projectile_scene = basic_projectile
	modifier_manager.check_and_call("set_projectile_scene", [self])

func spawn_projectile(spawn_position: Vector2, spawn_direction: Vector2, target_position: Vector2) -> void:
	Log.pr("Spawning projectile")
	modifier_manager.check_and_call("spawn_projectile", [self])

	## TODO: Handle multiple shots per fire 

	var projectile = projectile_scene.instantiate()
	projectile.global_position = spawn_position
	projectile.target_position = target_position
	projectile.speed = 200 # stats.get_stat("projectile_speed")
	projectile.damage = 10 # stats.get_stat("damage")
	projectile.lifetime = 2 # stats.get_stat("projectile_lifetime")
	projectile.source_weapon = self
	var size = stats.get_stat("ranged.projectile_size")
	projectile.set_projectile_scale(Vector2(size, size))
	if get_tree() and get_tree().get_root():
		get_tree().get_root().add_child(projectile)

func _on_modifier_added(_modifier: Modifier) -> void:
	set_projectile_scene()

func _on_modifier_removed(_modifier: Modifier) -> void:
	set_projectile_scene()

func _on_fire_timer_timeout() -> void:
	can_fire = true
