extends Node2D

@onready var _tab_bar: TabBar = $MarginContainer/VBoxContainer/TabBar
@onready var _tab_container: TabContainer = $MarginContainer/VBoxContainer/TabContainer
@onready var _music_volume_slider: HSlider = $MarginContainer/VBoxContainer/TabContainer/Audio/AudioVBox/MusicVolumeGroup/MusicVolumeSlider
@onready var _music_volume_value: Label = $MarginContainer/VBoxContainer/TabContainer/Audio/AudioVBox/MusicVolumeGroup/MusicVolumeValue
@onready var _music_mute_checkbox: CheckBox = $MarginContainer/VBoxContainer/TabContainer/Audio/AudioVBox/MusicVolumeGroup/MusicMuteCheckBox
@onready var _sfx_volume_slider: HSlider = $MarginContainer/VBoxContainer/TabContainer/Audio/AudioVBox/SfxVolumeGroup/SfxVolumeSlider
@onready var _sfx_volume_value: Label = $MarginContainer/VBoxContainer/TabContainer/Audio/AudioVBox/SfxVolumeGroup/SfxVolumeValue
@onready var _sfx_mute_checkbox: CheckBox = $MarginContainer/VBoxContainer/TabContainer/Audio/AudioVBox/SfxVolumeGroup/SfxMuteCheckBox
@onready var _menu_music: AudioStreamPlayer = get_tree().get_root().get_node_or_null("MenuMusic")
@onready var _menu_sfx: AudioStreamPlayer = get_tree().get_root().get_node_or_null("MenuSfx")

func _ready() -> void:
	_tab_bar.tab_changed.connect(_on_tab_bar_tab_changed)
	_tab_container.tab_changed.connect(_on_tab_container_tab_changed)
	_tab_container.current_tab = _tab_bar.current_tab
	_music_volume_slider.value_changed.connect(_on_music_volume_changed)
	_music_mute_checkbox.toggled.connect(_on_music_mute_toggled)
	_sfx_volume_slider.value_changed.connect(_on_sfx_volume_changed)
	_sfx_mute_checkbox.toggled.connect(_on_sfx_mute_toggled)
	_sync_audio_controls()
	_register_focus_sounds()

func _input(event):
	if event.is_action_pressed("ui_cancel"):
		get_tree().change_scene_to_file("uid://b4k81taauef4q")

func _on_tab_bar_tab_changed(tab_index: int) -> void:
	if _tab_container.current_tab != tab_index:
		_tab_container.current_tab = tab_index

func _on_tab_container_tab_changed(tab_index: int) -> void:
	if _tab_bar.current_tab != tab_index:
		_tab_bar.current_tab = tab_index

func _sync_audio_controls() -> void:
	var value: float = 0.7
	var muted: bool = false
	if _menu_music:
		if _menu_music.has_method("get_user_volume"):
			value = _menu_music.get_user_volume()
		if _menu_music.has_method("is_user_muted"):
			muted = _menu_music.is_user_muted()
	_apply_audio_control_state(_music_volume_slider, _music_mute_checkbox, _music_volume_value, value, muted)

	var sfx_value: float = 0.7
	var sfx_muted: bool = false
	if _menu_sfx:
		if _menu_sfx.has_method("get_user_volume"):
			sfx_value = _menu_sfx.get_user_volume()
		if _menu_sfx.has_method("is_user_muted"):
			sfx_muted = _menu_sfx.is_user_muted()
	_apply_audio_control_state(_sfx_volume_slider, _sfx_mute_checkbox, _sfx_volume_value, sfx_value, sfx_muted)

func _on_music_volume_changed(value: float) -> void:
	if _menu_music and _menu_music.has_method("set_user_volume"):
		_menu_music.set_user_volume(value)
	_update_volume_label(_music_volume_value, value, _music_mute_checkbox.button_pressed)

func _on_music_mute_toggled(pressed: bool) -> void:
	if _menu_music and _menu_music.has_method("set_user_muted"):
		_menu_music.set_user_muted(pressed)
	_music_volume_slider.editable = not pressed
	_update_volume_label(_music_volume_value, _music_volume_slider.value, pressed)

func _on_sfx_volume_changed(value: float) -> void:
	if _menu_sfx and _menu_sfx.has_method("set_user_volume"):
		_menu_sfx.set_user_volume(value)
	_update_volume_label(_sfx_volume_value, value, _sfx_mute_checkbox.button_pressed)

func _on_sfx_mute_toggled(pressed: bool) -> void:
	if _menu_sfx and _menu_sfx.has_method("set_user_muted"):
		_menu_sfx.set_user_muted(pressed)
	_sfx_volume_slider.editable = not pressed
	_update_volume_label(_sfx_volume_value, _sfx_volume_slider.value, pressed)

func _apply_audio_control_state(slider: HSlider, checkbox: CheckBox, value_label: Label, value: float, muted: bool) -> void:
	slider.set_block_signals(true)
	slider.value = value
	slider.set_block_signals(false)
	slider.editable = not muted
	checkbox.set_block_signals(true)
	checkbox.button_pressed = muted
	checkbox.set_block_signals(false)
	_update_volume_label(value_label, value, muted)

func _update_volume_label(value_label: Label, value: float, muted: bool) -> void:
	if muted:
		value_label.text = "Muted"
	else:
		var percent: int = int(round(value * 100.0))
		value_label.text = str(percent) + "%"

func _register_focus_sounds() -> void:
	_connect_focus_recursive(self)

func _connect_focus_recursive(node: Node) -> void:
	if node is Control:
		var control: Control = node
		if control.focus_mode != Control.FOCUS_NONE:
			if not control.is_connected("focus_entered", Callable(self, "_on_menu_item_focus")):
				control.focus_entered.connect(_on_menu_item_focus)
		if control.has_signal("mouse_entered") and (control is BaseButton or control.focus_mode != Control.FOCUS_NONE):
			if not control.is_connected("mouse_entered", Callable(self, "_on_menu_item_focus")):
				control.mouse_entered.connect(_on_menu_item_focus)
	for child in node.get_children():
		_connect_focus_recursive(child)

func _on_menu_item_focus() -> void:
	if MenuSfx:
		MenuSfx.play_hover()
