extends Node


const blinder_template = preload("res://templates/blinder.tscn")
const one_shot_template = preload("res://templates/one_shot.tscn")

const vs_man_level  = preload("res://scenes/test_level.tscn")
const pawn_select_scene = preload("res://scenes/character_select.tscn")

var player : PawnController
var hud : HUD
var level : Level
var multiplayer_game : bool = false
var num_players : int = 1
var pawns_selected : Dictionary[int, PawnBaseData] = {}
var level_synced : Dictionary[int, bool] = {}
var net_test : bool #TODO: Remove later

func _ready() -> void:
	var args : Dictionary = {}
	for arg in OS.get_cmdline_args():
		if arg.begins_with("--"):
			var flag = arg.lstrip("--")
			args[flag]=true
	if args.has("net_test"):
		net_test = true
	

func setup_player(pc : PawnController) -> void:
	var is_local = false
	Game.level.pawns[pc.id] = pc
	if pc.id == Multiplayer.id:
		is_local = true
	if is_local:
		player = pc
	if hud:
		hud.register_pawn(pc,is_local)

func is_multiplayer() -> bool:
	return multiplayer_game


func start_level(pawns : Dictionary[int, PawnBaseData]) -> void:
	if !Multiplayer.is_host():
		return
	pawns_selected = pawns
	for i in Multiplayer.players.values():
		level_synced[i] = false
	level_synced[1] = true
	switch_scene(vs_man_level)

func switch_scene(packed_scene : PackedScene, spawn : bool = true) -> void:
	var blinder = blinder_template.instantiate() as TextureRect
	var tween = create_tween()
	blinder.self_modulate.a = 0
	add_child(blinder)
	tween.tween_property(blinder, "self_modulate:a", 1, .15)
	if spawn:
		var spawner = get_tree().get_first_node_in_group("level_spawner") as MultiplayerSpawner
		tween.tween_callback(spawner.add_child.bind(packed_scene.instantiate(), true))
		var current_scene = get_tree().get_first_node_in_group("scenes")
		if current_scene:
			tween.tween_callback(current_scene.queue_free)
			current_scene.process_mode=Node.PROCESS_MODE_DISABLED
	else:	
		tween.tween_callback(get_tree().change_scene_to_packed.bind(packed_scene))
	tween.tween_property(blinder, "self_modulate:a", 0, .15)
	tween.tween_callback(blinder.queue_free)

@rpc("call_local", "any_peer", "reliable")
func start_pawn_select(mplayer : bool) -> void:
	var setup = get_tree().get_first_node_in_group("scenes")
	multiplayer_game = mplayer
	setup.queue_free()
	num_players = Multiplayer.players.size()
	var blinder = blinder_template.instantiate() as TextureRect
	var tween = create_tween()
	blinder.self_modulate.a = 0
	add_child(blinder)
	tween.tween_property(blinder, "self_modulate:a", 1, .15)
	if Multiplayer.is_server():
		var pawn_select = pawn_select_scene.instantiate()
		var spawner = get_tree().get_first_node_in_group("level_spawner") as MultiplayerSpawner
		tween.tween_callback(spawner.add_child.bind(pawn_select))
	tween.tween_property(blinder, "self_modulate:a", 0, .15)
	tween.tween_callback(blinder.queue_free)
	
@rpc("call_local", "any_peer", "reliable")
func start_vs_man() -> void:
	var setup = get_tree().get_first_node_in_group("scenes")
	setup.queue_free()
	if Multiplayer.is_server():
		var level_scene = vs_man_level.instantiate()
		var spawner = get_tree().get_first_node_in_group("level_spawner") as MultiplayerSpawner
		spawner.add_child(level_scene)
	

@rpc("call_remote", "any_peer", "reliable")
func level_spawned(peer_id : int) -> void:
	if !Multiplayer.is_host():
		return
	level_synced[peer_id] = true
	check_level_ready()


func check_level_ready() -> void:
	var ready : bool = true
	for id in level_synced:
		if level_synced[id] == false:
			ready = false
	if ready:
		level.setup()
	
func oneshot(stream : AudioStream) -> void:
	var shot = one_shot_template.instantiate()
	shot.stream = stream
	if level:
		level.add_child(shot)
	else:
		add_child(shot)
