## Reads the config, sets values. Acts a singleton because it proxies a const
## file path.
class_name ConfigManager extends Resource

const CONFIG_PATH := "user://settings.cfg"


var _config := ConfigFile.new()
var _watcher: FileWatcher

###############################################################################
#
# SIGNAL
#

signal time_sheet_loaded

func emit_loaded() -> void:
	emit_signal("time_sheet_loaded")


###############################################################################
#
# TIMESHEET FILE LOADING AND PARSING
#

var timesheet: TimeSheet setget ,get_timesheet


func get_timesheet() -> TimeSheet:
	if timesheet == null:
		timesheet = _load_timesheet(get_current_timesheet_file_path())
	return timesheet


func _load_timesheet(path: String) -> TimeSheet:
	var new_timesheet := TimeSheet.restore(path)
	if new_timesheet == null:
		return null
	_watcher = FileWatcher.new()
	_watcher.file_name = path
	# warning-ignore:return_value_discarded
	_watcher.connect("file_changed", self, "reload_timesheet")
	_watcher.start()
	return new_timesheet


func reload_timesheet() -> void:
	var new_timesheet = _load_timesheet(get_current_timesheet_file_path())
	if new_timesheet == null:
		printerr("failed to load new timesheet")
		return
	timesheet = new_timesheet
	emit_loaded()


###############################################################################
#
# TIMESHEET FILE PATH
#

var current_timesheet_file_path: String = "" setget set_current_timesheet_file_path, get_current_timesheet_file_path


func set_current_timesheet_file_path(value: String) -> void:
	timesheet = _load_timesheet(value)
	if timesheet == null:
		return
	current_timesheet_file_path = value
	_config.set_value("MAIN", "file", value)
	emit_loaded()
	save()


func get_current_timesheet_file_path() -> String:
	var _default_path := OS.get_system_dir(OS.SYSTEM_DIR_DOCUMENTS, true).plus_file("mouse_timer.csv")
	return _config.get_value("MAIN", "file", _default_path)


###############################################################################
#
# THEME FILE LOADING AND PARSING
#


var theme: Theme setget , get_theme


func get_theme() -> Theme:
		if theme == null:
			theme = ResourceLoader.load(theme_file_path, "Theme")
		return theme


###############################################################################
#
# THEME FILE PATH
#

signal theme_changed
var theme_file_path: String = "" setget set_theme_file_path, get_theme_file_path


func set_theme_file_path(value: String) -> void:
	var new_theme: Theme = ResourceLoader.load(value, "Theme")
	if new_theme != null:
		theme = new_theme
	theme_file_path = value
	_config.set_value("MAIN", "theme", value)
	emit_signal("theme_changed")
	save()


func get_theme_file_path() -> String:
	return _config.get_value("MAIN", "theme", preload("res://assets/default_theme.theme").resource_path)


###############################################################################
#
# SOUND OPTION
#

var sound_fx_on: bool = true setget set_sound_fx_on, get_sound_fx_on

func set_sound_fx_on(value: bool) -> void:
		sound_fx_on = value
		_config.set_value("MAIN", "sound_fx_on", value)
		save()

func get_sound_fx_on() -> bool:
		return _config.get_value("MAIN", "sound_fx", true)


###############################################################################
#
# SOME SETTINGS CACHE
#

var current_task_name := "" setget set_current_task_name, get_current_task_name


func set_current_task_name(value: String) -> void:
	current_task_name = value
	_config.set_value("CACHE", "current_task_name", value)
	save()


func get_current_task_name() -> String:
	return _config.get_value("CACHE", "current_task_name", "")


var last_window_position := Vector2() setget set_last_window_position, get_last_window_position


func set_last_window_position(value: Vector2) -> void:
	last_window_position = value
	_config.set_value("CACHE", "last_window_position", value)
	save()


func get_last_window_position() -> Vector2:
	return _config.get_value("CACHE", "last_window_position", OS.get_screen_size()/2)


###############################################################################
#
# BOOTSTRAP
#


func _init() -> void:
	# warning-ignore:return_value_discarded
	_config.load(CONFIG_PATH)


func save() -> void:
	# warning-ignore:return_value_discarded
	_config.save(CONFIG_PATH)
