package eu.friedcutlet.yokan.services.extensions import eu.friedcutlet.yokan.database.DbPageUrl import eu.friedcutlet.yokan.database.DbSource import eu.friedcutlet.yokan.database.YokanDatabase import eu.friedcutlet.yokan.domain.Status import eu.friedcutlet.yokan.util.nowMillis /** * Offline stand-in used when a source's real runtime fails: serves what the database already * knows about the source (saved titles, chapters and pages) without running extension code. * It serves no videos: there is no database-backed video cache, and fabricating a stream would * hand the episode page URL to the player and hide the extension failure from the UI. */ class DatabaseFallbackRuntimeService( override val source: DbSource, private val database: YokanDatabase, ) : ExtensionRuntimeService { override suspend fun supportsLatest(): Boolean = source.supportLatest == true override suspend fun headers(): Map = mapOf("User-Agent" to database.settings().userAgent) override suspend fun popular(page: Int): SourceListing = listing(page) override suspend fun latest(page: Int): SourceListing = listing(page) override suspend fun search(query: String, page: Int, filters: List): SourceListing { val normalized = query.trim().lowercase() val items = database.mangas(source.itemType) .filter { normalized.isBlank() || it.name.orEmpty().lowercase().contains(normalized) } .map { RemoteManga(it.name.orEmpty(), it.link.orEmpty(), it.imageUrl, it.description, initialized = true) } return SourceListing(items = items, hasNextPage = false) } override suspend fun detail(url: String): RemoteDetail { val candidates = database.mangas(source.itemType).filter { it.link == url } val manga = candidates.firstOrNull { it.sourceId == source.id } ?: candidates.firstOrNull { it.sourceId == null && it.source == source.name } val chapters = manga?.let { database.chapters(it.id) }.orEmpty().map { RemoteChapter( title = it.name.orEmpty(), url = it.url, dateUpload = it.dateUpload, scanlator = it.scanlator, ) } return RemoteDetail( title = manga?.name ?: url, url = url, description = manga?.description.orEmpty(), author = manga?.author, artist = manga?.artist, thumbnailUrl = manga?.imageUrl, chapters = chapters, cast = manga?.cast, seasons = manga?.takeIf { it.fetchType == eu.friedcutlet.yokan.database.DbFetchType.Seasons }?.let { parent -> database.mangas(source.itemType).filter { it.parentId == parent.id }.map { RemoteManga(it.name.orEmpty(), it.link.orEmpty(), it.imageUrl, it.description, initialized = true, seasonNumber = it.seasonNumber, fetchesSeasons = it.fetchType == eu.friedcutlet.yokan.database.DbFetchType.Seasons) } }, ) } // Only a fresh saved list stands in for the source; a stale one would pass expired URLs off as live. override suspend fun pageList(url: String): List { val now = nowMillis() return database.pageCache() .firstOrNull { (it.urls?.contains(url) == true || it.chapterUrl == url) && it.isFresh(source.id, source.version, now) } ?.urls ?.map { DbPageUrl(it) } ?: listOf(DbPageUrl(url)) } override suspend fun htmlContent(name: String, url: String): String = "$name$url" override suspend fun filterList(): List = listOf( SourceFilter( id = "genre", name = "Genre", type = "SelectFilter", kind = SourceFilterKind.SingleSelect, entries = listOf("Action", "Drama", "Fantasy").map { FilterEntry(it, it) }, ), SourceFilter( id = "status", name = "Status", type = "SelectFilter", kind = SourceFilterKind.SingleSelect, entries = listOf("Ongoing", "Completed").map { FilterEntry(it, it) }, ), ) override suspend fun sourcePreferences(): List = listOf( SourcePreferenceValue("baseUrl", "Base URL", source.baseUrl), SourcePreferenceValue("lang", "Language", source.lang), ) private fun listing(page: Int): SourceListing { val items = database.mangas(source.itemType).map { RemoteManga( title = it.name.orEmpty(), url = it.link.orEmpty(), thumbnailUrl = it.imageUrl, description = it.description, initialized = true, ) } return SourceListing(items = items, hasNextPage = page < 2) } }