extends Control


# Declare member variables here. Examples:
# var a = 2
# var b = "text"

var lastcheck = 0

# 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):
	lastcheck += delta
	if lastcheck > 1:
		print("Last check > 1...")
		lastcheck = 0;
		$HTTPRequest.request("http://127.0.0.1:10435/status/")
	#	#$HTTPRequest.request("http://www.mocky.io/v2/5185415ba171ea3a00704eed")
	


func _on_Control_ready():
	# TODO: cross-platform how?
	#var command = OS.get_user_data_dir()+"/youtube-dl.exe -o "+OS.get_user_data_dir()+"/temp https://www.youtube.com/watch?v=x1T6QFpd0J4 > "+OS.get_user_data_dir()+"/ytdl.log"
	#print(command)
	#OS.execute("CMD.exe", ["/C", command], false, output)
	#for line in output:
	#	print(line)
	#o.open(OS.get_user_data_dir()+"/ytdl.log", File.READ)
	#var line = ""
	#while !o.eof_reached():
	#	line = o.get_line()
	#	print(line)
	#print(OS.get_user_data_dir())
	#$HTTPRequest.request("http://127.0.0.1:10435/get?url=https://player.vimeo.com/video/459151338")
		#https://www.youtube.com/watch?v=x1T6QFpd0J4
	pass


func _on_HTTPRequest_request_completed(result, response_code, headers, body):
	#print(result)
	#print(response_code)
	#print(headers)
	#print(body.get_string_from_utf8())
	var json = JSON.parse(body.get_string_from_utf8())
	if json.result != null:
		$VBoxContainer2/DownloadProgress.value = json.result["percentage"]
		if json.result["percentage"] == 100:
			#Globals.video_duration = json.result["duration_seconds"]
			get_tree().change_scene("res://justplayer.tscn")
	#print(json.result)
	#print(json.result["percentage"])
	#pass


func _on_DownloadProgress_changed():
	pass # Replace with function body.


func _on_Button_pressed():
	print("http://127.0.0.1:10435/get?url="+$HBoxContainer/TextEdit.text)
	$HTTPRequest.request("http://127.0.0.1:10435/get?url="+$HBoxContainer/TextEdit.text)
