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 rules : RulesComponent = get_parent().get_node("RulesComponent") as RulesComponent
@onready var beehive : Beehive = get_parent().get_node("Beehive")
@onready var flowers : Flowers = get_parent().get_node("Flowers")
@onready var dog : Dog = null
@onready var drone_controls : HBoxContainer = %DroneControls
@onready var ui_controls : UIComponent = get_parent().get_node("UiComponent")
@onready var spawned_drones_container : Node = get_node("SpawnedDrones")

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


func _ready() -> void:
	if !rules.game_rules.collector_enabled:
		%SpawnCollector.visible = false
	if !rules.game_rules.dancer_enabled:
		%SpawnDancer.visible = false
	if !rules.game_rules.director_enabled:
		%SpawnDirector.visible = false
	if !rules.game_rules.distractor_enabled:
		%SpawnDistractor.visible = false

	# dog = get_parent().get_node("Dog")
	if get_parent().has_node("Dog"):
		dog = get_parent().get_node("Dog")

## Function to detect right click event 
func _input(event : InputEvent) -> 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 : Node = 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()
		# Hide the dancer button 
		%SpawnDancer.visible = false
		GameState.game_start() # Start the game when the first dancer is placed
	elif drone_type == "distractor":
		new_drone = distractor_drone.instantiate()
	elif drone_type == "collector":
		new_drone = collector_drone.instantiate()
		# Hide the collector button 
		%SpawnCollector.visible = false
	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()

	reset_node_highlights()

	GameState.add_drone() ## Increase drone count on the game state

func place_drone(drone_type : String) -> void:
	if !spawning_drone:
		CursorMgr.place()
		drone_controls.disable_buttons()
		Log.pr("Placing " + drone_type + "...")
		spawning_drone = true
		GameState.placing_drone = true # This should probably be rolled into the above line
		spawning_type = drone_type

func cancel_spawning() -> void:
	CursorMgr.reset_cursor()
	drone_controls.reset_button_focus()
	drone_controls.enable_buttons()
	spawning_drone = false
	GameState.placing_drone = false
	spawning_type = ""
	reset_node_highlights()

func _on_spawn_director_pressed() -> void:
	ui_controls.show_help_text("Help_Drone_Placement_Director")
	place_drone("director")
		
func _on_spawn_collector_pressed() -> void:
	flowers.show_outline()
	ui_controls.show_help_text("Help_Drone_Placement_Collector")
	place_drone("collector")

func _on_spawn_distractor_pressed() -> void:
	if dog:
		dog.show_outline()
	place_drone("distractor")

func _on_spawn_dancer_pressed() -> void:
	beehive.show_outline()
	ui_controls.show_help_text("Help_Drone_Placement_Dancer")
	place_drone("dancer")

func _on_spawn_director_mouse_entered() -> void:
	reset_node_highlights()
	ui_controls.show_help_text("Help_Drone_Placement_Director")

func _on_spawn_collector_mouse_entered() -> void:
	reset_node_highlights()
	flowers.show_outline()
	ui_controls.show_help_text("Help_Drone_Placement_Collector")

func _on_spawn_distractor_mouse_entered() -> void:
	reset_node_highlights()
	if dog:
		dog.show_outline()
	ui_controls.show_help_text("Help_Drone_Placement_Distractor")

func _on_spawn_dancer_mouse_entered() -> void:
	reset_node_highlights()
	beehive.show_outline()
	ui_controls.show_help_text("Help_Drone_Placement_Dancer")


## Function to clear highlights when a button is mouse exited, if we arent spawning a drone 
func _on_button_mouse_exited() -> void:
	## Update this to trigger something that hides help messages and resets highlights after a second or so
	#if !spawning_drone:
	#	reset_node_highlights.call_deferred()
	pass

func update_director_drone_list() -> void:
	director_drones.clear()
	var x : int = 1
	for drone : Node in spawned_drones_container.get_children():
		if drone is DirectorDrone:
			drone.visit_order = x
			director_drones.append(drone)
			x += 1

	Log.pr(director_drones.size())

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

func get_next_director(current_director_number : int) -> DirectorDrone:
	for drone : Node 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 : Node 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() -> CollectorDrone:
	for drone : Node in spawned_drones_container.get_children():
		if drone is CollectorDrone:
			return drone
	return null

func reset_node_highlights() -> void:
	ui_controls.hide_help_text()
	beehive.hide_outline()
	flowers.hide_outline()

	if dog:
		dog.hide_outline()