extends Node

## LevelInfo 
# Intended to allow nodes to get info about game levels and aid
# in transition of levels.
# Resets the camera, translates start position for players from 
# the previously loaded level to the new one.
# Also invokes scene transition while nodes load. 

##TODO: Should be based on screen position for starter
onready var player_start_position :=  Vector2(50,50) 

onready var current_music_track :AudioStream

##TODO: Main can probably be derived from project properties
onready var main = get_node("/root/Main")
onready var viewport_container = get_node("/root/Main/ViewportContainer") #$ViewportContainer
onready var viewport = get_node("/root/Main/ViewportContainer/Viewport") # $ViewportContainer/Viewport
onready var camera_guide :CameraGuide = get_node("/root/Main/ViewportContainer/Viewport/CameraGuide")

func _ready():
	assert(main != null, "LevelInfo: No Main Scene found.")
	assert(viewport_container != null, "LevelInfo: No Viewport Container")
	assert(viewport != null, "LevelInfo: No Viewport node found.")
	assert(camera_guide != null, "LevelInfo: No CameraGuide node found.")
	
	NetworkManager.connect("game_started", self, "_on_game_started")
	NetworkManager.connect("game_ended", self, "_on_game_ended")

func _on_game_started():
	camera_guide.enable_tracking()

func _on_game_ended():
	camera_guide.disable_tracking()
	camera_guide.reset_position()

func change_music_track(_music_track :AudioStream ):
	if current_music_track != _music_track:
		current_music_track = _music_track
		var juke_box: AudioStreamPlayer = get_node("/root/Main/MusicPlayer")
		if juke_box:
			if juke_box.playing:
				juke_box.stop()
			juke_box.stream = current_music_track
			juke_box.play()
		else:
			push_warning("Music Player '/root/Main/MusicPlayer' not found.")

func stop_music():
	var juke_box: AudioStreamPlayer = get_node("/root/Main/MusicPlayer")
	if juke_box:
		if juke_box.playing:
			juke_box.stop()

func change_level(_level_path :String, _position_name :String):
	#print("res://src/levels/" + _level_name + ".tscn" )
	var SceneTransition = get_node("/root/Main/ViewportContainer/Viewport/SceneTransition")
	#var simultaneous_scene = preload("res://levels/level2.tscn").instance()
	var new_level = load( _level_path ).instance()
	#print (get_tree().get_root().get_children())
	
	for l in viewport.get_children():
		#print (get_node("/root/Main").get_children())
		if l is Level:
			camera_guide.disable_tracking()
			##TODO: Disabled signal based yield because I think it was taking too long
			# No more errors but I'd like to know how to do this.
			#SceneTransition.transition_dissolve("foo")
			#yield(SceneTransition,"done")
			yield(SceneTransition.transition_dissolve(), "completed")
			if (main.debug_singletons):
				print(l)
			viewport.remove_child(l)
			viewport.add_child(new_level)
			viewport.move_child(new_level,SceneTransition.get_index() + 1)
			##TODO: Probably set start position 
			if new_level.entrances.has(_position_name):
				player_start_position = new_level.entrances[_position_name].transform.origin
			
			SceneTransition.reset()
			#camera_guide.transform.origin = Vector2(160,90)
			camera_guide.reset_position()
			camera_guide.enable_tracking()

