extends Control

@export var jobs_array: Array[JobData]
@export var job_limit: int = 5
@export var debug: bool = false

@onready var debug_xp: Button = $VBoxContainer/TopBar/VBoxContainer/MarginContainer/HBoxContainer/DebugXP
@onready var debug_money: Button = $VBoxContainer/TopBar/VBoxContainer/MarginContainer/HBoxContainer/DebugMoney
@onready var debug_button: Button = $VBoxContainer/TopBar/VBoxContainer/MarginContainer/HBoxContainer/DebugButton
@onready var jobs_tab_button: Button = $VBoxContainer/MainArea/RightColumn/TabSwitcher/TabButtons/JobsTabButton

const PRINTER = preload("uid://33ctlyrpibs3")

func _ready() -> void:
	check_debug()

	_on_xp_changed(PlayerData.xp)
	_on_level_changed(PlayerData.level)
	_on_money_changed(PlayerData.money)
	
	PlayerData.xp_changed.connect(_on_xp_changed)
	PlayerData.level_changed.connect(_on_level_changed)
	PlayerData.money_changed.connect(_on_money_changed)
	GameData.job_generated.connect(_on_job_generated)
	GameData.job_interact.connect(_on_job_interact)
	GameData.printer_bought.connect(_on_printer_bought)

# --- Experience, Level and Money update ---
func _on_xp_changed(xp) -> void:
	%Experience.value = xp
func _on_level_changed(level) -> void:
	%Level.text = 'Level: %d' % level
func _on_money_changed(money) -> void:
	%Money.text = 'Money: %.2f €' % money

func _on_job_generated(job: Node) -> void:
	%JobContainer.add_child(job)

func _on_job_interact() -> void:
	jobs_tab_button.text = 'Jobs (%d / %d)' % [GameData.current_job_count, GameData.MAX_JOBS]

func _on_printer_bought(new_printer: Node) -> void:
	%PrinterContainer.add_child(new_printer)

#region Debug
func check_debug() -> void:
	debug_xp.visible = false
	debug_money.visible = false
	if debug:
		debug_xp.visible = true
		debug_money.visible = true

func _on_debug_money_pressed() -> void:
	PlayerData.give_money.emit(100)

func _on_debug_xp_pressed() -> void:
	var xp = randf() * 100
	PlayerData.give_experience.emit(xp)

func _on_debug_button_pressed() -> void:
	toggle_debug()

func toggle_debug() -> void:
	if debug == true:
		debug = false
	else:
		debug = true
	check_debug()
#endregion
