# Example of how to use the ValueStepper component in any scene
extends Control

@onready
var language_stepper: ValueStepper = $VBoxContainer/Examples/LanguageContainer/LanguageStepper
@onready
var difficulty_stepper: ValueStepper = $VBoxContainer/Examples/DifficultyContainer/DifficultyStepper
@onready
var resolution_stepper: ValueStepper = $VBoxContainer/Examples/ResolutionContainer/ResolutionStepper
@onready var custom_stepper: ValueStepper = $VBoxContainer/Examples/CustomContainer/CustomStepper

# Example of setting up custom navigation
var navigable_steppers: Array[ValueStepper] = []
var current_stepper_index: int = 0


func _ready():
	DebugManager.log_info("ValueStepper example ready", "Example")

	# Setup navigation array
	navigable_steppers = [language_stepper, difficulty_stepper, resolution_stepper, custom_stepper]

	# Connect to value change events
	for stepper in navigable_steppers:
		if not stepper.value_changed.is_connected(_on_stepper_value_changed):
			stepper.value_changed.connect(_on_stepper_value_changed)

	# Setup custom stepper with custom values
	var themes = ["Light", "Dark", "Blue", "Green", "Purple"]
	var theme_values = ["light", "dark", "blue", "green", "purple"]
	custom_stepper.setup_custom_values(theme_values, themes)
	custom_stepper.data_source = "theme"  # For better logging

	# Highlight first stepper
	_update_stepper_highlighting()


func _input(event: InputEvent):
	# Example navigation handling
	if event.is_action_pressed("move_up"):
		_navigate_steppers(-1)
		get_viewport().set_input_as_handled()
	elif event.is_action_pressed("move_down"):
		_navigate_steppers(1)
		get_viewport().set_input_as_handled()
	elif event.is_action_pressed("move_left"):
		_handle_stepper_input("move_left")
		get_viewport().set_input_as_handled()
	elif event.is_action_pressed("move_right"):
		_handle_stepper_input("move_right")
		get_viewport().set_input_as_handled()


func _navigate_steppers(direction: int):
	current_stepper_index = (current_stepper_index + direction) % navigable_steppers.size()
	if current_stepper_index < 0:
		current_stepper_index = navigable_steppers.size() - 1
	_update_stepper_highlighting()
	DebugManager.log_info("Stepper navigation: index " + str(current_stepper_index), "Example")


func _handle_stepper_input(action: String):
	if current_stepper_index >= 0 and current_stepper_index < navigable_steppers.size():
		var stepper = navigable_steppers[current_stepper_index]
		if stepper.handle_input_action(action):
			AudioManager.play_ui_click()


func _update_stepper_highlighting():
	for i in range(navigable_steppers.size()):
		navigable_steppers[i].set_highlighted(i == current_stepper_index)


func _on_stepper_value_changed(new_value: String, new_index: int):
	DebugManager.log_info(
		"Stepper value changed to: " + new_value + " (index: " + str(new_index) + ")", "Example"
	)
	# Handle value change in your scene
	# For example: apply settings, save preferences, update UI, etc.


# Example of programmatically setting values
func _on_reset_to_defaults_pressed():
	AudioManager.play_ui_click()
	language_stepper.set_current_value("en")
	difficulty_stepper.set_current_value("normal")
	resolution_stepper.set_current_value("1920x1080")
	custom_stepper.set_current_value("dark")
	DebugManager.log_info("Reset all steppers to defaults", "Example")
