extends PanelContainer

@onready var printer_name_label: Label = $MarginContainer/MainArea/TopInfo/PrinterLabel
@onready var printer_model_label: Label = $MarginContainer/MainArea/PrinterModelLabel
@onready var progress_bar: ProgressBar = $MarginContainer/MainArea/PrintInfo/ProgressBar
@onready var select_job: OptionButton = $MarginContainer/MainArea/SelectJob

var printer_data: PrinterData
var selected_job: JobData
var selected_job_index: int

func initialize_printer(data: PrinterData) -> void:
	printer_data = data
	#print("Creating bought printer: " + printer_data.printer_manufacturer)
	printer_name_label.text = printer_data.printer_manufacturer
	printer_model_label.text = printer_data.printer_type
	progress_bar.value = 0
	_on_select_job_pressed()

func _on_sell_button_pressed() -> void:
	PlayerData.give_money.emit(printer_data.printer_price * PlayerData.sell_multiplier)
	queue_free()

func _on_select_job_pressed() -> void:
	select_job.clear()
	
	if PlayerData.jobs.is_empty():
		select_job.add_item("No jobs accepted")
	else:
		for job in PlayerData.jobs:
			var item_text = '%s (XP: %d | %d €)' % [job.job_name, job.job_experience, job.job_reward]
			select_job.add_item(item_text)
			
			var current_index = select_job.item_count - 1
			select_job.set_item_metadata(current_index, job)

func _on_start_button_pressed() -> void:
	if select_job.get_item_text(0) == "No jobs accepted":
		print("No job selected")
	else:
		PlayerData.give_money.emit(selected_job.job_reward)
		if PlayerData.jobs.size() == 1:
			PlayerData.jobs.clear()
		else:
			PlayerData.jobs.remove_at(selected_job_index)
		

func _on_select_job_item_selected(index: int) -> void:
	selected_job = select_job.get_item_metadata(index)
	selected_job_index = index
	print(selected_job)
