##############################################################################
### PRESENCODE TEST FILE                                                   ###
### Copyright (c) 2024 JeremyStarTM & Contributors                         ###
### Licensed under the GNU General Public License v3                       ###
###                                                                        ###
### This program is free software: you can redistribute it and/or modify   ###
### it under the terms of the GNU General Public License as published by   ###
### the Free Software Foundation, either version 3 of the License, or      ###
### (at your option) any later version.                                    ###
###                                                                        ###
### This program is distributed in the hope that it will be useful,        ###
### but WITHOUT ANY WARRANTY; without even the implied warranty of         ###
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          ###
### GNU General Public License for more details.                           ###
###                                                                        ###
### You should have received a copy of the GNU General Public License      ###
### along with this program.  If not, see <https://www.gnu.org/licenses/>. ###
##############################################################################
### example/test.gd (Example presentation script)                          ###
###                                                                        ###
### This test file serves as the entrypoint for the example presentation   ###
### bundled in Presencode's source code. It's only interesting for         ###
### developers working on Presencode and for people wanting to make their  ###
### own Presencode presentations.                                          ###
##############################################################################
extends Node

# Viewport
var viewport: Control = null

# Resources
var resources_files: Array = ["foxxo.jpg", "meme.jpg"]
var resources: Dictionary = {}

# Presentation controller registration
func _ready() -> void:
	# Print debug message
	logger.diag("Example presentation entrypoint has been loaded into memory")
	# Load resources into memory
	for resource_name in resources_files:
		var resource = preader.read_file("images/" + resource_name)
		if resource_name.ends_with(".jpg"):
			var image: Image = Image.new()
			image.load_jpg_from_buffer(resource)
			resource = ImageTexture.create_from_image(image)
		elif resource_name.ends_with(".png"):
			var image: Image = Image.new()
			image.load_png_from_buffer(resource)
			resource = ImageTexture.create_from_image(image)
		#elif resource_name.ends_with(".gif"):
		#	resource = GifManager.sprite_frames_from_buffer(resource)
		resources.merge({resource_name: resource}, true)
	# Register controller with these arguments:
	## version: 1
	## max slides: 3
	## animations: no
	## quit on last slide: no
	## controller: this script
	pmana.register(1, 2, false, false, self.get_path())

# pmana has registered this controller
func presentation_start(viewport_: Control) -> void:
	logger.diag("presentation_start() called")
	viewport = viewport_
	# Hide log output
	pmana.hide_log()
	# Switch to slide 0
	# Note: We explicitly don't call pmana.change_slide(0) here as
	#       current_slide is set to 0 already. pmana expects that the
	#       presentation controller switches to slide zero automatically.
	change_slide(0)

# Presentation has ended
## This function will only be called if the controller or the user exceeds
## the maximum amount of slides by one (if quit_last_slide=true) or by two
## (if quit_last_slide=false) and Presencode wants to shut down.
## This is useful as you can unregister this controller and register another
## without calling this function.
func presentation_end() -> void:
	logger.diag("presentation_end() called")
	queue_free()

# Change the current slide to another one
func change_slide(new_slide: int) -> void:
	logger.diag("change_slide(new_slide=" + str(new_slide) + ") called")
	match(new_slide):
		0:
			logger.info("Displaying slide about memes")
			pmana.clear_viewport()
			var npr: NinePatchRect = NinePatchRect.new()
			npr.name = "Educational Meme"
			npr.texture = resources["meme.jpg"]
			npr.size = Vector2(680, 453)
			npr.position = Vector2(100, 50)
			viewport.add_child(npr)
		1:
			logger.info("Displaying slide about foxes")
			pmana.clear_viewport()
			var npr: NinePatchRect = NinePatchRect.new()
			npr.name = "Fox"
			npr.texture = resources["foxxo.jpg"]
			npr.size = Vector2(400, 400)
			npr.position = misc.get_center_float(viewport.size, npr.size)
			viewport.add_child(npr)
		2:
			logger.info("Displaying slide about mario")
			pmana.clear_viewport()
		#	var as2d: AnimatedSprite2D = AnimatedSprite2D.new()
		#	as2d.name = "Mario Melee"
		#	as2d.sprite_frames = resources["mario.gif"]
		#	var size: Vector2 = as2d.sprite_frames.get_frame_texture(as2d.sprite_frames.get_animation_names()[0], 0).get_size()
		#	as2d.position = Vector2(viewport.size.x-size.x, viewport.size.y-size.y)
		#	viewport.add_child(as2d)
		#	as2d.play()
		_:
			logger.error("Invalid slide")

# Display the end slide, unused if quit_last_slide=true
func display_end_slide() -> void:
	logger.diag("display_end_slide() called")
	logger.info("Displaying end slide")
	pmana.clear_viewport()
	var rtl: RichTextLabel = RichTextLabel.new()
	rtl.name = "End"
	rtl.text = "THE END"
	rtl.scroll_active = false
	rtl.size = Vector2(69, 22)
	rtl.position = misc.get_center_float(viewport.size, rtl.size)
	viewport.add_child(rtl)