extends Control

const pawns : Array[PawnBaseData] = [
	preload("res://data/pawns/van_reily.tres"),
	preload("res://data/pawns/lou_riche.tres"),
	preload("res://data/pawns/tico.tres"),
	preload("res://data/pawns/john_bishous.tres"),
	preload("res://data/pawns/abdoll_relin.tres"),
	preload("res://data/pawns/tenrou_ugetsu.tres")
]

const p_colors : Array[Color] = [
	Color.ROYAL_BLUE,
	Color.RED
]


const selector_template = preload("res://templates/pawn_selector.tscn")

@onready var pawn_displays : Array[PawnDisplay] = [
	%P1PawnDisplay,
	%P2PawnDisplay
]

var selector_wait : float = 0
var selected : int = -1
var selectors : Dictionary[int,PawnSelector] = {}
var displays : Dictionary[int, PawnDisplay] = {}
@onready var selector_start : Control = %SelectorStart
@onready var switch_sound : AudioStreamPlayer = %SwitchSound
@onready var select_sound : AudioStreamPlayer = %SelectSound

func _ready() -> void:
	Multiplayer.waiting = true
	var plist
	if Game.is_multiplayer():
		plist = Multiplayer.players.values()
		for i in range(Game.num_players):
			displays[plist[i]] = pawn_displays[i]
			change_display(plist[i], 0)
	else:
		displays[1] = pawn_displays[0]
		change_display(1, 0)
	Multiplayer.all_ready.connect(add_selector, CONNECT_ONE_SHOT)
	Multiplayer.set_player_ready.rpc(Multiplayer.id)
	#Set up each pawn on bottom
	#Highlight P1 pawn as 0
	

func add_selector() -> void:
	spawn_selector.rpc_id(1, Multiplayer.id)

@rpc("call_local","any_peer","reliable")
func spawn_selector(peer_id) -> void:
	var selector = selector_template.instantiate()
	selector.setup(peer_id, p_colors[0] if Multiplayer.is_host() else p_colors[1])
	selector.name = selector.name + str(peer_id)
	selector_start.add_child(selector)
	selector.set_multiplayer_authority(peer_id)

	#print("Character select spawned selector %s for player %d" % [selector.name, Multiplayer.id])
	
func _process(delta : float) -> void:
	if !selectors.has(Multiplayer.id):
		return
	var locked = true
	var pawns_selected : Dictionary[int, PawnBaseData] = {}
	for selector : PawnSelector in selectors.values():
		if !selector.selected:
			locked = false
		else:
			pawns_selected[selector.player_id] = pawns[selector.selection]
	if locked:
		Game.start_level(pawns_selected)

	if selectors[Multiplayer.id].selected != false:
		return
	var selector = selectors[Multiplayer.id]
	if Input.is_action_just_pressed("ui_accept"):
		lock_selector.rpc(selector.player_id)
	if selector_wait > 0:
		selector_wait -= delta
		return
	var input_vector : Vector2

	input_vector = Input.get_vector("west","east","north","south")
	
	var move_dir : Vector2 = Vector2.ZERO
	
	if input_vector.x < 0:
		move_dir.x -= 1
	
	if input_vector.x > 0:
		move_dir.x += 1
	
	if move_dir.x != 0:
		move_selector.rpc(selector.player_id, wrapi(selector.selection + sign(move_dir.x),0, len(pawns)))
		selector_wait = 0.25
	
@rpc("any_peer","call_local","reliable")
func move_selector(peer_id : int, selection : int) -> void:
	var selector : PawnSelector = selectors[peer_id]
	selector.selection = selection 
	change_display(peer_id, selector.selection)
	var v = Vector2(175 * selector.selection, 0)
	selector.position = Vector2(175 * selector.selection, 0)
	switch_sound.play()

@rpc("any_peer","call_local","reliable")
func lock_selector(peer_id : int) -> void:
	var selector : PawnSelector = selectors[peer_id]
	selector.selected = true
	selector.play_animation("selected")
	select_sound.play()
		
func change_display(player : int, selection: int) -> void:
	var pd : PawnDisplay = displays[player]
	pd.set_pawn_name(pawns[selection].name)
	pd.set_portrait(pawns[selection].portrait)
	pd.set_traps(pawns[selection].starting_traps)


func _on_selector_start_child_entered_tree(node: Node) -> void:
	selectors[node.player_id] = node


func _on_selector_spawner_spawned(node: Node) -> void:
	selectors[node.player_id] = node
