extends Control


@onready var editor_button = $"menu/editor_button"
@onready var play_button = $"menu/play_button"
@onready var set_path_button = $"menu/set_path_button"
@onready var message_label = $"menu/message_label"
@onready var message_label2 = $"menu/message_label2"

@onready var level_manager = $"/root/LevelManager"

@onready var settings = $"/root/Settings"



# Called when the node enters the scene tree for the first time.
func _ready():
	self.connect_buttons()
	
	self.update_level_path()
	
	if !OS.has_feature("editor"):
		self.set_path_button.hide()
		
	



func connect_buttons():

	self.editor_button.connect("pressed", Callable(self, "open_editor"))
	self.play_button.connect("pressed", Callable(self, "play_game"))
	self.set_path_button.connect("pressed", Callable(self, "set_path_pressed"))
	%DirSelect/FileDialog.connect("dir_selected", Callable(self, "_on_file_dialog_dir_selected"))
	
func update_level_path():
	var is_editor = OS.has_feature("editor")
	
	var path = settings.get_option("editor_level_path")
	if is_editor:
		if path == "":		
			message_label.text = "no level path set"
		else:
			level_manager.editor_level_path = path
			message_label.text = "level path set to: " + str(path)
		
		
	level_manager.load_levels_from_dir()

func set_path_pressed():
	print ("set path called")
	%DirSelect.show_file_dialog()

func _on_file_dialog_dir_selected(dir):
	settings.set_option("editor_level_path", dir + str("/"))
	self.update_level_path()
	

func open_editor():
	
	#var level_name = ""

	#if level_name == "" || level_name == null:
	#	message_label.text = str("open editor - name field empty")
	self.get_tree().change_scene_to_file("res://scenes/level_editor/editor.tscn")


	#if not self.level_manager.level_exists(level_name):
	#	message_label.text = str("open editor - level: ") + str(level_name) + str(" not found")
	#	return
	
	#message_label.text = str("open editor - loading level: ") + str(level_name)
	#level_manager.selected_level = level_name
	#self.get_tree().change_scene_to_file("res://scenes/level_editor/editor.tscn")
	#self.execute_success(level_name, "load")


func play_game():
	
	
	self.get_tree().change_scene_to_file("res://scenes/ui/level_select_menu.tscn")

	#self.execute_success(level_name, "save")
