extends Node2D

@onready var score_label := $Score
@onready var maintenance_label := $Maintenance
@onready var crew_containter := $Crew/VBoxContainer

@onready var new_crew_button := $NewCrewButton

var crew_icon := load("res://utils/crew_member.tscn")


### 	 ACCESSORS 	 ###






### 	 BUILT-IN 	 ###

func _ready() -> void:
	GLOBALS.score -= GLOBALS.mainteance
	score_label.text = str(GLOBALS.score)
	GLOBALS.mainteance = 10
	maintenance_label.text = str(GLOBALS.mainteance)
	for member in GLOBALS.crew:
		add_crew_icon(member)
	check_money()




### 	 VIRTUAL 	 ###





### 	 LOGIC 	 ###

func add_crew() -> void:
	var new_crew = GLOBALS.new_crew.duplicate(true)
	new_crew.call_sign = GLOBALS.get_new_callsign()
	GLOBALS.crew.append(new_crew)
	add_crew_icon(new_crew)
	check_money()


func add_crew_icon(crew_member) -> void:
	var new_member = crew_icon.instantiate()
	crew_containter.add_child(new_member)
	new_member.set_crew(crew_member)
	GLOBALS.mainteance += 40 + GLOBALS.level
	maintenance_label.text = str(GLOBALS.mainteance)


func start_game() -> void:
	GLOBALS.score -= GLOBALS.mainteance
	GLOBALS.mainteance = 0
	get_tree().change_scene_to_file("res://scenes/level.tscn")


func check_money() -> void:
	if GLOBALS.score < 100:
		new_crew_button.set_deferred("disabled", true)

### 	 SIGNAL-RESPONSE 	 ###


func _on_new_crew_button_pressed() -> void:
	GLOBALS.score -= 100
	score_label.text = str(GLOBALS.score)
	add_crew()


func _on_start_pressed() -> void:
	start_game()
