extends Node2D

var packname = "01Vivi.gts"
var artwork = "01Vivi.png"

var installed = false

signal pack_download
signal pack_complete


func _ready():
	var filename_dlc = packname.replace(".gts", "")
	if DLC.get_content_list().has(filename_dlc):
		$Download.text = "Pack installed!"
		installed = true

func change_text(title, description, author, version):
	$Title.text = title + " - " + description
	$Owner.text = "Owner of the Character: " + author
	$Version.text = "Version: v" + str(float(version))

#01_Vivi.png
func load_image():
	# Create an HTTP request node and connect its completion signal.
	var http_request = HTTPRequest.new()
	add_child(http_request)
	http_request.connect("request_completed", self, "_http_request_completed")

	# Perform the HTTP request. The URL below returns a PNG image as of writing.
	var error = http_request.request("URL/packs/" + artwork)
	if error != OK:
		push_error("An error occurred in the HTTP request.")


# Called when the HTTP request is completed.
# warning-ignore:unused_argument
# warning-ignore:unused_argument
# warning-ignore:unused_argument
func _http_request_completed(result, response_code, headers, body):
	var image = Image.new()
	var error = image.load_png_from_buffer(body)
	if error != OK:
		push_error("Couldn't load the image.")

	var texture = ImageTexture.new()
	texture.create_from_image(image)
	$DlcImage.texture = texture



#01_Vivi.gts
func download_pack():
	Server.connect("download_complete", self, "download_complete")
	Server.connect("download_process", self, "download_process")
	Server.download("packs/" + packname, "URL/packs/" + packname)
	emit_signal("pack_download")
	$Download.text = "Downloading, wait..."
	installed = true
	$Selection.hide()

func download_process(file_size, file_total):
	var total = int(file_size / 1024 / 1024)
	$Download.text = "Downloaded " + str(total) + "MB..."

func download_complete(file_size):
	Server.disconnect("download_complete", self, "download_complete")
	Server.disconnect("download_process", self, "download_process")
	$Download.text = "Pack installed!"
	emit_signal("pack_complete")
	installed = true
 


