extends CanvasLayer

@onready var bg: ColorRect = $BG
@onready var main_panel: PanelContainer = $MainPanel
@onready var settings_panel: PanelContainer = $SettingsPanel

@onready var btn_resume: Button = $MainPanel/V/BtnResume
@onready var btn_settings: Button = $MainPanel/V/BtnSettings
@onready var btn_main: Button = $MainPanel/V/BtnMainMenu
@onready var btn_quit: Button = $MainPanel/V/BtnQuit

@onready var sens_slider: HSlider = $SettingsPanel/V/SensRow/Slider
@onready var sens_value: Label = $SettingsPanel/V/SensRow/Value
@onready var vol_slider: HSlider = $SettingsPanel/V/VolRow/Slider
@onready var vol_value: Label = $SettingsPanel/V/VolRow/Value
@onready var btn_apply: Button = $SettingsPanel/V/Buttons/Apply
@onready var btn_back: Button = $SettingsPanel/V/Buttons/Back


func _ready() -> void:
	process_mode = Node.PROCESS_MODE_ALWAYS
	visible = false
	main_panel.visible = true
	settings_panel.visible = false
	layer = 100

	btn_resume.pressed.connect(_resume)
	btn_settings.pressed.connect(_open_settings)
	btn_main.pressed.connect(_to_main_menu)
	btn_quit.pressed.connect(_quit)
	btn_apply.pressed.connect(_apply_settings)
	btn_back.pressed.connect(_close_settings)
	sens_slider.value_changed.connect(func(_v: float) -> void: _update_value_labels())
	vol_slider.value_changed.connect(func(_v: float) -> void: _update_value_labels())


func _input(event: InputEvent) -> void:
	if event is InputEventKey and event.pressed and event.keycode == KEY_ESCAPE:
		if visible:
			if settings_panel.visible:
				_close_settings()
			else:
				_resume()
			get_viewport().set_input_as_handled()
		elif _can_pause():
			_open()
			get_viewport().set_input_as_handled()


func _can_pause() -> bool:
	var scene: Node = get_tree().current_scene
	if scene == null:
		return false
	# Не паузим в главном меню
	if scene.name == "MainMenu":
		return false
	return true


func _open() -> void:
	visible = true
	main_panel.visible = true
	settings_panel.visible = false
	get_tree().paused = true
	Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
	btn_resume.grab_focus()


func _resume() -> void:
	visible = false
	get_tree().paused = false
	# Восстановить захват мыши только для 3D-сцен (с FPV игроком)
	var scene: Node = get_tree().current_scene
	if scene != null and scene.has_node("PlayerFPV"):
		Input.mouse_mode = Input.MOUSE_MODE_CAPTURED


func _open_settings() -> void:
	sens_slider.value = GameState.mouse_sensitivity * 1000.0
	vol_slider.value = GameState.master_volume * 100.0
	_update_value_labels()
	main_panel.visible = false
	settings_panel.visible = true
	btn_back.grab_focus()


func _close_settings() -> void:
	settings_panel.visible = false
	main_panel.visible = true
	btn_resume.grab_focus()


func _update_value_labels() -> void:
	sens_value.text = "%.4f" % (sens_slider.value / 1000.0)
	vol_value.text = "%d%%" % int(vol_slider.value)


func _apply_settings() -> void:
	GameState.mouse_sensitivity = sens_slider.value / 1000.0
	GameState.master_volume = vol_slider.value / 100.0
	GameState._apply_audio_volume()
	GameState.save_settings()
	_close_settings()


func _to_main_menu() -> void:
	GameState.save_game(get_tree().current_scene.scene_file_path if get_tree().current_scene else "")
	visible = false
	get_tree().paused = false
	SceneManager.go_main()


func _quit() -> void:
	GameState.save_game(get_tree().current_scene.scene_file_path if get_tree().current_scene else "")
	get_tree().quit()
