extends Node


# Declare member variables here. Examples:
# var a = 2
# var b = "text"
##TODO: Should be based on screen position for starter
onready var player_start_position :=  Vector2(50,50) 

onready var current_music_track :AudioStream

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 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_name :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("res://src/levels/" + _level_name + ".tscn" ).instance()
	#print (get_tree().get_root().get_children())
	
	for l in get_node("/root/Main/ViewportContainer/Viewport").get_children():
		#print (get_node("/root/Main").get_children())
		if l is Level:
			camera_guide.disable_tracking()
			SceneTransition.transition_dissolve("foo")
			yield(SceneTransition,"done")
			print(l)
			get_node("/root/Main/ViewportContainer/Viewport").remove_child(l)
			get_node("/root/Main/ViewportContainer/Viewport").add_child(new_level)
			get_node("/root/Main/ViewportContainer/Viewport").move_child(new_level,1)
			##TODO: Probably set start position 
			if new_level.entrances.has(_position_name):
				player_start_position = new_level.entrances[_position_name].transform.origin
			print (get_node("/root/Main/ViewportContainer/Viewport").get_children())
			#get_node("/root/Main/ViewportContainer/Viewport/CameraGuide").global_position = Vector2(160,90)
			SceneTransition.reset()
			camera_guide.transform.origin = Vector2(160,90)
			camera_guide.enable_tracking()
			#get_node("/root/Main").add_child_below_node(SceneTransition,new_level)
			

# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#	pass
