extends Node2D
class_name DroneManager

var spawning_drone : bool = false
var spawning_type : String = ""

var director_drones : Array = [] # List of all director drones in the world

@onready var drone_controls = %DroneControls
@onready var spawned_drones_container = get_node("SpawnedDrones")
@onready var cursor = preload("res://resources/cursors/launch_drone.png")

# Drones!
@onready var director_drone = preload("res://entities/DirectorDrone.tscn")
@onready var dancer_drone = preload("res://entities/DancerDrone.tscn")
@onready var distractor_drone = preload("res://entities/DistractorDrone.tscn")
@onready var collector_drone = preload("res://entities/CollectorDrone.tscn")

func _ready() -> void:
	Log.pr("Drone Manager Ready...")

## Function to detect right click event 
func _input(event) -> void:
	if spawning_drone:
		if (event is InputEventMouseButton && event.button_index == MOUSE_BUTTON_RIGHT && event.pressed):
			Log.pr("Cancelling placement of drone")
			cancel_spawning()
		if (event is InputEventMouseButton && event.button_index == MOUSE_BUTTON_LEFT && event.pressed):
			spawn_drone(spawning_type)
			cancel_spawning()

func spawn_drone(drone_type : String) -> void:
	Log.pr("This function will put a " + drone_type + " drone in the world")

	var new_drone = null
	# Create a new instance of the drone
	if drone_type == "director":
		new_drone = director_drone.instantiate()
		# new_drone.visit_order = spawned_drones_container.get_child_count()
	elif drone_type == "dancer":
		new_drone = dancer_drone.instantiate()
	elif drone_type == "distractor":
		new_drone = distractor_drone.instantiate()
	elif drone_type == "collector":
		new_drone = collector_drone.instantiate()
	else:
		Log.pr("Unknown drone type: " + drone_type)
	
	spawned_drones_container.add_child(new_drone)
	new_drone.position = get_viewport().get_mouse_position()

	if drone_type == "director":
		# Set this drones visit order to the next in line
		new_drone.visit_order = director_drones.size() + 1
		# Update the director drone list
		update_director_drone_list()

func place_drone(drone_type : String) -> void:
	if !spawning_drone:
		Input.set_custom_mouse_cursor(cursor, Input.CURSOR_ARROW, Vector2(32, 32))
		drone_controls.disable_buttons()
		Log.pr("Placing " + drone_type + "...")
		spawning_drone = true
		spawning_type = drone_type

func cancel_spawning() -> void:
	Input.set_custom_mouse_cursor(null)
	drone_controls.reset_button_focus()
	drone_controls.enable_buttons()
	spawning_drone = false
	spawning_type = ""

func _on_spawn_director_pressed() -> void:
	place_drone("director")
		
func _on_spawn_collector_pressed() -> void:
	place_drone("collector")

func _on_spawn_distractor_pressed() -> void:
	place_drone("distractor")

func _on_spawn_dancer_pressed() -> void:
	place_drone("dancer")

func update_director_drone_list():
	director_drones.clear()
	for drone in spawned_drones_container.get_children():
		if drone is DirectorDrone:
			director_drones.append(drone)

	Log.pr(director_drones.size())

func get_director(drone_number : int) -> DirectorDrone:
	for drone in director_drones:
		if drone.visit_order == drone_number:
			return drone
	return null

func get_next_director(current_director_number : int) -> DirectorDrone:
	for drone in director_drones:
		if drone.visit_order == current_director_number + 1:
			return drone
	return null

func get_previous_director(current_director_number : int) -> DirectorDrone:
	for drone in director_drones:
		if drone.visit_order == current_director_number - 1:
			return drone
	return null

## For now this just returns the first collector drone it finds 
## This will need to be updated to return the closest collector drone potentially?
func get_collector():
	for drone in spawned_drones_container.get_children():
		if drone is CollectorDrone:
			return drone
	return null