extends Node3D

# https://www.youtube.com/watch?v=oQZWExfryPI

var player_class = preload("res://scenes/player.tscn")
var npc_class = preload("res://scenes/npc.tscn")

var player1
var player2
var is_two_players = false

var spawn_points : Array

func _ready():
	spawn_points = get_tree().get_nodes_in_group("Spawn Point")
	setup()
	AudioPlayer.play_game_music()

func setup():
	%GameTimer.wait_time = SceneManager.play_time
	%ButtonBack.visible = false
	%FinishPanel.visible = false
	%PausePanel.visible = false
	$GridContainer.columns = SceneManager.player_count
	%Player1VP.visible = true
	%Player2VP.visible = SceneManager.player_count > 1
	
	player1 = player_class.instantiate()
	%Player1VP.get_node("SV").add_child(player1)
	player1.setup(1, %Player1SP.global_position)
	player1.player_number = 1
	player1.connect("score_increased", on_score_increased)
	
	%Player2Score.visible = false

	if (SceneManager.player_count > 1):
		is_two_players = true
		player2 = player_class.instantiate()
		%Player2VP.get_node("SV").add_child(player2)
		player2.setup(2, %Player2SP.global_position)
		player2.connect("score_increased", on_score_increased)
		%Player2Score.visible = true
		
	%StartPanel.visible = true

func _process(delta):
	if Input.is_action_just_pressed("pause"):
		pause_game()

	var start_time_dec = %StartTimer.time_left
	%StartPanel.get_node("StartTimeLabel").text = str(ceil(start_time_dec))
	
	var time_left_dec = %GameTimer.time_left
	%HUD.get_node("TimerLabel").text = "%d:%02d" % [floor(time_left_dec / 60), int(time_left_dec) % 60]


func _on_timer_timeout():
	finish_and_show_winner()


func on_score_increased(player: int, score: int):
	if (player == 1):
		%Player1Score.text = str(score)
	else:
		%Player2Score.text = str(score)
		
func finish_and_show_winner():
	%FinishPanel.visible = true
	%GameTimer.stop()
	var win_label = %FinishPanel.get_node("WinnerLabel")
	
	AudioPlayer.play_finish_music()
	
	player1.status = Player.PLAYER_STATUS.NOT_READY

	if (is_two_players):
		%FinishPanel.get_node("WinnerLabel").visible = false
		
		player2.status = Player.PLAYER_STATUS.NOT_READY

		await get_tree().create_timer(2).timeout
		if (player1.score > player2.score):
			win_label.text = tr("TEXT_PLAYER1_WIN")
		elif (player1.score < player2.score):
			win_label.text = tr("TEXT_PLAYER2_WIN")
		else:
			win_label.text = tr("TEXT_PLAYERS_DRAW")
			
		win_label.visible = true
	else:
		win_label.text = tr("TEXT_FINAL_SCORE") + " : " + str(player1.score)
		
	await get_tree().create_timer(3).timeout
	%ButtonBack.visible = true
	%ButtonBack.grab_focus()


func _on_start_timer_timeout():
	%StartTimer.stop()
	%GameTimer.start()
	%StartPanel.visible = false
	player1.status = Player.PLAYER_STATUS.WALKING

	if (player2):
		player2.status = Player.PLAYER_STATUS.WALKING


func _on_spawn_timer_timeout():
	var pos_index = randi_range(0, spawn_points.size()-1)
	var new_npc = npc_class.instantiate()
	new_npc.global_position = spawn_points[pos_index].global_position
	%NPCS.add_child(new_npc)
	var des_index = randi_range(0, spawn_points.size()-1)
	new_npc.set_destination(spawn_points[des_index].global_position)
	while (des_index == pos_index):
		des_index = randi_range(0, spawn_points.size()-1)
		new_npc.set_destination(spawn_points[des_index].global_position)


func _on_button_back_pressed():
	SceneManager.go_to_main_menu()


func pause_game():
	get_tree().paused = !get_tree().paused
	%PausePanel.visible = get_tree().paused
	if(%PausePanel.visible):
		%ButtonGameResume.grab_focus()


func _on_button_game_resume_pressed():
	pause_game()


func _on_button_game_quit_pressed():
	get_tree().paused = false
	SceneManager.go_to_main_menu()
