extends Control

var players = []

# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var AudioSyncs = []
var AudioStreams = []
var AudioVolumes = []
var playing = false
export(VideoStreamWebm) var VideoStream
export(bool) var paused
export(int) var VideoDurationSeconds
export(float) var stream_position
export(float) var volume_db
signal finished

# Called when the node enters the scene tree for the first time.
func _ready():
	$VideoPlayer.stream = VideoStream
	$VideoPlayer.paused = paused
	$TextureProgress.max_value = VideoDurationSeconds
	$VideoPlayer.volume_db = volume_db
	print(">>>count is "+String(len(AudioSyncs))+","+String(len(players))+","+String(len(AudioStreams)))

func play():
	$VideoPlayer.play()
	playing = true
	
func stop():
	$VideoPlayer.stop()
	for playerid in range(0,len(players)):
		print(">>>Stopping player"+String(playerid))
		players[playerid].stop()
	playing = false
	
func set_VideoStream(new_stream):
	VideoStream = new_stream
	if !is_inside_tree(): return
	$VideoPlayer.stream = new_stream
	print(">>>VideoStream is now "+new_stream.to_string())
	
func set_paused(value):
	if !is_inside_tree(): return
	$VideoPlayer.paused = value
	paused = value
	print(">>>Paused is now "+String(value))
	
func set_VideoDurationSeconds(value):
	if !is_inside_tree(): return
	$TextureProgress.max_value = value
	VideoDurationSeconds = value
	print(">>>VideoDurationSeconds is now "+String(value))

func set_volume_db(value):
	if !is_inside_tree(): return
	$VideoPlayer.volume_db = value
	volume_db = value
	print(">>>volume_db is now "+String(value))

func set_audio_volume_db(which, value):
	players[which].set_volume_db(value)

func set_stream_delay(which, value):
	if playing:
		if !paused:
			$VideoPlayer.paused = true
			for playerid in range(0,len(players)):
				players[playerid].stream_paused = true
		players[which].seek($VideoPlayer.stream_position + value)
		if !paused:
			$VideoPlayer.paused = false
			for playerid in range(0,len(players)):
				players[playerid].stream_paused = false
	AudioSyncs[which] = value

func get_stream_position():
	return $VideoPlayer.stream_position

func add_audio_stream(stream, delay):
	var player = AudioStreamPlayer.new()
	player.set_stream(stream)
	player.volume_db = 1
	player.pitch_scale = 1
	add_child(player)
	players.append(player)
	AudioSyncs.append(delay)
	AudioStreams.append(stream)
	print(">>>added stream, count is now "+String(len(AudioSyncs))+","+String(len(players))+","+String(len(AudioStreams)))

func play_combined():
	playing = true
	$VideoPlayer.play()
	for playerid in range(0,len(players)):
		print(">>>Playing player"+String(playerid))
		players[playerid].play()
		players[playerid].stream_paused = true
		players[playerid].seek(AudioSyncs[playerid])
		players[playerid].stream_paused = false
	
	
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	$TextureProgress.value = $VideoPlayer.stream_position


func _on_VideoPlayer_finished():
	playing = false
	for playerid in range(0,len(players)):
		print(">>>Stopping player"+String(playerid))
		players[playerid].stop()
	emit_signal("finished")
