extends Control

@export var world_path : NodePath = NodePath()

@onready var game_running_widgets : Control = $'GameRunningWidgets'
@onready var game_startup_widgets : Control = $'GameStartupWidgets'
@onready var game_finished_widgets : Control = $'GameFinishedWidgets'

@onready var player1_score_label : Label = $'GameRunningWidgets/Player1Widgets/ScoreLabel'
@onready var player1_energy_progressbar : ProgressBar = $'GameRunningWidgets/Player1Widgets/EnergyProgressBar'
@onready var player2_score_label : Label = $'GameRunningWidgets/Player2Widgets/ScoreLabel'
@onready var player2_energy_progressbar : ProgressBar = $'GameRunningWidgets/Player2Widgets/EnergyProgressBar'
@onready var player2_widgets : Control = $'GameRunningWidgets/Player2Widgets'

@onready var time_left_label : Label = $'GameRunningWidgets/TimeWidgets/TimeLeftLabel'

@onready var winner_label : Label = $'GameFinishedWidgets/VBoxContainer/WinnerLabel'
@onready var player1_stats_points : Label = $'GameFinishedWidgets/VBoxContainer/HBoxContainer/Player1Stats/Points'
@onready var player2_stats_points : Label = $'GameFinishedWidgets/VBoxContainer/HBoxContainer/Player2Stats/Points'

var world : World = null

# Called when the node enters the scene tree for the first time.
func _ready():
	world = get_node(world_path)

	if world.players.size() == 1:
		player2_widgets.visible = false

func _process(_delta):
	if world.game_state == World.GameState.Startup:
		game_startup_widgets.visible = true
		game_running_widgets.visible = false
		game_finished_widgets.visible = false
	elif world.game_state == World.GameState.Running:
		game_startup_widgets.visible = false
		game_running_widgets.visible = true
		game_finished_widgets.visible = false
		
		var minutes = floor(world.time_left / 60.)
		var seconds = fmod(floor(world.time_left), 60)
		var milliseconds = floor((world.time_left - floor(world.time_left)) * 10)
		
		time_left_label.text = str(minutes) + ":" + str(seconds) + ":" + str(milliseconds)
		
		player1_score_label.add_theme_color_override("font-color", world.players[0].color)
		if player2_widgets.visible:
			player2_score_label.add_theme_color_override("font-color", world.players[1].color)
	elif world.game_state == World.GameState.Finished:
		game_startup_widgets.visible = false
		game_running_widgets.visible = false
		game_finished_widgets.visible = true
	
	player1_score_label.text = str(world.players[0].score)
	player1_energy_progressbar.value = world.players[0].energy
	player1_stats_points.text = str(world.players[0].score)
	var winner_player : Player = world.players[0]
	
	if player2_widgets.visible:
		player2_score_label.text = str(world.players[1].score)
		player2_energy_progressbar.value = world.players[1].energy
		player2_stats_points.text = str(world.players[1].score)
		if world.players[1].score > world.players[0].score:
			winner_player = world.players[1]

	winner_label.add_theme_color_override("font_color", winner_player.color)
	winner_label.text = winner_player.name + " wins!"
	

func _input(event):
	if event.is_action_pressed("ui_accept"):
		if world.game_state == World.GameState.Startup:
			world.game_state = World.GameState.Running
	if event.is_action_pressed("ui_menu"):			
		if world.game_state == World.GameState.Finished:
			world.game_state = World.GameState.Startup

