extends Control

signal back_to_main_menu

@onready var master_slider = $SettingsContainer/MasterVolumeContainer/MasterVolumeSlider
@onready var music_slider = $SettingsContainer/MusicVolumeContainer/MusicVolumeSlider
@onready var sfx_slider = $SettingsContainer/SFXVolumeContainer/SFXVolumeSlider
@onready var language_selector = $SettingsContainer/LanguageContainer/LanguageSelector

@export var settings_manager: Node = SettingsManager
@export var localization_manager: Node = LocalizationManager

var language_codes = []

func _ready():
	add_to_group("localizable")
	print("SettingsMenu ready")
	setup_language_selector()

	var master_callback = _on_volume_slider_changed.bind("master_volume")
	if not master_slider.value_changed.is_connected(master_callback):
		master_slider.value_changed.connect(master_callback)

	var music_callback = _on_volume_slider_changed.bind("music_volume")
	if not music_slider.value_changed.is_connected(music_callback):
		music_slider.value_changed.connect(music_callback)

	var sfx_callback = _on_volume_slider_changed.bind("sfx_volume")
	if not sfx_slider.value_changed.is_connected(sfx_callback):
		sfx_slider.value_changed.connect(sfx_callback)

	if not language_selector.item_selected.is_connected(Callable(self, "_on_language_selector_item_selected")):
		language_selector.item_selected.connect(_on_language_selector_item_selected)

	_update_controls_from_settings()
	update_text()

func _update_controls_from_settings():
	master_slider.value = settings_manager.get_setting("master_volume")
	music_slider.value = settings_manager.get_setting("music_volume")
	sfx_slider.value = settings_manager.get_setting("sfx_volume")

	var current_lang = settings_manager.get_setting("language")
	var lang_index = language_codes.find(current_lang)
	if lang_index >= 0:
		language_selector.selected = lang_index

func _on_volume_slider_changed(value, setting_key):
	settings_manager.set_setting(setting_key, value)

func _exit_settings():
	print("Exiting settings")
	settings_manager.save_settings()
	back_to_main_menu.emit()

func _input(event):
	if event.is_action_pressed("ui_cancel") or event.is_action_pressed("ui_menu_toggle"):
		print("ESC pressed in settings")
		_exit_settings()
		get_viewport().set_input_as_handled()

func _on_back_button_pressed():
	AudioManager.play_ui_click()
	print("Back button pressed")
	_exit_settings()

func setup_language_selector():
	var languages_data = settings_manager.get_languages_data()
	if languages_data.has("languages"):
		language_selector.clear()
		language_codes.clear()

		for lang_code in languages_data.languages.keys():
			language_codes.append(lang_code)
			var display_name = languages_data.languages[lang_code]["display_name"]
			language_selector.add_item(display_name)

		var current_lang = settings_manager.get_setting("language")
		var lang_index = language_codes.find(current_lang)
		if lang_index >= 0:
			language_selector.selected = lang_index

func _on_language_selector_item_selected(index: int):
	if index < language_codes.size():
		var selected_lang = language_codes[index]
		settings_manager.set_setting("language", selected_lang)
		print("Language changed to: ", selected_lang)
		localization_manager.change_language(selected_lang)

func update_text():
	$SettingsContainer/SettingsTitle.text = tr("settings_title")
	$SettingsContainer/MasterVolumeContainer/MasterVolume.text = tr("master_volume")
	$SettingsContainer/MusicVolumeContainer/MusicVolume.text = tr("music_volume")
	$SettingsContainer/SFXVolumeContainer/SFXVolume.text = tr("sfx_volume")
	$SettingsContainer/LanguageContainer/LanguageLabel.text = tr("language")
	$BackButtonContainer/BackButton.text = tr("back")


func _on_reset_setting_button_pressed() -> void:
	AudioManager.play_ui_click()
	print("Resetting settings")
	settings_manager.reset_settings_to_defaults()
	_update_controls_from_settings()
	localization_manager.change_language(settings_manager.get_setting("language"))
