extends Node

# - Signals
signal job_generated(job: Node)
signal job_interact
signal printer_bought(printer: Node)

# - Variables - Code managed
var job_spawn_timer: Timer
var jobs_array: Array[JobData]
var current_job_count: int = 0

# - Scenes
const JOB_CARD = preload("uid://dvfcie8hqcb0")
const PRINTER = preload("uid://33ctlyrpibs3")

# - Constants - Settings
# Jobs
const JOB_SPAWN_WAIT: float = 2.0
const MAX_JOBS: int = 4


func _ready() -> void:
	load_all_jobs("res://Resources/Jobs/")
	
	# Create timer for job spawning
	job_spawn_timer = Timer.new()
	add_child(job_spawn_timer)
	job_spawn_timer.wait_time = JOB_SPAWN_WAIT
	job_spawn_timer.one_shot = false
	job_spawn_timer.timeout.connect(_on_job_spawn_timer_timeout)
	job_spawn_timer.start()

func _on_shop_printer_purchase_req(printer_data: PrinterData) -> void:
	if PlayerData.try_purchase(printer_data.printer_price):
		var new_printer = PRINTER.instantiate()
		printer_bought.emit(new_printer)
		new_printer.initialize_printer(printer_data)


#region Job generation
func _on_job_spawn_timer_timeout() -> void:
	print("Spawn timer over")
	#generate_job()
	draw_job()

func generate_job() -> void:
	if not current_job_count >= MAX_JOBS:
		var random_job: JobData = jobs_array.pick_random()
		current_job_count += 1
		var new_job = JOB_CARD.instantiate()
		job_generated.emit(new_job)
		new_job.initialize_job(random_job)
		new_job.job_accepted.connect(_on_job_accepted_pressed)
		job_interact.emit()

func draw_job() -> void:
	if not current_job_count >= MAX_JOBS:
		var job: JobData = generate_random_job()
		current_job_count += 1
		var new_job = JOB_CARD.instantiate()
		job_generated.emit(new_job)
		new_job.initialize_job(job)
		new_job.job_accepted.connect(_on_job_accepted_pressed)
		job_interact.emit()

func generate_random_job() -> JobData:
	var new_job = JobData.new()
	var random_job: JobData = jobs_array.pick_random()
	var amount = randf_range(1, PlayerData.max_order_amount)
	
	new_job.job_name = random_job.job_name
	new_job.job_reward = random_job.job_reward * amount
	new_job.job_experience = random_job.job_experience * amount
	new_job.job_difficulty = random_job.job_difficulty
	new_job.job_amount = roundf(amount)
	
	print('Random amount: %.2f' % amount)
	print('Job amount: %.2f' % new_job.job_amount)
	print('Generated new job: %s (%d € | %d xp | %d pcs)' % [new_job.job_name, new_job.job_reward, new_job.job_experience, new_job.job_amount])
	return new_job

func _on_job_accepted_pressed(accepted_job) -> void:
	print("Job accepted")
	PlayerData.jobs.append(accepted_job.job_data)
	print(PlayerData.jobs[0].job_name)
	GameData.current_job_count -= 1
	accepted_job.queue_free()
	job_interact.emit()
#endregion
#region Utility - Load job resources
func load_all_jobs(path: String) -> void:
	var dir = DirAccess.open(path)
	
	if dir:
		dir.list_dir_begin()
		var file_name = dir.get_next()
		
		while file_name != "":
			if not dir.current_is_dir() and file_name.get_extension() == "tres":
				var full_path = path + file_name
				var job_resource = load(full_path) as JobData
				
				if job_resource:
					jobs_array.append(job_resource)
					#print(job_resource)
			file_name = dir.get_next()
		dir.list_dir_end()
		#print(jobs_array)
	else:
		print("No files found on path")
#endregion
