extends Control
class_name IngameControls

signal menu_button_clicked
signal settings_button_clicked
signal save_button_clicked
signal save_button_released
signal save_timer_incremented(value: int)
signal save_timer_reached

var save_button_held_down: bool = false
var save_timer: float = 0.0
var time_to_save: float = 0.8 # second

func _process(delta) -> void:
	if save_button_held_down:
		if save_timer >= time_to_save:
			save_timer_reached.emit()
			save_timer = 0.0
			
			# Save the game
			SaveManager.save_game()
			
			save_button_held_down = false
		else:
			save_timer_incremented.emit(ceili((save_timer / time_to_save) * 100))
			save_timer += delta

func _on_menu_button_pressed() -> void:
	menu_button_clicked.emit()

func _on_settings_button_pressed() -> void:
	settings_button_clicked.emit()

# the save function should work as follows:
# a percentage sign with 0% should be shown
# the player has to hold down the save button
# during holding down, the percentage increases until 100%
# a finishing animation plays, the game is saved and the countdown disappears again
func _on_save_button_button_down() -> void:
	save_button_held_down = true
	save_button_clicked.emit()

func _on_save_button_button_up() -> void:
	save_button_held_down = false
	save_button_released.emit()
	save_timer = 0.0

func disable_save_button() -> void:
	%SaveButton.disabled = true

func enable_save_button() -> void:
	%SaveButton.disabled = false
