class_name HighScores extends Node

@onready var loaded_data = SaveStateResource.new()

func save():
	var save_data : SaveStateResource = SaveStateResource.new()
	
	save_data.level_1_score = loaded_data.level_1_score
	save_data.level_2_score = loaded_data.level_2_score
	save_data.level_3_score = loaded_data.level_3_score
	save_data.level_4_score = loaded_data.level_4_score
	save_data.level_5_score = loaded_data.level_5_score
	save_data.level_6_score = loaded_data.level_6_score

	save_data.total_bees_killed = loaded_data.total_bees_killed
	save_data.total_honey_collected = loaded_data.total_honey_collected

	ResourceSaver.save(save_data, "user://savedata.tres")

func load():
	var save_data : SaveStateResource = load("user://savedata.tres")

	if save_data:
		loaded_data.level_1_score = save_data.level_1_score
		loaded_data.level_2_score = save_data.level_2_score
		loaded_data.level_3_score = save_data.level_3_score
		loaded_data.level_4_score = save_data.level_4_score
		loaded_data.level_5_score = save_data.level_5_score
		loaded_data.level_6_score = save_data.level_6_score

		loaded_data.total_bees_killed = save_data.total_bees_killed
		loaded_data.total_honey_collected = save_data.total_honey_collected

func add_honey(honey : int):
	loaded_data.total_honey_collected += honey

func add_dead_bees(dead : int):
	loaded_data.total_bees_killed += dead

func update_highscore(level : int, points : int) -> void:
	var current_highscore = get_level_highscore(level)

	if points > current_highscore:
		match level:
			1:
				loaded_data.level_1_score = points
			2:
				loaded_data.level_2_score = points
			3:
				loaded_data.level_3_score = points
			4:
				loaded_data.level_4_score = points
			5:
				loaded_data.level_5_score = points
			6:
				loaded_data.level_6_score = points
	pass

func debug_save_high_score():
	loaded_data.level_1_score = 3000
	save()

func debug_output():
	Log.pr("High Scores", loaded_data.level_1_score)

func get_level_highscore(level : int) -> int:
	match level:
		1:
			return loaded_data.level_1_score
		2:
			return loaded_data.level_2_score
		3:
			return loaded_data.level_3_score
		4:
			return loaded_data.level_4_score
		5:
			return loaded_data.level_5_score
		6:
			return loaded_data.level_6_score

	return 0