extends Control

static var volume: float = 100.0

var settings_panel: Panel

func _ready() -> void:
	process_mode = Node.PROCESS_MODE_ALWAYS
	set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
	_build_ui()
	_apply_volume(volume)

func _build_ui() -> void:
	var bg := ColorRect.new()
	bg.color = Color(0.06, 0.04, 0.10)
	bg.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
	add_child(bg)

	var title := Label.new()
	title.text = "KickSurvivors"
	title.add_theme_font_size_override("font_size", 52)
	title.add_theme_color_override("font_color", Color.WHITE)
	title.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
	title.anchor_left = 0.5
	title.anchor_right = 0.5
	title.anchor_top = 0.0
	title.offset_left = -200
	title.offset_right = 200
	title.offset_top = 110
	add_child(title)

	var vbox := VBoxContainer.new()
	vbox.add_theme_constant_override("separation", 14)
	vbox.anchor_left = 0.5
	vbox.anchor_right = 0.5
	vbox.anchor_top = 0.5
	vbox.anchor_bottom = 0.5
	vbox.offset_left = -110
	vbox.offset_right = 110
	vbox.offset_top = -90
	vbox.offset_bottom = 90
	add_child(vbox)

	vbox.add_child(_btn("Играть", _on_play))
	vbox.add_child(_btn("Настройки", _on_settings))
	vbox.add_child(_btn("Выход", _on_exit))

	_build_settings_panel()

func _btn(text: String, cb: Callable) -> Button:
	var b := Button.new()
	b.text = text
	b.custom_minimum_size = Vector2(220, 54)
	b.add_theme_font_size_override("font_size", 20)
	b.connect("pressed", cb)
	return b

func _build_settings_panel() -> void:
	settings_panel = Panel.new()
	settings_panel.visible = false
	settings_panel.anchor_left   = 0.5
	settings_panel.anchor_right  = 0.5
	settings_panel.anchor_top    = 0.5
	settings_panel.anchor_bottom = 0.5
	settings_panel.offset_left   = -200
	settings_panel.offset_right  =  200
	settings_panel.offset_top    = -130
	settings_panel.offset_bottom =  130
	add_child(settings_panel)

	var vbox := VBoxContainer.new()
	vbox.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
	vbox.alignment = BoxContainer.ALIGNMENT_CENTER
	vbox.add_theme_constant_override("separation", 18)
	settings_panel.add_child(vbox)

	var lbl := Label.new()
	lbl.text = "Настройки"
	lbl.add_theme_font_size_override("font_size", 26)
	lbl.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
	vbox.add_child(lbl)

	var vol_lbl := Label.new()
	vol_lbl.name = "VolumeLabel"
	vol_lbl.text = "Громкость: %d" % int(volume)
	vol_lbl.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
	vbox.add_child(vol_lbl)

	var slider := HSlider.new()
	slider.min_value = 0
	slider.max_value = 100
	slider.step = 1
	slider.value = volume
	slider.custom_minimum_size = Vector2(320, 32)
	slider.connect("value_changed", func(v: float) -> void:
		volume = v
		vol_lbl.text = "Громкость: %d" % int(v)
		_apply_volume(v)
	)
	vbox.add_child(slider)

	vbox.add_child(_btn("Назад", func(): settings_panel.visible = false))

func _apply_volume(v: float) -> void:
	var db := linear_to_db(v / 100.0) if v > 0.0 else -80.0
	AudioServer.set_bus_volume_db(0, db)

func _on_play() -> void:
	get_tree().change_scene_to_file("res://scenes/Main.tscn")

func _on_settings() -> void:
	settings_panel.visible = true

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