extends Node

@export var master_bus_index : int = 0
@export var music_bus_index : int = 1
@export var sfx_bus_index : int = 2

@export var remappable_input_actions = [
	"move_up",
	"move_down",
	"move_left",
	"move_right",
	"attack",
	"move_up_controller",
	"move_down_controller",
	"move_left_controller",
	"move_right_controller",
	"attack_controller",
	"aim_up_controller",
	"aim_left_controller",
	"aim_down_controller",
	"aim_right_controller"
]

const resolutions_dictionary : Dictionary = {
	"640x480" : Vector2(640, 480),
	"800x600" : Vector2(800, 600),
	"1024x546" : Vector2(1024, 546),
	"1280x720" : Vector2(1280, 720),
	"1600x900" : Vector2(1600, 900),
	"1366x768" : Vector2(1366, 768),
	"1920x1080" : Vector2(1920, 1080),
	"1920x1200" : Vector2(1920, 1200),
	"2560x1440" : Vector2(2560, 1440),
	"3840x2160" : Vector2(3840, 2160),
}

# Video Global Settings
func set_display_mode(value: int) -> void:
	Save.game_data.display_mode = value
	Save.save_data()
	if value == 0: DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
	elif value == 1: DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
	elif value == 2: DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_MAXIMIZED)
	elif value == 3: DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_MINIMIZED)
	elif value == 4: DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_EXCLUSIVE_FULLSCREEN)
	else: DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
	pass

func  toggle_vsync(value : bool) -> void:
	Save.game_data.vsync_on = value
	Save.save_data()
	if value: DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_ENABLED)
	else: DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_DISABLED)
	pass

func set_resolution(index):
	Save.game_data.current_resolution_index = index
	Save.save_data()
	for i in resolutions_dictionary.keys().size():
		if i == index: DisplayServer.window_set_size(resolutions_dictionary.values()[i])

# Audio Global Settings
func update_master_volume(vol : int) -> void:
	Save.game_data.master_volume = vol
	Save.save_data()
	AudioServer.set_bus_volume_db(master_bus_index, vol)
	pass

func update_music_volume(vol : int) -> void:
	Save.game_data.music_volume = vol
	Save.save_data()
	AudioServer.set_bus_volume_db(music_bus_index, vol)
	pass

func update_sfx_volume(vol : int) -> void:
	Save.game_data.sfx_volume = vol
	Save.save_data()
	AudioServer.set_bus_volume_db(sfx_bus_index, vol)
	pass

# Controls
func set_controls_from_save_file() -> void:
	for remappable_input_action in remappable_input_actions:
		var action = Save.game_data.controls.keys().find(remappable_input_action)
		for key in InputMap.action_get_events(remappable_input_action):
			InputMap.action_erase_event(remappable_input_action, key)
		var binding = Save.game_data.controls.values()[action]
		var control_event
		match str(binding)[0]:
			"m":
				binding = int(binding.right(binding.length()-1))
				control_event = InputEventMouseButton.new()
				control_event.button_index = binding
			"b":
				binding = int(binding.right(binding.length()-1))
				control_event = InputEventJoypadButton.new()
				control_event.button_index = binding
			"j":
				binding = binding.right(binding.length()-1)
				binding = binding.split("/")
				control_event = InputEventJoypadMotion.new()
				control_event.axis = int(binding[0])
				control_event.axis_value = float(binding[1])
			_:
				control_event = InputEventKey.new()
				control_event.set_keycode(binding)
		InputMap.action_add_event(remappable_input_action, control_event)
	pass

func reset_controls_to_save_file() -> void:
	for remappable_input_action in remappable_input_actions:
		var action = Save.default_game_data.controls.keys().find(remappable_input_action)
		for key in InputMap.action_get_events(remappable_input_action):
			InputMap.action_erase_event(remappable_input_action, key)
		var binding = Save.default_game_data.controls.values()[action]
		var control_event
		match str(binding)[0]:
			"m":
				binding = int(binding.right(binding.length()-1))
				control_event = InputEventMouseButton.new()
				control_event.button_index = binding
			"b":
				binding = int(binding.right(binding.length()-1))
				control_event = InputEventJoypadButton.new()
				control_event.button_index = binding
			"j":
				binding = binding.right(binding.length()-1)
				binding = binding.split("/")
				control_event = InputEventJoypadMotion.new()
				control_event.axis = int(binding[0])
				control_event.axis_value = float(binding[1])
			_:
				control_event = InputEventKey.new()
				control_event.set_keycode(binding)
		InputMap.action_add_event(remappable_input_action, control_event)
	pass
