class_name SliderLabel
extends Label


@export var slider: Slider: set = set_slider
@export var pad_zeros: int = 1
@export var pad_decimals: int = 2
@export var prefix: String = ""
@export var suffix: String = ""
@export_group("Remap Value")
@export var do_remap: bool = false
@export var remap_initial_min: float = 0.0
@export var remap_initial_max: float = 1.0
@export var remap_output_min: float = 0.0
@export var remap_output_max: float = 1.0


func set_slider(new_slider: Slider) -> void:
	if new_slider == slider:
		return

	if is_instance_valid(slider):
		slider.value_changed.disconnect(_on_value_changed)

	if is_instance_valid(new_slider):
		new_slider.value_changed.connect(_on_value_changed)
		if not new_slider.is_node_ready():
			await new_slider.ready
		_on_value_changed(new_slider.get_value()) # Apply the value of the new slider.

	slider = new_slider


func _on_value_changed(new_value: float) -> void:
	var value: float = new_value
	if do_remap:
		value = remap(value, remap_initial_min, remap_initial_max, remap_output_min, remap_output_max)

	var new_text: String = str(value).pad_zeros(pad_zeros).pad_decimals(pad_decimals)
	set_text(str(prefix, new_text, suffix))
