extends Node

@export var back_button : Control
@export var tab_container : TabContainer
@export var bottom_item_video : Control
@export var bottom_item_audio : Control
@export var bottom_item_controls : Control

@export var remap_controller : RemapController

@export var display_options_button : OptionButton
@export var vsync_toggle : CheckButton
@export var resolutions_options_button : OptionButton

var on_back : Callable

# Audio Settings UI References
@export var master_volume_slider : Slider
@export var music_volume_slider : Slider
@export var sfx_volume_slider : Slider

# Engine Callbacks
func _ready():
	back_button.grab_focus()
	back_button.focus_neighbor_top = bottom_item_video.get_path()
	
	add_resolution_items()
	display_options_button.select(Save.game_data.display_mode)
	GlobalSettings.set_display_mode(Save.game_data.display_mode)
	vsync_toggle.set_pressed_no_signal(Save.game_data.vsync_on)
	GlobalSettings.toggle_vsync(Save.game_data.vsync_on)
	if Save.game_data.display_mode == DisplayServer.WINDOW_MODE_FULLSCREEN or Save.game_data.display_mode == DisplayServer.WINDOW_MODE_EXCLUSIVE_FULLSCREEN:
		GlobalSettings.set_resolution(Save.game_data.current_resolution_index)
	resolutions_options_button.select(Save.game_data.current_resolution_index)
	master_volume_slider.value = Save.game_data.master_volume
	GlobalSettings.update_master_volume(Save.game_data.master_volume)
	music_volume_slider.value = Save.game_data.music_volume
	GlobalSettings.update_music_volume(Save.game_data.music_volume)
	sfx_volume_slider.value = Save.game_data.sfx_volume
	GlobalSettings.update_sfx_volume(Save.game_data.sfx_volume)
	pass
	
func _process(_delta):
	update_change_tab()
	pass
	
func  update_change_tab():
	if(remap_controller.is_remapping): return
	var do_left = Input.is_action_just_pressed("ui_tab_left")
	var do_right = Input.is_action_just_pressed("ui_tab_right")
	
	if do_left:
		var new_rab = tab_container.current_tab - 1
		tab_container.current_tab = new_rab if new_rab >= 0 else tab_container.get_tab_count() - 1
	if do_right:
		var new_rab = tab_container.current_tab + 1
		tab_container.current_tab = new_rab if new_rab < tab_container.get_tab_count() else 0
		
	if do_left or do_right:
		match tab_container.current_tab:
			0: back_button.focus_neighbor_top = bottom_item_video.get_path()
			1: back_button.focus_neighbor_top = bottom_item_audio.get_path()
			2: back_button.focus_neighbor_top = bottom_item_controls.get_path()
		back_button.grab_focus()
	pass

func add_resolution_items():
	for key in GlobalSettings.resolutions_dictionary.keys():
		resolutions_options_button.add_item(key)

# Other Buttons
func _on_back_pressed():
	Save.save_data()
	on_back.call()
	queue_free()
	pass

# Video Settings
func _on_display_mode_options_item_selected(index):
	GlobalSettings.set_display_mode(index)
	pass

func _on_vsync_button_toggled(button_pressed):
	GlobalSettings.toggle_vsync(button_pressed)
	pass

func _on_resolutions_options_item_selected(index):
	GlobalSettings.set_resolution(index)
	pass

# Audio Settings
func _on_master_volume_slider_value_changed(value):
	GlobalSettings.update_master_volume(value)
	pass

func _on_music_vol_slider_value_changed(value):
	GlobalSettings.update_music_volume(value)
	pass

func _on_sfx_vol_slider_value_changed(value):
	GlobalSettings.update_sfx_volume(value)
	pass
