extends RichTextLabel

@export var stopwatch: Stopwatch
@export var spawner: Node2D
var config = ConfigFile.new()

const API_BASE = "https://ape-ame.ultrablob.me/api"
#const API_BASE = "http://localhost:5001"

func make_urlsafe(data: String):
	return data.replace("+", "-").replace("/", "_")

func get_scores():
	$HTTPRequest.request(API_BASE + "/leaderboard")
	
func submit_score():
	if OS.is_debug_build():
		text = "Debug Build\nLeaderboard Disabled"
		return
	var err = config.load("user://settings.cfg")
	if not (err == OK and config.has_section_key("config", "username")):
		text = "Error: No Username!\nSet a username in settings to submit scores"
	var encrypted_score = "EGH" + make_urlsafe(Marshalls.utf8_to_base64(str(stopwatch.time_since_wave if stopwatch.time_since_wave != 0 else stopwatch.time_elapsed))) + "eHa" + make_urlsafe(Marshalls.utf8_to_base64(config.get_value("config", "username")) + "lAx" + make_urlsafe(Marshalls.utf8_to_base64(str(spawner.wave_count-1))))
	$HTTPRequest.request_completed.connect(_on_request_completed)
	$HTTPRequest.request(API_BASE + "/leaderboard?score=%s" % encrypted_score, [], HTTPClient.METHOD_POST)

func _on_request_completed(result, response_code, headers, body):
	var json = JSON.parse_string(body.get_string_from_utf8())
	#print(json)
	if json and "scores" in json and "position" in json:
		var scoredata = json["scores"]
		var scores = []
		for score in scoredata:
			scores.append(float(score[1]))
		#print(scores)
		#print(scoredata)
		var placement = float(json["position"])
		print(placement)
		
		$"../Summary".text = "You reached wave %d in %s" % [spawner.wave_count-1, seconds2mmss(stopwatch.time_since_wave if stopwatch.time_since_wave != 0 else stopwatch.time_elapsed)]
		if placement >= 1:
			text = "[center]You placed #%d\n[/center]" % [placement]
		else:
			text = "[center]You placed top %d%%\n[/center]" % [placement * 100]
		var n = 1
		while n <= 50 and n <= len(scores):
			#print(n)
			#print(scoredata[n-1])
			text += "[fill]%s #%d Wave %s %s[/fill]" % [ scoredata[n-1][0], n, scoredata[n-1][2], seconds2mmss(scores[n-1]) ]
			#print([ scoredata[n-1][0], n, scoredata[n-1][2], seconds2mmss(scores[n-1]) ])
			n += 1

func closest(my_number:int, my_array:Array)->int:
	# Initialize
	var closest_num:int
	var closest_delta:int = 0
	var closest_idx = -1
	var temp_delta:int = 0
	# Loop through entire array
	for i in range(my_array.size()):
		if my_array[i] == my_number:
			print("Exact Match!")
			return my_array[i] # exact match found!
		temp_delta = int(abs(my_array[i]-my_number))
		if closest_delta == 0 or temp_delta < closest_delta:
			closest_num = my_array[i]
			closest_idx = i
			closest_delta = temp_delta
	# Return closest number found
	return closest_idx

func seconds2mmss(total_seconds: float) -> String:
	var seconds: float = fmod(total_seconds , 60.0)
	var minutes: int   =  int(total_seconds / 60.0) % 60
	var hhmmss_string: String = "%02d:%05.2f" % [minutes, seconds]
	return hhmmss_string
