extends Control

var config: ConfigManager = preload("res://config_manager.tres")

onready var file_path_line_edit := $"%FilePathLineEdit" as LineEdit
onready var file_path_button := $"%FilePathButton" as Button
onready var theme_path_button := $"%ThemePathButton" as Button
onready var sound_check_box := $"%SoundCheckBox" as CheckBox
onready var open_data_dir_button := $"%OpenDataDirButton" as Button
onready var file_path_open_button := $"%FilePathOpenButton" as Button
onready var attributions_rich_text_label := $"%AttributionsRichTextLabel" as RichTextLabel
onready var file_path_file_dialog := $"%FilePathFileDialog" as FileDialog
onready var theme_path_file_dialog := $"%ThemePathFileDialog" as FileDialog


func _ready() -> void:
	
	# warning-ignore:return_value_discarded
	config.connect("theme_changed", self, "_on_theme_changed")
	
	# warning-ignore:return_value_discarded
	config.connect("time_sheet_loaded", self, "_on_current_file_changed")
	
	# warning-ignore:return_value_discarded
	file_path_button.connect("pressed", self, "_on_file_path_button_pressed")
	
	# warning-ignore:return_value_discarded
	file_path_file_dialog.connect("file_selected", self, "_on_current_file_selected")
	# warning-ignore:return_value_discarded
	file_path_line_edit.connect("text_entered", self, "_on_current_file_selected")
	
	# warning-ignore:return_value_discarded
	theme_path_button.connect("pressed", self, "_on_theme_path_button_pressed")
	
	# warning-ignore:return_value_discarded
	theme_path_file_dialog.connect("file_selected", self, "_on_new_theme_selected")
	
	theme_path_file_dialog.hide()
	file_path_file_dialog.hide()
	
	sound_check_box.pressed = config.sound_fx_on
	
	# warning-ignore:return_value_discarded
	sound_check_box.connect("toggled", self, "_on_sound_toggle")

	# warning-ignore:return_value_discarded
	attributions_rich_text_label.connect("meta_clicked", self, "_on_attributions_rich_text_label_meta_clicked")
	
	# warning-ignore:return_value_discarded
	open_data_dir_button.connect("pressed", self, "_on_open_data_dir_button_pressed")
# warning-ignore:return_value_discarded
	file_path_open_button.connect("pressed", self, "_on_file_path_open_button_pressed")
	_on_current_file_changed()


func _on_current_file_changed() -> void:
	file_path_file_dialog.current_path = config.current_timesheet_file_path
	file_path_file_dialog.current_dir = config.current_timesheet_file_path.get_base_dir()
	file_path_line_edit.text = config.current_timesheet_file_path


func _on_theme_changed() -> void:
	theme = config.theme


func _on_file_path_button_pressed() -> void:
	file_path_file_dialog.popup_centered()


func _on_current_file_selected(new_file: String) -> void:
	config.set_current_file = new_file


func _on_sound_toggle(is_on: bool) -> void:
	config.sound_fx_on = is_on


func _on_theme_path_button_pressed() -> void:
	theme_path_file_dialog.popup_centered()


func _on_new_theme_selected(new_theme_path: String) -> void:
	config.set_theme_path = new_theme_path


func _on_attributions_rich_text_label_meta_clicked(meta) -> void:
	if meta is String:
		# warning-ignore:return_value_discarded
		OS.shell_open(meta)


func _on_open_data_dir_button_pressed() -> void:
	# warning-ignore:return_value_discarded
	OS.shell_open(OS.get_user_data_dir())


func _on_file_path_open_button_pressed() -> void:
	# warning-ignore:return_value_discarded
	OS.shell_open(config.current_timesheet_file_path.get_base_dir())
