extends VBoxContainer

signal changed
signal leave_stage

var has_stage:bool = false:
	set(stage):
		has_stage = stage
		if is_node_ready() and has_stage:
			screenreader_check.grab_focus()

@export var is_in_beginning: bool = false

@onready var reduce_motion_check: CheckBox  = %ReduceMotion
@onready var screenreader_check: CheckBox = %Screenreader
@onready var disable_rendering_check: CheckBox = %DisableRendering
@onready var simplify_controls_check: CheckBox = %SimplyifyControls
@onready var font_picker: OptionButton = %FontSeettings
@onready var subtitle_picker: OptionButton = %SubtitleSettings
@onready var scale_slider: HSlider = %UIScaleSlider
@onready var accept_or_continue: Button = %SaveButton
@onready var back_button: Button = %BackButton

func _ready() -> void:
	if is_in_beginning:
		%SaveButton.text = "save and continue"
	else:
		%SaveButton.text = "save and exit"
	
	update_ui_from_state()

	reduce_motion_check.pressed.connect(func(value): State.reduce_motion = value)
	screenreader_check.pressed.connect(func(value): State.screen_reader_enabled = value)
	disable_rendering_check.pressed.connect(func(value): State.rendering_disabled = value)
	simplify_controls_check.pressed.connect(func(value): State.use_simplified_navigation = value)
	font_picker.item_selected.connect(func(value): State.font_style = value)
	subtitle_picker.item_selected.connect(func(value): State.subtitles = value)
	scale_slider.drag_ended.connect(_on_scale_slider_dragged)
	

func _on_scale_slider_dragged(update:bool):
	if update:
		State.ui_scaling = scale_slider.value

func update_ui_from_state():
	reduce_motion_check.button_pressed = State.reduce_motion
	screenreader_check.button_pressed = State.screen_reader_enabled
	disable_rendering_check.button_pressed = State.rendering_disabled
	simplify_controls_check.button_pressed = State.use_simplified_navigation
	font_picker.selected = State.font_style
	subtitle_picker.selected = State.subtitles
	scale_slider.value = State.ui_scaling
	
func _on_exit_button_pressed() -> void:
	leave_stage.emit()
	State.save_settings()
	State.leave_stage(self)
