// Script:         pauseAll.ltgs 
// Description:    Pause the song at the end of all loops (Second recorded loop)
//				   This script is meant to be used with Looper emulator scripts
// 				   Will only work If the $gSecondRecordLoopID global variable has been created (normally by an looper emulator script)
// 				   Warning: Once instantiated, a global variable will exist until LoopToGo is close so this script will 
// 				   still run If a song is loaded after the use of looper script emulation and will bring strange result// Arguments: 
// Arguments:
//			pauseNow :  1, will pause now and jump to start 
//						2, will pause at next bar
//						otherwise, will pause at end of loops
//	
// Author : LoopToGo 

loadScript ..\aliases.txt					// To use aliases instead of shortcut commands

//callFunction printAllMarkers "---- 1 -----"  

callFunction findTemporaryGotoMarker

//print temporaryGotoMarkerID : $temporaryGotoMarkerID

if $isPaused

	getMarkerBar markerBar $temporaryGotoMarkerID 

	variable cond1 $arg1 < 2
	variable cond2 $songBar > $markerBar
	
	//print songBar : $songBar markerBar : $markerBar	
	//print cond1 : $cond1 cond2 : $cond2

	// The jumpTo is just a safe gard
	if $cond1 + $cond2
		if $gFirstRecordLoopStart
			jumpTo $gFirstRecordLoopStart	// A jumpTo is better that setBar (no audio glitch)
		else
			jumpTo 1.0	
		endIf
	endIf
	
	record // Start recording	(from Looptogo\aliases.txt)
	
	waitDelta .05
	
	// We delete the jumpTo marker if it is still there
	getMarkerName markerName $lastCreatedMarkerID
	
	if $markerName = "jumpTo"
		deleteMarker $lastCreatedMarkerID
	endIf
	

	endScriptDynamic
endIf	

if $arg1 = 1 // 1 means pause now (not at end)
	pause
	waitForSongPause // Needed before setBar so MIDI events at bar 1 are not played
	setBar 1.0
	endScriptDynamic
endIf


alias aMarkerName PauseMarker

if $arg1 = 2  // Pause at next bar
	variable nextBar $songBar + 1 1
	
endIf



//print loopRefID : $loopRefID

if $temporaryGotoMarkerID

	// We check If the last marker was a pause marker. If yes, we suppose the user changed his mind and remove it
	getMarkerName markerName $lastCreatedMarkerID
	
	//print markerName : $markerName
		
	if $markerName = aMarkerName
		deleteMarker aMarkerName $lastCreatedMarkerID
		
		deleteAllMarkers  8192 // Delete all pause temporary markers
		
		//print deleted aMarkerName $lastCreatedMarkerID
		
		//callFunction printAllMarkers "---- 2 -----"

		
		endScriptDynamic
	endIf
  
	// the song is playing or recording, we will add a Pause marker at the end of the Second recorded loop
	if $isRecording + $isPlaying
	 
 		deleteAllMarkers  2049 // delete Function marker if-any

		ifNot $nextBar
			getMarkerBar markerBar $temporaryGotoMarkerID 
			addTemporaryPauseMarker $markerBar 	1   // 1 = remove after action
		else
			addTemporaryPauseMarker $nextBar 	1   // 1 = remove after action
		endIf
		
		setMarkerColor $lastCreatedMarkerID yellow
		setMarkerName $lastCreatedMarkerID aMarkerName	// We give a name to be able to check If it's there

		//print "Added a pause marker at " $loopEnd
		
		getMarkerName markerName $lastCreatedMarkerID
	
		//print 2: markerName : $markerName
		
		//callFunction printAllMarkers "---- 3 -----"

	endIf

endIf

function printAllMarkers

	print $arg1

	for i 1 $nbMarkers
		
		variable markerID markerID & $i
		getMarkerName markerName  $$markerID
		getMarkerBar markerBar $$markerID
		getMarkerType markerType $$markerID
		
		print $i " = " $markerName $markerBar $markerType
	
	endFor
	
	print "-----------"

endFunction

function findTemporaryGotoMarker

	//print In findTemporaryGotoMarker

	parentVariable temporaryGotoMarkerID ""
	
	for i 1 $nbMarkers
		
		variable markerID markerID & $i
		
		getMarkerType markerType $$markerID
		
		//print $i " = " $$markerID $markerType

		
		if $markerType = 64 // Temporary goto marker
			
			parentVariable temporaryGotoMarkerID $$markerID
			
			variable i $nbMarkers
			
		endIf
	
	endFor
	
endFunction