package link.roadio.riff.player.library import link.roadio.cuetree.CatalogView import link.roadio.cuetree.Flags import link.roadio.cuetree.NodeKind import link.roadio.riff.indexer.core.media.MediaId import link.roadio.riff.player.storage.StoredAudiobookOverride import link.roadio.riff.ui.trackReactionKey /** Playback needs classification and album membership, independent of browser ordering and directory rows. */ internal class CuetreePlaybackFacts( val snapshot: CatalogView, private val overrides: Map, hash: (ByteArray) -> Long, ) { val browser by lazy { CuetreeProfileFacts(this, hash) } val albumBySource = IntArray(snapshot.nodes.size) { -1 } private val automaticAlbums = BooleanArray(snapshot.nodes.size) private val mediaFlags = ByteArray(snapshot.nodes.size) private val folderChoices = ByteArray(snapshot.nodes.size) val albumSources = Array(snapshot.albums.size) { album -> snapshot.albumSources(album).toIntArray().also { sources -> val automatic = snapshot.albumHasAttribute(album, "automaticBook", "1") for (source in sources) { albumBySource[source] = album if (automatic) automaticAlbums[source] = true } } } fun automaticAt(index: Int) = flags(index) and 1 != 0 fun bookAt(index: Int) = flags(index) and 2 != 0 @Synchronized private fun flags(index: Int): Int { val existing = mediaFlags[index].toInt() if (existing != 0) return existing val key = snapshot.nodeId(index) val canonical = MediaId(key.hi, key.lo ?: 0L, key.lo != null) val members = snapshot.sources(key) val representative = members.firstOrNull(snapshot::nodeAvailable) ?: members.first() val automatic = members.any { automaticAlbums[it] } || snapshot.nodeHasAttribute(representative, "automaticBook", "1") val reaction = if (members.any { snapshot.nodeFlags(it) and Flags.SHRT != 0 }) MediaId(canonical.high, 0, false) else canonical var book = automatic if (overrides.isNotEmpty()) { val source = representative val album = albumBySource[source] val choice = overrides[trackReactionKey(reaction)] ?: overrides[trackReactionKey(canonical)] ?: album.takeIf { it >= 0 }?.let { overrides["album:${snapshot.albumId(it).toULong().toString(16)}"] } ?: folderChoice(source) when (choice) { StoredAudiobookOverride.BOOK -> book = true StoredAudiobookOverride.MUSIC -> book = false null -> Unit } } val flags = 4 or (if (automatic) 1 else 0) or (if (book) 2 else 0) members.forEach { mediaFlags[it] = flags.toByte() } return flags } private fun folderChoice(index: Int): StoredAudiobookOverride? { val parent = snapshot.parentOf(index) ?: return null if (snapshot.nodeKind(parent) != NodeKind.DIRECTORY) return folderChoice(parent) if (folderChoices[parent].toInt() == 0) { val choice = overrides["folder:${snapshot.nodeId(parent).hi.toULong().toString(16)}"] ?: folderChoice(parent) folderChoices[parent] = when (choice) { null -> 1 StoredAudiobookOverride.MUSIC -> 2 StoredAudiobookOverride.BOOK -> 3 } } return when (folderChoices[parent].toInt()) { 2 -> StoredAudiobookOverride.MUSIC 3 -> StoredAudiobookOverride.BOOK else -> null } } }