extends Node

const MAX_NUMBER_OF_PLAYERS: int = 7

var default_path: String = "https://music.sanplex.xyz"
var selected_server: int = 0

var is_local: bool = false

var is_debug: bool = false

var stop_after_current: bool = true
var auto_repeat_song: bool = true
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 inspiration_list_speed: int = 1
var hide_for_seconds: float = 5.0
var hide_beginning: bool = false
var hide_length: bool = false
var hide_ticks: 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.8.5"

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()
