
/********************************************************/
/********************************************************/
/************** FUNCIONES *******************************/
/********************************************************/
/********************************************************/


/********* AUDIO *****************/
def removeLastComa(txt) {
    return txt.substring(0, txt.size()-2)
}
def getMaxQuality (mapAudioIndexedByFormat) { 
    def formatSet = ["PCM", "MLP FBA" ,"DTS", "FLAC", "AC-3", "E-AC-3", "EAC3", "AAC", "MPEG Audio", "MP3"]
	for (cFormat in formatSet) {
        if (mapAudioIndexedByFormat.containsKey(cFormat)){
            def difChannelsList = mapAudioIndexedByFormat[cFormat]
            return difChannelsList.size()==1 ? difChannelsList[0] : getBestChannelFromList(difChannelsList)
        }
	}
    return null
}
def printAudio (audio2Print) {
	def format = audio2Print.Format
    def fCom = audio2Print.Format_Commercial
	switch (format) {
		case "AC-3"	-> fCom = "DD"
		case "E-AC-3" -> fCom = "DD+"
		case "MPEG Audio" -> fCom = "MPEG"
		case "DTS" -> { if (fCom.contains("Master Audio")) fCom="DTS_MA"
                        else if (fCom.contains("DTS-HD High Resolution Audio")) fCom="DTS_HRA"
						else if (fCom.contains("DTS-ES")) fCom="DTS-ES"
					}
        case "MLP FBA" -> {def tmp = "D_HD"; if (fCom.contains("Atmos")) tmp+="_Atmos"; fCom = tmp}
	}
	def canales = audio2Print.Channels.replace(2:'2.0', 3:'2.1', 6:'5.1', 7:'6.1', 8:'7.1')
	return fCom + ' ' + canales
}
def getBestChannelFromList (audiosFormatFound) {
	def bestChanel
	def currentValue=0
	for (myAudio2 in audiosFormatFound) {
		if (myAudio2.Channels>currentValue) {
			currentValue = myAudio2.Channels
			bestChanel = myAudio2
		}
	}
	return bestChanel
}
def countAudiosInMap(audMap){
    int count=0
    for (aud in audMap) count += aud.value.size()
    return count
}
/********* SUBS ***************/
def handleSub(sub, capturedSubs) {
	def lang = sub.Language_String3 !=null ? sub.Language_String3 
		: (sub.Title=~/(?i).+spa.+/?'spa': sub.Title.substring(0,3).toLowerCase())
	if (sub.Title!=null && sub.Title=~/(?i).*latin.*/) lang='lat'
	def forced = sub.title!=null ? sub.title.toLowerCase().contains("forzado") : false
	if (!capturedSubs.containsKey(lang)) {
		capturedSubs[lang] = lang+(forced?' F':'') 
	} else {
		def existing=capturedSubs[lang]
		def newTxt=''+existing
		if (!existing.contains('+')) newTxt += '+'
		if (forced && !existing.contains('F')) newTxt += ' F'
		capturedSubs[lang]=newTxt
	}
}

/***********/
/* otras funciones */
def getInicial () {
	def nomIngles = localize.English.n
	if (nomIngles.startsWith("The ") || nomIngles.startsWith("A "))
		nomIngles = nomIngles.replaceFirst("The ",'').replaceFirst("A ",'')
	def ini0 = nomIngles[0].toUpperCase()
	def iniClear = nomIngles.size()>1 ? ini0.replaceAll(/[¡¿'#*\(]/,nomIngles[1].toUpperCase()) : ini0
	def inicial = iniClear.replaceAll(/[0-9]/,'#')+'/'
	return inicial
}
def sanityCheck(newId) {
	def oldId = fn.find(/\{tmdb-\d+\}/)?.find(/\d+/)
	return oldId!=null ? oldId  == newId.toString() : true
}
def normalitzarText(txt) {
	return txt.replaceAll(':','.').replaceAll('\\?','¿').replaceAll(/\"|>|\*|'/,'-')
}
/***********/
