extends Control

@onready var _options_popup: Window = $OptionsPopup
@onready var _music_slider: HSlider = $OptionsPopup/VBox/MusicSlider
@onready var _sfx_slider: HSlider = $OptionsPopup/VBox/SFXSlider
@onready var _host_port_popup: Window = $HostPortPopup
@onready var _host_port_input: LineEdit = $HostPortPopup/VBox/PortInput
@onready var _host_status: Label = $HostPortPopup/VBox/StatusLabel


func _ready() -> void:
	AudioManager.play_music("underwater_theme.mp3")
	AudioManager.play_ambient_loop("underwater_ambient.ogg")


func _on_dive_pressed() -> void:
	NetworkManager.current_mode = NetworkManager.Mode.SOLO
	get_tree().change_scene_to_file("res://scenes/Main.tscn")


func _on_host_pressed() -> void:
	_host_status.text = ""
	_host_port_input.text = str(NetworkManager.DEFAULT_PORT)
	_host_port_popup.popup_centered()


func _on_host_confirm_pressed() -> void:
	var port: int = int(_host_port_input.text.strip_edges())
	_host_port_popup.hide()
	if NetworkManager.start_host(port):
		get_tree().change_scene_to_file("res://scenes/Main.tscn")
	else:
		_host_status.text = "Erreur demarrage serveur"
		_host_port_popup.popup_centered()


func _on_join_pressed() -> void:
	get_tree().change_scene_to_file("res://scenes/LobbyMenu.tscn")


func _on_options_pressed() -> void:
	_options_popup.popup_centered()


func _on_quit_pressed() -> void:
	get_tree().quit()


func _on_music_slider_value_changed(value: float) -> void:
	AudioManager.set_music_volume(linear_to_db(value))


func _on_sfx_slider_value_changed(value: float) -> void:
	AudioManager.set_sfx_volume(linear_to_db(value))
