extends Control


# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var spectrum_play
var spectrum_record
var recorder
var populated_devices

func capture_device_name_to_id(find_device_name):
	var capture_devices = AudioServer.capture_get_device_list()
	var current_device_id = 0
	for device in capture_devices:
		if device == find_device_name:
			return current_device_id
		current_device_id += 1

func capture_id_to_device_name(find_device_id):
	var capture_devices = AudioServer.capture_get_device_list()
	var current_device_id = 0
	for device in capture_devices:
		if current_device_id == find_device_id:
			return device
		current_device_id += 1

func playback_device_name_to_id(find_device_name):
	var playback_devices = AudioServer.get_device_list()
	var current_device_id = 0
	for device in playback_devices:
		if device == find_device_name:
			return current_device_id
		current_device_id += 1

func playback_id_to_device_name(find_device_id):
	var playback_devices = AudioServer.get_device_list()
	var current_device_id = 0
	for device in playback_devices:
		if current_device_id == find_device_id:
			return device
		current_device_id += 1

# Called when the node enters the scene tree for the first time.
func _ready():
	Globals.loadConfig() # noop if startup has done it.
	populated_devices = false
	var capture_device_list = $MarginContainer/VBoxContainer/TabContainer/Audio/GridContainer/HBoxContainer/RecordDeviceDropdown
	var playback_device_list = $MarginContainer/VBoxContainer/TabContainer/Audio/GridContainer/HBoxContainer3/PlaybackDeviceDropdown
	capture_device_list.clear()
	playback_device_list.clear()
	var capture_devices = AudioServer.capture_get_device_list()
	print(capture_devices)
	var current_device_id = 0
	for device in capture_devices:
		capture_device_list.add_item(device, current_device_id)
		current_device_id += 1
	if Globals.capture_device != "":
		capture_device_list.select(capture_device_name_to_id(Globals.capture_device))
		
	var playback_devices = AudioServer.get_device_list()
	print(playback_devices)
	current_device_id = 0
	for device in playback_devices:
		playback_device_list.add_item(device, current_device_id)
		current_device_id += 1
	if Globals.playback_device != "":
		playback_device_list.select(playback_device_name_to_id(Globals.playback_device))
		
	var idx = AudioServer.get_bus_index("Record")
	recorder = AudioServer.get_bus_effect(idx, 0)
	spectrum_play = AudioServer.get_bus_effect_instance(0,0)
	spectrum_record = AudioServer.get_bus_effect_instance(1,1)
	populated_devices = true


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass


func _on_Quit_pressed():
	get_tree().change_scene("res://main_menu.tscn")


func _on_DecordDeviceDropdown_item_selected(index):
	if !populated_devices:
		return
	var deviceString = capture_id_to_device_name(index)
	AudioServer.capture_set_device(deviceString)
	Globals.capture_device = deviceString
	Globals.saveConfig()
	


func _on_TestPlaybackButton_pressed():
	$AudioStreamPlayer.play()


func _on_PlaybackDeviceDropdown_item_selected(index):
	if !populated_devices:
		return
	var deviceString = playback_id_to_device_name(index)
	AudioServer.set_device(deviceString)
	Globals.playback_device = deviceString
	Globals.saveConfig()
