/*************************************************************************************************************/
/****  Carpeta / tipo contenido ******************************************************************************/
/*************************************************************************************************************/

//inits
def tipoYaSeteado=false
def prodCountries= {try{info.ProductionCountries} catch(e){[]}}()
def certifications={try{info.Certifications} catch(e){[]}}()
def pais = {try{country} catch (err) {null}}()
if (pais==null) pais = {try{info.country} catch (err) {null}}()
def numPaises = prodCountries.size()
def llengu = {try {def langs=[]; for (lan in languages) langs.add(lan.toString()); return langs } catch(err) {[]}}()
def audioLlengu = {try {def langs=[]; for (lan in audioLanguages) langs.add(lan.toString()); return langs } catch(err) {[]}}()
def llenguaOriginal = {try{info.OriginalLanguage} catch(e){null}}()

//animacion (no concluyente)
def fakeAnimacionList = ["blue & malone", "cool world"]
def tieneAnimacion = genres.any{ it =~ /Anima.i.n/} && !fakeAnimacionList.any{localize.English.n.toLowerCase().contains(it)}
//familiar (no conclud)
def tieneFamiliar = genres.any{ it =~ /Familia|Family/}
//docu
def esDocu = genres.any{ it =~ /Documental|Documentary|Reality/} && !(localize.English.n =~ /Jackass/)
//Anime ---------- (Animacion encara no es pot perquè depen del infantil)
def esAnime = false
if (tieneAnimacion && llenguaOriginal=='ja') {
	esAnime = (numPaises==1 || anime || pais=="JP") 
	&& ["nemo"].any{!localize.English.n.toLowerCase().contains(it)}
}
//musica
def esMusica = genres.any{ it =~ /Música|Music/}

tipoYaSeteado = esDocu || esAnime

//***** familiar vs infantil ****
def esFamiliar = tieneFamiliar
def	infantil = false

if (!tipoYaSeteado) {
	def tieneCertificacion = certifications!=null && certifications.size()!=0
	def detectadoNoInfantil = tieneCertificacion && (
				(certifications.US!=null && certifications.US==~/PG-13|R/)
			||	(certifications.ES!=null && certifications.ES==~/16|18/))

	if (detectadoNoInfantil) infantil=false
	else if (tieneFamiliar && tieneAnimacion) infantil=true
	else if (tieneAnimacion) {
		if (tieneCertificacion) {
				infantil = (certifications.US!=null ? certifications.US!="R" : true) &&
					(certifications.US=="G"
					|| certifications.ES==~/Ai|APTA/
					|| certifications.DE==~/0/
					|| certifications.IT=="T" 
					|| certifications.IE=="G" 
					|| certifications.LT=="V" || certifications.RU ==~ /6.+/
					|| certifications.BR=="L"
					|| certifications.NO=="A"
					|| certifications.FR=="U")
		} else {
			def titulosInfantiles = ['boonie bears', 'the jungle bunch']
			if (titulosInfantiles.any { localize.English.n.toLowerCase().contains(it) })
				infantil = true
		}
	} else if (!esFamiliar && false) { //em posa pelicules que no toquen
		if (tieneCertificacion && (
				certifications.US=="G"
			)) {
			esFamiliar = true //posem a familiar pelis de nens que no son animacio
		}
	}
	tipoYaSeteado = tipoYaSeteado || infantil
}

//animacion
def esAnimacion = tieneAnimacion && !infantil && !["brexitazo","dibujos maléficos"].any{localize.Spanish.n.toLowerCase().contains(it)}

//catalana
def catalana = llenguaOriginal=~/cat?/ //|| (llengu.contains('cat') && llengu.size()<4)

//hispana
def espanyola = llenguaOriginal=='gl' || llenguaOriginal=='eu'

/** español <castellano / latino> **/
def latina = ["che: el argentino"].any{localize.Spanish.n.toLowerCase().contains(it)} 
def notSure = false
tipoYaSeteado == tipoYaSeteado || esAnimacion || catalana || espanyola || latina
if (!tipoYaSeteado && llenguaOriginal=~/es|spa/) {
	// latina ***
	def latinWhitelist = {try{info.ProductionCompanies.any{it=~/Corazón Films/}} catch(e){false} }()
			|| {try{["Kenya Marquez"].any{director==it}}catch(err){false}}()

	if (latinWhitelist) {
		latina = true
	} else if (["nightmare radio"].any{ localize.Spanish.n.toLowerCase().contains(it) } 
			|| numPaises == 0 /*en seguiment*/
			|| {try{ info.Network =~ /(?i)\bFLIXOL.\b|\b.?TVE\b|\bAntena 3\b/ }catch(e){ false } }()
			|| {try{ info.ProductionCompanies.any{it =~ /.?TVE|ESDIP|Canal Sur|Canal\+|Telecinco/} }catch(e){false}}()
			) {
		//latina = false (redundant)
		if (numPaises == 0) notSure = true
	} else {
		def paisesLatinos = ["MX", "AR", "CO", "BR", "CL", "PE", "CU", "VE", "UY"]
		def numPaisesLat = paisesLatinos.intersect(prodCountries).size()
		latina = numPaisesLat>0
		//double check fake latin
		if (latina) {
			def isActuallySpanish = 
					(certifications.size()==1 && certifications.ES!=null)
				||	{try{ movie.alternativeTitles.size()==1 && movie.alternativeTitles.containsKey("ES") }catch(e){false}}()
			if (isActuallySpanish) latina = false
		}
	}
	if (!latina) espanyola = true
	tipoYaSeteado = tipoYaSeteado || espanyola || latina
}

//asiaticas
def asiatica = false
if (!tipoYaSeteado) {
	def languajAsia = isMovie ? ["zh", "cn", "ko", "th", "vi", "ja", "id", "hi", "tl", "bo", "te"]
	: ["zho", "cn", "kor", "tha", "vie", "jpn", "ind", "hin", "tgl", "bod"]
	asiatica = languajAsia.contains(llenguaOriginal)
	tipoYaSeteado = tipoYaSeteado || asiatica
}

def muslim = false
//mohameds
if (!tipoYaSeteado) {
	def languajMohamed = ["ar", "tr", "ur"]
	muslim = languajMohamed.contains(llenguaOriginal)
	tipoYaSeteado = tipoYaSeteado || muslim
}

def eslavo = false
//mohameds
if (!tipoYaSeteado) {
	def languajEslavo = ["pl", "ru", "uk", "be", "cs", "sk", "sl", "hr", "bs", "mk", "sr", "bg", "me"]
	eslavo = languajEslavo.contains(llenguaOriginal)
	tipoYaSeteado = tipoYaSeteado || eslavo
}