extends "res://scenes/Map.gd"

var playground = null
var debug_obj = null
var dialog = null
var effects = null
var UI = null
var audio = null
var popup_obj
var version = "1.2"

var can_pause = true
var paused = false

var character = "01Vivi"
var coins = 0
var coin_multiplication = 1

var stomped = false

var player = null
var player_harm = false


# game speed changes, it's automatically changed.
export var game_speed = 1.0 setget game_speed_change
func game_speed_change(new_speed):
	game_speed = float(new_speed)
	Engine.time_scale = game_speed



# --------------- 1# LEVEL CONTROLS -----------------
# ---------------------------------------------------

# use actual_level_id to get next level_id on Levelmap and load it
func next_level():
	self.load_level_name(get_next_scene())
	pass

# use actual_level_id to get secret level_id on Levelmap and load it
func next_secret_level():
	self.load_level_name(get_next_secret_scene())
	pass

# get string on Levelmap by level_id and load scene
func load_level(level_id):
	self.load_level_name(get_scene(level_id))
	pass

# loading scenes by "levels/level1/scene1" string, found on Levelmap
func load_level_name(scene_name):
	self.load_scene("res://scenes/" + scene_name + ".tscn")
	

# the hard work - unload actual playground scene and load next
func load_scene(scene_name):
	
	Game.effects.is_lavarock = false
	
	if not playground:
		print("ERROR! No playground found. Nothing done.")
		return  # no playground set, no load is made
	
	if not Utils.file_exist(scene_name):
		print("ERROR! Next scene wasn't found. The actual scene continues.")
		return # nothing was loaded
	
	var scene = Utils.load_file(scene_name)
	if not scene: 
		print("ERROR! Next scene wasn't found. The actual scene continues.")
		return # nothing was loaded
	
	var instance = scene.instance()
	instance.set_name("level_scene")
	
	for node in self.playground.get_children(): node.queue_free() # empty actual scene
	
	playground.call_deferred("add_child", instance) # add actual scene there	
	
	if debug_obj: debug_obj.update_specs()
	
	# hide "HideAtStart" objects after loaded (mainly used when the scene doesn't load the "Level" class)
	var objects_to_hide = self.playground.get_tree().get_nodes_in_group("HideAtStart")
	if not objects_to_hide: return
	for object in objects_to_hide:
		object.hide()
	
func continue_game():
	load_level(MemoryCard.get_progress("continue"))


# --------------- 3# POPUP CONTROLS -----------------
# ---------------------------------------------------



func popup(text):
	popup_obj.text(text)
	popup_obj.show_popup()
	pass

func popup_confirm(obj, method):
	popup_obj.connect_confirm(obj, method)
	pass

func popup_labels(confirm, cancel):
	popup_obj.labels(confirm, cancel)

func popup_cancel(obj, method):
	popup_obj.connect_cancel(obj, method)
	pass

func hide_popup():
	popup_obj.hide_popup()



# ----------------- ### FADE LEVEL ------------------
# ---------------------------------------------------

var next_fade_level = 0
var next_fade_time = 0
var next_fade_wait = 0.4
var fade_color = "black"

#load level on face with color
func fade_level(level_id, time = 0.5, color="black"):
	next_fade_level = level_id
	next_fade_time = time
	fade_color = color
	if color == "black":
		self.effects.fade_in_black(time)
	else:
		self.effects.fade_in_white(time)
	Utils.execute_after(self, "fade_open_level", time + 0.1)

# load level but adding a different time for the fade back
func fade_level_speed(level_id, fadein = 0.5, fadeout = 0.5, color="black"):
	fade_level(level_id, fadein, color)
	next_fade_time = fadeout

# does differet fade in and fade out speeds, plus a "wait" time for allow the level to load, or for extra effect
func fade_level_wait(level_id, fadein = 0.5, wait_time = 0.4, fadeout = 0.5, color="black"):
	next_fade_time = wait_time
	fade_level_speed(level_id, fadein, fadeout, color)
	
# give some time for level to load
func fade_open_level():
	self.load_level(next_fade_level)
	Utils.execute_after(self, "fade_show_level", next_fade_wait)

# finally show the level
func fade_show_level():
	next_fade_wait = 0.4
	if fade_color == "black":
		self.effects.fade_out_black(next_fade_time)
	else:
		self.effects.fade_out_white(next_fade_time)



# --------------- ### MISCELLANEOUS -----------------
# ---------------------------------------------------

func fade_close(time = 0.5):
	Audio.fade_out(time - 0.1)
	if Game.effects:
		Game.effects.fade_in_black(time)
		Utils.execute_after(self, "close", time + 0.1)
	else: close()

func close():
	get_tree().quit()

func fps():
	var fps = Engine.get_frames_per_second()
	if fps < 15: fps = 30
	return fps

# warning-ignore:unused_argument
func _process(delta):
	Engine.time_scale = game_speed

func _ready():
	Engine.time_scale = game_speed
