extends Node2D

var pid_of_dlserver = 0
var capture_device = "Default"
var playback_device = "Default"
var audio_delay_seconds = 0.0
var config_loaded = false
var config = ConfigFile.new()
# Declare member variables here. Examples:
# var a = 2
# var b = "text"


# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


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

func _notification(what):
	if what == MainLoop.NOTIFICATION_WM_QUIT_REQUEST:
		var h = HTTPClient.new()
		var err = h.connect_to_host("127.0.0.1", 10435) # Connect to host/port.
		assert(err == OK)
		while h.get_status() == HTTPClient.STATUS_CONNECTING or h.get_status() == HTTPClient.STATUS_RESOLVING:
			h.poll()
			print("Connecting...")
			OS.delay_msec(500)
		assert(h.get_status() == HTTPClient.STATUS_CONNECTED) # Could not connect
		var headers = [
			"User-Agent: Pirulo/1.0 (Godot)",
			"Accept: */*"
		]
		h.request(HTTPClient.METHOD_GET, "/quit/", headers)
		get_tree().quit() # default behavior

func loadConfig():
	if !config_loaded:
		config.load("user://settings.cfg")
		if not config.has_section_key("audio","playback_device"):
			config.set_value("audio","playback_device","Default")
		playback_device = config.get_value("audio","playback_device","Default")
		AudioServer.set_device(playback_device)
		if not config.has_section_key("audio","capture_device"):
			config.set_value("audio","capture_device","Default")
		capture_device = config.get_value("audio","capture_device","Default")
		AudioServer.capture_set_device(capture_device)
		if not config.has_section_key("audio","audio_delay_seconds"):
			config.set_value("audio","audio_delay_seconds",0.0)
		audio_delay_seconds = config.get_value("audio","audio_delay_seconds",0.0)
		config.save("user://settings.cfg")
		config_loaded = true
	
func saveConfig():
	config.load("user://settings.cfg")
	config.set_value("audio","playback_device",playback_device)
	config.set_value("audio","capture_device",capture_device)
	config.set_value("audio","audio_delay_seconds",audio_delay_seconds)
	config.save("user://settings.cfg")
	
