class_name TimeSheet


var source_path := ""
var entries := []
# warning-ignore:integer_division
var _last_update := Time.get_ticks_msec() / 1000

## Loads the data file
func load_file() -> bool:
	var file := File.new()
	var success := file.open(source_path, File.READ)
	if success != OK:
		success = file.open(source_path, File.WRITE)
		if success != OK:
			printerr("Failed to open file %s"%[ProjectSettings.globalize_path(source_path)])
			return false
		return true
	
	while not file.eof_reached():
		var line := file.get_csv_line()
		if line.size() == 0 or "".join(line).length() == 0:
			continue
		if not TimeEntry.is_csv_line_valid(line):
			push_warning("CSV Line `%s` is not conform"%[",".join(line)])
			continue
		var entry := TimeEntry.new().from_csv_line(line)
		entries.append(entry)
	file.close()
	
	entries.sort_custom(self, "_sort_entries")
	
	return true


func _sort_entries(a: TimeEntry, b: TimeEntry) -> bool:
	return a.name < b.name


func get_active_entry_from_name(task_name: String) -> TimeEntry:
	for _entry in entries:
		var current_time_entry := _entry as TimeEntry
		if current_time_entry.name == task_name and not current_time_entry.is_closed:
			return current_time_entry
	return null


## Adds a new time entry to the tree and to the data file
func add_entry(entry_name: String) -> TimeEntry:
	var current_entry := TimeEntry.new().start_recording()
	current_entry.name = entry_name
	current_entry.is_closed = false
	var file := File.new()
	var success := file.open(source_path, File.READ_WRITE)
	if success != OK:
		printerr("Could not open file")
		return null
	file.seek_end()
	entries.append(current_entry)
	file.store_csv_line(current_entry.to_csv_line())
	return current_entry


func stop_entry(entry_name: String, do_save := true) -> bool:
	for _entry in entries:
		var current_time_entry := _entry as TimeEntry
		if current_time_entry.name == entry_name and not current_time_entry.is_closed:
			current_time_entry.close()
			if do_save:
				save()
			return true
	return false


func toggle_entry(entry_name: String, do_save := true) -> void:
	if stop_entry(entry_name, do_save):
		return
	else:
		# warning-ignore:return_value_discarded
		add_entry(entry_name)


func update() -> void:
	# warning-ignore:integer_division
	var current_time := Time.get_ticks_msec() / 1000
	if current_time == _last_update:
		return
	_last_update = current_time
	for entry in entries:
		var time_entry := entry as TimeEntry
		if time_entry.is_closed == false:
			time_entry.update()


func save() -> void:
	var file := File.new()
	var success := file.open(source_path, File.WRITE)
	if success != OK:
		printerr("Could not open file")
		return
	for time_entry in entries:
		file.store_csv_line(time_entry.to_csv_line())


func make_items_tree() -> TimeEntryTreeItem:
	var tree := TimeEntryTreeItem.new()
	for entry_index in entries.size():
		var entry := entries[entry_index] as TimeEntry
		var parts := entry.name.split("/")
		var repo: TimeEntryTreeItem = tree.get_child(parts, true)
		repo.append(entry)
	return tree


static func restore(file_path: String) -> TimeSheet:
	var timesheet = load("res://scripts/time_sheet.gd").new()
	timesheet.source_path = file_path
	var success: bool = timesheet.load_file()
	if success:
		return timesheet
	return null
