// Display a report for Chords consistency
// Arguments:  
//		(no argument)
//
// Author: LoopToGo
//
// Description  : 		Display a report showing when Chords for Play loops 
//						are not the same as the Chords from the corresponding linked Record loop


showErrorAndLogConsole		// Make sure the Error/Log console is visible


variable hardCheck $arg1 // if set to 0, Em7 is equivalent to Em and Cm/G is equivalent to Cm
	

print Song contains $nbChords chords and $nbLoops Loops
print Checking if chords at play loops correspond to chords at linked record loop
print 

variable nbError 0

for n 1 $nbChords

	variable _chordName chordName & $n
	variable _chordBar chordBar & $n
	variable chordName $$_chordName
	variable chordBar $$_chordBar
	
	ifNot $hardCheck
		callFunction chord2BasicChord chordName $chordName
	endIf
	
	//print $chordName $chordBar
	
	for n2 1 $nbLoops
	
		variable _loopID	loopID & $n2
		variable loopID 	$$_loopID
		
		getLoopStart loopStart $loopID
		getLoopEnd loopEnd $loopID
		getLoopType loopType $loopID	

		
		variable condition1	 $loopType = Play
		variable condition2  $chordBar = $loopStart
		variable condition3  $chordBar > $loopStart
		variable condition4  $chordBar < $loopEnd
		
		variable condition2or3 $condition2 + $condition3
									
		if $condition1 * $condition2or3 * $condition4
		
			getLoopChordNameAtBar chordLoopName $loopID $chordBar
			
			ifNot $hardCheck
				callFunction chord2BasicChord chordLoopName $chordLoopName
			endIf
			
			getLoopLink loopLinkID $loopID	
					
			if $loopLinkID
															
				getLoopStart loopLinkStart $loopLinkID	// Start of the corresponding record loop
				
				variable correction $loopStart - $loopLinkStart
				
				variable chordBarEquivalent $chordBar - $correction
			
				ifNot $chordName = $chordLoopName
				
					getLoopName loopName $loopID
					getLoopTrack loopTrack $loopID
				
					print ERROR [ $loopTrack "-" $loopName ] : $chordName "at bar" $chordBar "is different from" $chordLoopName "at bar" $chordBarEquivalent
					variable nbError $nbError + 1
				endIf
			endIf
			
		endIf

		
	endFor
	
endFor	


if $nbError
	variable _error error
	
	if $nbError > 1
		variable _error errors
	endIf
	
	print 
	print Error : $nbError $_error found!
else
	print Great news! No error found!
endIf


function chord2BasicChord // basic [out] original [in]

	// Change Em7 to Em7
	// Change Dm7/G to Dm
	// Change E#m9 to E#m
	// Change G/B to G
	// Do not change Em E D CM, etc.
	
	//print Input: $arg2
	
	// Let's remove base note if any	
	regexReplace result $arg2 "(/.*$)" ""
	

	// Let's remove number if any
	regexReplace result $result "([0-9].*$)" ""
		
	parentVariable $arg1 $result
	
	//print Output: $result

endFunction 