extends Control

var save_path = "user://map_scores.save"
var map_scores = {}


func LoadMap1() -> void:
	get_tree().change_scene_to_file("res://Game/Maps/map_1.tscn")
	pass # Replace with function body.

func LoadMap2() -> void:
	get_tree().change_scene_to_file("res://Game/Maps/map_2.tscn")
	pass # Replace with function body.

func LoadMap3() -> void:
	get_tree().change_scene_to_file("res://Game/Maps/map_3.tscn")
	pass # Replace with function body.
	
func LoadMap4() -> void:
	get_tree().change_scene_to_file("res://Game/Maps/map_4.tscn")
	pass # Replace with function body.
	
func LoadMap5() -> void:
	get_tree().change_scene_to_file("res://Game/Maps/map_5.tscn")
	pass # Replace with function body.
	
func LoadMap6() -> void:
	get_tree().change_scene_to_file("res://Game/Maps/map_6.tscn")
	pass # Replace with function body.
	
func LoadMap7() -> void:
	get_tree().change_scene_to_file("res://Game/Maps/map_7.tscn")
	pass # Replace with function body.

func LoadMap8() -> void:
	get_tree().change_scene_to_file("res://Game/Maps/map_8.tscn")
	pass # Replace with function body.

func LoadMap9() -> void:
	get_tree().change_scene_to_file("res://Game/Maps/map_9.tscn")
	pass # Replace with function body.
	
func LoadMap10() -> void:
	get_tree().change_scene_to_file("res://Game/Maps/map_10.tscn")
	pass # Replace with function body.
	
	
func _ready() -> void:
	loadData()
	get_node("VBoxContainer/HBoxContainer/first/Button").connect("button_up", LoadMap1)
	get_node("VBoxContainer/HBoxContainer/second/Button").connect("button_up", LoadMap2)
	get_node("VBoxContainer/HBoxContainer/third/Button").connect("button_up", LoadMap3)
	get_node("VBoxContainer/HBoxContainer/fourth/Button").connect("button_up", LoadMap4)
	get_node("VBoxContainer/HBoxContainer/fith/Button").connect("button_up", LoadMap5)
	get_node("VBoxContainer/HBoxContainer/sixth/Button").connect("button_up", LoadMap6)
	get_node("VBoxContainer/HBoxContainer2/seventh/Button").connect("button_up", LoadMap7)
	get_node("VBoxContainer/HBoxContainer2/eighth/Button").connect("button_up", LoadMap8)
	get_node("VBoxContainer/HBoxContainer2/nineth/Button").connect("button_up", LoadMap9)
	get_node("VBoxContainer/HBoxContainer2/tenth/Button").connect("button_up", LoadMap10)
	
	var unlocker = true
	for i in get_node("VBoxContainer").get_children():
		for ii in i.get_children():
			if ii.get_node("Label2").text == "Haven't finished yet":
				if unlocker:
					unlocker = false
					ii.get_node("Button").get_child(0).hide()
					ii.get_node("Button").disabled = false
				else:
					ii.get_node("Button").get_child(0).show()
					ii.get_node("Button").disabled = true
			else:
				ii.get_node("Button").get_child(0).hide()
				ii.get_node("Button").disabled = false
		

func loadData() -> void:
	if FileAccess.file_exists(save_path):
		var file = FileAccess.open(save_path, FileAccess.READ)

		if file:
			map_scores = file.get_var()
			file.close()
			get_node("VBoxContainer/HBoxContainer/first/Label2").text = "Haven't finished yet" if map_scores["Map1"] == 0 else "Scored: " + str(map_scores["Map1"]) + " out of 10"
			get_node("VBoxContainer/HBoxContainer/second/Label2").text = "Haven't finished yet" if map_scores["Map2"] == 0 else "Scored: " + str(map_scores["Map2"]) + " out of 10"
			get_node("VBoxContainer/HBoxContainer/third/Label2").text = "Haven't finished yet" if map_scores["Map3"] == 0 else "Scored: " + str(map_scores["Map3"]) + " out of 10"
			get_node("VBoxContainer/HBoxContainer/fourth/Label2").text = "Haven't finished yet" if map_scores["Map4"] == 0 else "Scored: " + str(map_scores["Map4"]) + " out of 10"
			get_node("VBoxContainer/HBoxContainer/fith/Label2").text = "Haven't finished yet" if map_scores["Map5"] == 0 else "Scored: " + str(map_scores["Map5"]) + " out of 10"
			get_node("VBoxContainer/HBoxContainer/sixth/Label2").text = "Haven't finished yet" if map_scores["Map6"] == 0 else "Scored: " + str(map_scores["Map6"]) + " out of 10"
			get_node("VBoxContainer/HBoxContainer2/seventh/Label2").text = "Haven't finished yet" if map_scores["Map7"] == 0 else "Scored: " + str(map_scores["Map7"]) + " out of 10"
			get_node("VBoxContainer/HBoxContainer2/eighth/Label2").text = "Haven't finished yet" if map_scores["Map8"] == 0 else "Scored: " + str(map_scores["Map8"]) + " out of 10"
			get_node("VBoxContainer/HBoxContainer2/nineth/Label2").text = "Haven't finished yet" if map_scores["Map9"] == 0 else "Scored: " + str(map_scores["Map9"]) + " out of 10"
			get_node("VBoxContainer/HBoxContainer2/tenth/Label2").text = "Haven't finished yet" if map_scores["Map10"] == 0 else "Scored: " + str(map_scores["Map10"]) + " out of 10"

	else:
			map_scores["Map1"] = 0
			map_scores["Map2"] = 0
			map_scores["Map3"] = 0
			map_scores["Map4"] = 0
			map_scores["Map5"] = 0
			map_scores["Map6"] = 0
			map_scores["Map7"] = 0
			map_scores["Map8"] = 0
			map_scores["Map9"] = 0
			map_scores["Map10"] = 0

			save_scores()
			loadData()
			pass


func save_scores():
	var file = FileAccess.open(save_path, FileAccess.WRITE)
	if file:
		file.store_var(map_scores)
		file.close()


func _on_button_pressed() -> void:
	get_tree().change_scene_to_file("res://Game/Menu/main.tscn")
	pass # Replace with function body.
