extends Node

var default_path: String = "https://music.sanplex.tech"
var selected_server: int = 0
#var default_path: String = "https://tmusic.sanplex.tech"
#var selected_server: int = 1

var is_local: bool = false

var is_debug: bool = false

var stop_after_current: bool = true
var auto_repeat_song: bool = false
var hide_next_track: bool = true
var add_to_stats: bool = true
var use_low_played_mode: bool = false
var winning_score: int = 20
var fullscreen: bool = false
var play_local: bool = false

var player_array: Array[PlayerObject]
var edit_players: bool = false
var currently_syncing: bool = false
var character_select_open: bool = false

var version: String = "1.7.0"

var whats_new: String = "Changelog:
1.7.0:
	#22: Now block add player if player name is empty
	#23: Add a log for when points are given to players
	#25: Fixed some graphical stuff
	#26: Changed so the same character can be on a song multiple times
	#27: Turning off statistics after win
1.6.1:
	#17: Removed Debug text in release
	#18: Fixed bug with new character select screen
1.6.0:
	#1: Fixed bug with inspiration list not reloading after sync
	#2: New dialog for sync
	#4: Now downloads characters from the server
	#5: New character select screen
	#6: Now show progress during sync
	#7: Blocking all requests to the server during sync
	#8: Added functionality to cache more than one song
1.5.0: Made big changes to players and the song list and how the local song list works
0.9.0-Beta: Fixed settings and updated the player view
0.8.0-Beta: Fixed reset buttons and some other small things
0.7.8-Beta: Added shortcuts. Added dialog for winner. Started cleaning code.
0.7.5-Beta: Added settings menu, most things don't do anything yet
0.7-Beta: Can now hop between songs"

var whats_left: String = "Things left to do:
Fix layout
Fix for local play"

var shortcuts: String = "Shortcuts:
Alt + S = Search
Alt + A = Add Players
Alt + Z = Reset
Alt + X = Play/Pause
Alt + C = Next Song
Alt + V = Show Answer
Alt + Enter = Fullscreen
Alt + UP = Volume up
Alt + DOWN = Volume down
Alt + LEFT = Jump back in song
Alt + Ctrl = Restart song
Alt + RIGHT = Jump forward in song
Alt + 1, 2, 3, 4, 5, 6 = Give player point
Alt + Ctrl + 1, 2, 3, 4, 5, 6 = Take point from player"

func make_request2(address: String, func_name: Callable, expect_data: bool) -> void:
	var error_handling: Callable = func(_result: int, response_code: int, _headers: PackedStringArray, body: PackedByteArray) -> void:
		print("request done to address: ", default_path + address)
		if response_code == 200:
			print("func_name: ", func_name.get_method())
			if !expect_data:
				func_name.call()
			else:
				var json: JSON = JSON.new()
				var error: int = json.parse(body.get_string_from_utf8())
				if error == OK:
					var data_received = json.get_data()
					print("data_received type: ", type_string(typeof(data_received)))
					if typeof(data_received) == TYPE_ARRAY:
						func_name.call(data_received)
					elif func_name != null:
						func_name.call(data_received)
				else:
					print("data_received type: ", type_string(typeof(body)))
					func_name.call(body)

	var http_request: HTTPRequest = HTTPRequest.new()
	add_child(http_request)
	http_request.request_completed.connect(error_handling)
	
	# Perform a GET request. The URL below returns JSON as of writing.
	print("address: ", default_path + address)
	var request_error: int = http_request.request(default_path + address)
	if request_error != OK:
		push_error("An error occurred in the HTTP request.")

func make_request3(address: String) -> void:
	var error_handling: Callable = func(_result: int, response_code: int, _headers: PackedStringArray, _body: PackedByteArray) -> void:
		print("request done to address: ", default_path + address)
		if response_code != 200:
			print("Error: " + str(response_code))
			
	var http_request: HTTPRequest = HTTPRequest.new()
	add_child(http_request)
	http_request.request_completed.connect(error_handling)
	# Perform a GET request. The URL below returns JSON as of writing.
	var request_error: int = http_request.request(default_path + address)
	if request_error != OK:
		push_error("An error occurred in the HTTP request.")
		
func make_put_request(address: String) -> void:
	var error_handling: Callable = func(_result: int, response_code: int, _headers: PackedStringArray, _body: PackedByteArray) -> void:
		print("request done to address: ", default_path + address)
		if response_code != 200:
			print("Error: " + str(response_code))
			
	var http_request: HTTPRequest = HTTPRequest.new()
	add_child(http_request)
	http_request.request_completed.connect(error_handling)
	# Perform a GET request. The URL below returns JSON as of writing.
	var headers: PackedStringArray = ["Content-Type: application/json"]
	var request_error: int = http_request.request(default_path + address, headers, HTTPClient.METHOD_PUT)
	if request_error != OK:
		push_error("An error occurred in the HTTP request.")
	
func delete_children(node: Node) -> void:
	for n: Node in node.get_children():
		node.remove_child(n)
		n.queue_free()

func delete_player_children(node: Node) -> void:
	for n: Node in node.get_children():
		print(n)
		print(n.name)
		if n.name == "HBoxContainer":
			pass
		elif n.name == "AddPlayerContainer":
			pass
		else:
			node.remove_child(n)
			n.queue_free()
