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
@onready var status_label: Label = $MarginContainer/MainArea/TopInfo/StatusLabel
@onready var time_left_label: Label = $MarginContainer/MainArea/PrintInfo/TimeLeftLabel
@onready var start_button: Button = $MarginContainer/MainArea/Buttons/StartButton

var printer_data: PrinterData
var selected_job: JobData
var selected_job_index: int
var is_printing: bool = false
var time_left: float

func initialize_printer(data: PrinterData) -> void:
	printer_data = data
	printer_name_label.text = printer_data.printer_manufacturer
	printer_model_label.text = printer_data.printer_type
	time_left_label.visible = false
	time_left = 0
	set_status()
	progress_bar.visible = false
	progress_bar.value = 0
	_on_select_job_pressed()

func _process(delta) -> void:
	if is_printing:
		select_job.disabled = true
		progress_bar.visible = true
		time_left_label.visible = true
		set_status()
		var total_time = selected_job.job_time / printer_data.printer_speed
		var progress = (total_time - time_left) / total_time
		time_left -= delta
		progress_bar.value = progress
		time_left_label.text = 'Time left: %s' % calculate_time_left(time_left)
		
		if time_left <= 0:
			PlayerData.give_money.emit(selected_job.job_reward)
			PlayerData.give_experience.emit(selected_job.job_experience)
			if PlayerData.jobs.size() == 1:
				PlayerData.jobs.clear()
			else:
				PlayerData.jobs.remove_at(selected_job_index)
			reset_status()

func reset_status() -> void:
	is_printing = false
	_on_select_job_pressed()
	set_status()
	time_left_label.visible = false
	progress_bar.visible = false
	progress_bar.value = 0
	select_job.disabled = false

func set_status() -> void:
	if is_printing:
		status_label.text = 'Status: Printing'
	else:
		status_label.text = 'Status: Free'

func calculate_time_left(time) -> String:
	var minutes: int = 0
	
	if time >= 60:
		while time >= 60:
			minutes += 1
			time -= 60
		return '%d m %.2f s' % [minutes, time]
	else:
		return '%.2f s' % time

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:
	#if selected_job != null:
		#PlayerData.jobs.append(selected_job)
	
	select_job.clear()
	if PlayerData.jobs.is_empty():
		select_job.add_item("No jobs accepted")
		start_button.disabled = true
	else:
		start_button.disabled = false
		for job in PlayerData.jobs:
			var item_text = '%s (XP: %d | %d € | %s)' % [job.job_name, job.job_experience, job.job_reward, calculate_time_left(job.job_time / printer_data.printer_speed)]
			select_job.add_item(item_text)
			
			var current_index = select_job.item_count - 1
			select_job.set_item_metadata(current_index, job)
		selected_job = select_job.get_item_metadata(0)
		selected_job_index = 0

func _on_start_button_pressed() -> void:
	if select_job.get_item_text(0) == "No jobs accepted":
		print("No job selected")
	else:
		time_left = selected_job.job_time / printer_data.printer_speed
		progress_bar.value = 0
		is_printing = true
	
	_on_select_job_pressed()

func _on_select_job_item_selected(index: int) -> void:
	selected_job = select_job.get_item_metadata(index)
	selected_job_index = index
	#PlayerData.jobs.remove_at(index)
