class_name ProgressTracker
extends Object

signal progress_update()
signal step_change(step: int)
signal complete()
signal step_complete()

var _current_step: ProgressStepTracker = null
var _current_step_index: int = -1
var _steps_count: int

func _init(step_count: int = 0) -> void:
	_steps_count = step_count
	self.substep_complete.connect(self, "_on_substep_complete")

func _on_step_complete() -> void:
	if _current_step_index == _steps_count:
		self.complete.emit()
		return

# next_step: should be called before the step is actually started
func next_step(step_name: String = "") -> ProgressStepTracker:
	_current_step_index += 1
	if _current_step_index > _steps_count:
		push_error("Number of steps tracked is larger than number of steps used!")
	step_change.emit(_current_step_index)

	self._current_step = ProgressStepTracker.new(self, step_name)
	self.progress_update.emit()

	return self._current_step
	
# step_complete: returns the progress of all steps
func get_step_progress() -> float:
	return float(_current_step_index) / float(_steps_count)

# get_total_progress_percentage: returns the progress of all steps and substeps
func get_total_progress_percentage() -> float:
	return self.get_step_progress()

# get_progress_data: returns a dictionary with data about the current progress for the UI
func get_progress_data() -> Dictionary:
	return {
		"step_name": self._current_step.get_step_name(),
		"message": self._current_step.get_message(),
		"substeps": self._current_step.get_substeps(),
		"current_substep": self._current_step.get_substep_index(),
		"total_progress":self.get_total_progress_percentage()		
	}
